github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/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] NAME|ID [NAME|ID...]", 29 Short: "Return low-level information on Docker objects", 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", "plugin": 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 inspectPlugin(ctx context.Context, dockerCli *command.DockerCli) inspect.GetRefFunc { 99 return func(ref string) (interface{}, []byte, error) { 100 return dockerCli.Client().PluginInspectWithRaw(ctx, ref) 101 } 102 } 103 104 func inspectAll(ctx context.Context, dockerCli *command.DockerCli, getSize bool, typeConstraint string) inspect.GetRefFunc { 105 var inspectAutodetect = []struct { 106 ObjectType string 107 IsSizeSupported bool 108 ObjectInspector func(string) (interface{}, []byte, error) 109 }{ 110 {"container", true, inspectContainers(ctx, dockerCli, getSize)}, 111 {"image", false, inspectImages(ctx, dockerCli)}, 112 {"network", false, inspectNetwork(ctx, dockerCli)}, 113 {"volume", false, inspectVolume(ctx, dockerCli)}, 114 {"service", false, inspectService(ctx, dockerCli)}, 115 {"task", false, inspectTasks(ctx, dockerCli)}, 116 {"node", false, inspectNode(ctx, dockerCli)}, 117 {"plugin", false, inspectPlugin(ctx, dockerCli)}, 118 } 119 120 isErrNotSwarmManager := func(err error) bool { 121 return strings.Contains(err.Error(), "This node is not a swarm manager") 122 } 123 124 return func(ref string) (interface{}, []byte, error) { 125 for _, inspectData := range inspectAutodetect { 126 if typeConstraint != "" && inspectData.ObjectType != typeConstraint { 127 continue 128 } 129 v, raw, err := inspectData.ObjectInspector(ref) 130 if err != nil { 131 if typeConstraint == "" && (apiclient.IsErrNotFound(err) || isErrNotSwarmManager(err)) { 132 continue 133 } 134 return v, raw, err 135 } 136 if getSize && !inspectData.IsSizeSupported { 137 fmt.Fprintf(dockerCli.Err(), "WARNING: --size ignored for %s\n", inspectData.ObjectType) 138 } 139 return v, raw, err 140 } 141 return nil, nil, fmt.Errorf("Error: No such object: %s", ref) 142 } 143 }