github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/service/inspect.go (about) 1 package service 2 3 import ( 4 "fmt" 5 "strings" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/cli" 10 "github.com/docker/docker/cli/command" 11 "github.com/docker/docker/cli/command/formatter" 12 apiclient "github.com/docker/docker/client" 13 "github.com/spf13/cobra" 14 ) 15 16 type inspectOptions struct { 17 refs []string 18 format string 19 pretty bool 20 } 21 22 func newInspectCommand(dockerCli *command.DockerCli) *cobra.Command { 23 var opts inspectOptions 24 25 cmd := &cobra.Command{ 26 Use: "inspect [OPTIONS] SERVICE [SERVICE...]", 27 Short: "Display detailed information on one or more services", 28 Args: cli.RequiresMinArgs(1), 29 RunE: func(cmd *cobra.Command, args []string) error { 30 opts.refs = args 31 32 if opts.pretty && len(opts.format) > 0 { 33 return fmt.Errorf("--format is incompatible with human friendly format") 34 } 35 return runInspect(dockerCli, opts) 36 }, 37 } 38 39 flags := cmd.Flags() 40 flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template") 41 flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format.") 42 return cmd 43 } 44 45 func runInspect(dockerCli *command.DockerCli, opts inspectOptions) error { 46 client := dockerCli.Client() 47 ctx := context.Background() 48 49 if opts.pretty { 50 opts.format = "pretty" 51 } 52 53 getRef := func(ref string) (interface{}, []byte, error) { 54 service, _, err := client.ServiceInspectWithRaw(ctx, ref) 55 if err == nil || !apiclient.IsErrServiceNotFound(err) { 56 return service, nil, err 57 } 58 return nil, nil, fmt.Errorf("Error: no such service: %s", ref) 59 } 60 61 f := opts.format 62 if len(f) == 0 { 63 f = "raw" 64 if len(dockerCli.ConfigFile().ServiceInspectFormat) > 0 { 65 f = dockerCli.ConfigFile().ServiceInspectFormat 66 } 67 } 68 69 // check if the user is trying to apply a template to the pretty format, which 70 // is not supported 71 if strings.HasPrefix(f, "pretty") && f != "pretty" { 72 return fmt.Errorf("Cannot supply extra formatting options to the pretty template") 73 } 74 75 serviceCtx := formatter.Context{ 76 Output: dockerCli.Out(), 77 Format: formatter.NewServiceFormat(f), 78 } 79 80 if err := formatter.ServiceInspectWrite(serviceCtx, opts.refs, getRef); err != nil { 81 return cli.StatusError{StatusCode: 1, Status: err.Error()} 82 } 83 return nil 84 }