(ctx context.Context, dockerCLI command.Cli, opts inspectOptions)
| 51 | } |
| 52 | |
| 53 | func runInspect(ctx context.Context, dockerCLI command.Cli, opts inspectOptions) error { |
| 54 | apiClient := dockerCLI.Client() |
| 55 | |
| 56 | if opts.pretty { |
| 57 | opts.format = "pretty" |
| 58 | } |
| 59 | |
| 60 | getRef := func(ref string) (any, []byte, error) { |
| 61 | // Service inspect shows defaults values in empty fields. |
| 62 | res, err := apiClient.ServiceInspect(ctx, ref, client.ServiceInspectOptions{InsertDefaults: true}) |
| 63 | if err == nil || !errdefs.IsNotFound(err) { |
| 64 | return res.Service, res.Raw, err |
| 65 | } |
| 66 | return nil, nil, fmt.Errorf("no such service: %s", ref) |
| 67 | } |
| 68 | |
| 69 | getNetwork := func(ref string) (any, []byte, error) { |
| 70 | res, err := apiClient.NetworkInspect(ctx, ref, client.NetworkInspectOptions{Scope: "swarm"}) |
| 71 | if err == nil || !errdefs.IsNotFound(err) { |
| 72 | return res.Network, res.Raw, err |
| 73 | } |
| 74 | return nil, nil, fmt.Errorf("no such network: %s", ref) |
| 75 | } |
| 76 | |
| 77 | f := opts.format |
| 78 | if len(f) == 0 { |
| 79 | f = "raw" |
| 80 | if len(dockerCLI.ConfigFile().ServiceInspectFormat) > 0 { |
| 81 | f = dockerCLI.ConfigFile().ServiceInspectFormat |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // check if the user is trying to apply a template to the pretty format, which |
| 86 | // is not supported |
| 87 | if strings.HasPrefix(f, "pretty") && f != "pretty" { |
| 88 | return errors.New("cannot supply extra formatting options to the pretty template") |
| 89 | } |
| 90 | |
| 91 | serviceCtx := formatter.Context{ |
| 92 | Output: dockerCLI.Out(), |
| 93 | Format: newFormat(f), |
| 94 | } |
| 95 | |
| 96 | if err := inspectFormatWrite(serviceCtx, opts.refs, getRef, getNetwork); err != nil { |
| 97 | return cli.StatusError{StatusCode: 1, Status: err.Error()} |
| 98 | } |
| 99 | return nil |
| 100 | } |
no test coverage detected
searching dependent graphs…