github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/service/inspect.go (about) 1 // FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16: 2 //go:build go1.19 3 4 package service 5 6 import ( 7 "context" 8 "strings" 9 10 "github.com/docker/cli/cli" 11 "github.com/docker/cli/cli/command" 12 "github.com/docker/cli/cli/command/formatter" 13 flagsHelper "github.com/docker/cli/cli/flags" 14 "github.com/docker/docker/api/types" 15 "github.com/docker/docker/errdefs" 16 "github.com/pkg/errors" 17 "github.com/spf13/cobra" 18 ) 19 20 type inspectOptions struct { 21 refs []string 22 format string 23 pretty bool 24 } 25 26 func newInspectCommand(dockerCli command.Cli) *cobra.Command { 27 var opts inspectOptions 28 29 cmd := &cobra.Command{ 30 Use: "inspect [OPTIONS] SERVICE [SERVICE...]", 31 Short: "Display detailed information on one or more services", 32 Args: cli.RequiresMinArgs(1), 33 RunE: func(cmd *cobra.Command, args []string) error { 34 opts.refs = args 35 36 if opts.pretty && len(opts.format) > 0 { 37 return errors.Errorf("--format is incompatible with human friendly format") 38 } 39 return runInspect(cmd.Context(), dockerCli, opts) 40 }, 41 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 42 return CompletionFn(dockerCli)(cmd, args, toComplete) 43 }, 44 } 45 46 flags := cmd.Flags() 47 flags.StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) 48 flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format") 49 return cmd 50 } 51 52 func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error { 53 client := dockerCli.Client() 54 55 if opts.pretty { 56 opts.format = "pretty" 57 } 58 59 getRef := func(ref string) (any, []byte, error) { 60 // Service inspect shows defaults values in empty fields. 61 service, _, err := client.ServiceInspectWithRaw(ctx, ref, types.ServiceInspectOptions{InsertDefaults: true}) 62 if err == nil || !errdefs.IsNotFound(err) { 63 return service, nil, err 64 } 65 return nil, nil, errors.Errorf("Error: no such service: %s", ref) 66 } 67 68 getNetwork := func(ref string) (any, []byte, error) { 69 network, _, err := client.NetworkInspectWithRaw(ctx, ref, types.NetworkInspectOptions{Scope: "swarm"}) 70 if err == nil || !errdefs.IsNotFound(err) { 71 return network, nil, err 72 } 73 return nil, nil, errors.Errorf("Error: no such network: %s", ref) 74 } 75 76 f := opts.format 77 if len(f) == 0 { 78 f = "raw" 79 if len(dockerCli.ConfigFile().ServiceInspectFormat) > 0 { 80 f = dockerCli.ConfigFile().ServiceInspectFormat 81 } 82 } 83 84 // check if the user is trying to apply a template to the pretty format, which 85 // is not supported 86 if strings.HasPrefix(f, "pretty") && f != "pretty" { 87 return errors.Errorf("Cannot supply extra formatting options to the pretty template") 88 } 89 90 serviceCtx := formatter.Context{ 91 Output: dockerCli.Out(), 92 Format: NewFormat(f), 93 } 94 95 if err := InspectFormatWrite(serviceCtx, opts.refs, getRef, getNetwork); err != nil { 96 return cli.StatusError{StatusCode: 1, Status: err.Error()} 97 } 98 return nil 99 }