github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/system/inspect.go (about) 1 package system 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/inspect" 12 apiclient "github.com/docker/docker/client" 13 "github.com/spf13/cobra" 14 ) 15 16 type inspectOptions struct { 17 format string 18 inspectType string 19 size bool 20 ids []string 21 } 22 23 // NewInspectCommand creates a new cobra.Command for `docker inspect` 24 func NewInspectCommand(dockerCli *command.DockerCli) *cobra.Command { 25 var opts inspectOptions 26 27 cmd := &cobra.Command{ 28 Use: "inspect [OPTIONS] CONTAINER|IMAGE|TASK [CONTAINER|IMAGE|TASK...]", 29 Short: "Return low-level information on a container, image or task", 30 Args: cli.RequiresMinArgs(1), 31 RunE: func(cmd *cobra.Command, args []string) error { 32 opts.ids = args 33 return runInspect(dockerCli, opts) 34 }, 35 } 36 37 flags := cmd.Flags() 38 flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given go template") 39 flags.StringVar(&opts.inspectType, "type", "", "Return JSON for specified type") 40 flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes if the type is container") 41 42 return cmd 43 } 44 45 func runInspect(dockerCli *command.DockerCli, opts inspectOptions) error { 46 var elementSearcher inspect.GetRefFunc 47 switch opts.inspectType { 48 case "", "container", "image", "node", "network", "service", "volume", "task": 49 elementSearcher = inspectAll(context.Background(), dockerCli, opts.size, opts.inspectType) 50 default: 51 return fmt.Errorf("%q is not a valid value for --type", opts.inspectType) 52 } 53 return inspect.Inspect(dockerCli.Out(), opts.ids, opts.format, elementSearcher) 54 } 55 56 func inspectContainers(ctx context.Context, dockerCli *command.DockerCli, getSize bool) inspect.GetRefFunc { 57 return func(ref string) (interface{}, []byte, error) { 58 return dockerCli.Client().ContainerInspectWithRaw(ctx, ref, getSize) 59 } 60 } 61 62 func inspectImages(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { 63 return func(ref string) (interface{}, []byte, error) { 64 return dockerCli.Client().ImageInspectWithRaw(ctx, ref) 65 } 66 } 67 68 func inspectNetwork(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { 69 return func(ref string) (interface{}, []byte, error) { 70 return dockerCli.Client().NetworkInspectWithRaw(ctx, ref) 71 } 72 } 73 74 func inspectNode(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { 75 return func(ref string) (interface{}, []byte, error) { 76 return dockerCli.Client().NodeInspectWithRaw(ctx, ref) 77 } 78 } 79 80 func inspectService(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { 81 return func(ref string) (interface{}, []byte, error) { 82 return dockerCli.Client().ServiceInspectWithRaw(ctx, ref) 83 } 84 } 85 86 func inspectTasks(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { 87 return func(ref string) (interface{}, []byte, error) { 88 return dockerCli.Client().TaskInspectWithRaw(ctx, ref) 89 } 90 } 91 92 func inspectVolume(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { 93 return func(ref string) (interface{}, []byte, error) { 94 return dockerCli.Client().VolumeInspectWithRaw(ctx, ref) 95 } 96 } 97 98 func inspectAll(ctx context.Context, dockerCli *command.DockerCli, getSize bool, typeConstraint string) inspect.GetRefFunc { 99 var inspectAutodetect = []struct { 100 ObjectType string 101 IsSizeSupported bool 102 ObjectInspector func(string) (interface{}, []byte, error) 103 }{ 104 {"container", true, inspectContainers(ctx, dockerCli, getSize)}, 105 {"image", false, inspectImages(ctx, dockerCli)}, 106 {"network", false, inspectNetwork(ctx, dockerCli)}, 107 {"volume", false, inspectVolume(ctx, dockerCli)}, 108 {"service", false, inspectService(ctx, dockerCli)}, 109 {"task", false, inspectTasks(ctx, dockerCli)}, 110 {"node", false, inspectNode(ctx, dockerCli)}, 111 } 112 113 isErrNotSwarmManager := func(err error) bool { 114 return strings.Contains(err.Error(), "This node is not a swarm manager") 115 } 116 117 return func(ref string) (interface{}, []byte, error) { 118 for _, inspectData := range inspectAutodetect { 119 if typeConstraint != "" && inspectData.ObjectType != typeConstraint { 120 continue 121 } 122 v, raw, err := inspectData.ObjectInspector(ref) 123 if err != nil { 124 if typeConstraint == "" && (apiclient.IsErrNotFound(err) || isErrNotSwarmManager(err)) { 125 continue 126 } 127 return v, raw, err 128 } 129 if getSize && !inspectData.IsSizeSupported { 130 fmt.Fprintf(dockerCli.Err(), "WARNING: --size ignored for %s\n", inspectData.ObjectType) 131 } 132 return v, raw, err 133 } 134 return nil, nil, fmt.Errorf("Error: No such object: %s", ref) 135 } 136 }