github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/container/inspect.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/inspect"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  type inspectOptions struct {
    13  	format string
    14  	size   bool
    15  	refs   []string
    16  }
    17  
    18  // newInspectCommand creates a new cobra.Command for `docker container inspect`
    19  func newInspectCommand(dockerCli command.Cli) *cobra.Command {
    20  	var opts inspectOptions
    21  
    22  	cmd := &cobra.Command{
    23  		Use:   "inspect [OPTIONS] CONTAINER [CONTAINER...]",
    24  		Short: "Display detailed information on one or more containers",
    25  		Args:  cli.RequiresMinArgs(1),
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			opts.refs = args
    28  			return runInspect(dockerCli, opts)
    29  		},
    30  	}
    31  
    32  	flags := cmd.Flags()
    33  	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
    34  	flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes")
    35  
    36  	return cmd
    37  }
    38  
    39  func runInspect(dockerCli command.Cli, opts inspectOptions) error {
    40  	client := dockerCli.Client()
    41  	ctx := context.Background()
    42  
    43  	getRefFunc := func(ref string) (interface{}, []byte, error) {
    44  		return client.ContainerInspectWithRaw(ctx, ref, opts.size)
    45  	}
    46  	return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
    47  }