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

     1  package context
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/docker/cli/cli/command"
     7  	"github.com/docker/cli/cli/command/inspect"
     8  	"github.com/docker/cli/cli/context/store"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  type inspectOptions struct {
    13  	format string
    14  	refs   []string
    15  }
    16  
    17  // newInspectCommand creates a new cobra.Command for `docker image inspect`
    18  func newInspectCommand(dockerCli command.Cli) *cobra.Command {
    19  	var opts inspectOptions
    20  
    21  	cmd := &cobra.Command{
    22  		Use:   "inspect [OPTIONS] [CONTEXT] [CONTEXT...]",
    23  		Short: "Display detailed information on one or more contexts",
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			opts.refs = args
    26  			if len(opts.refs) == 0 {
    27  				if dockerCli.CurrentContext() == "" {
    28  					return errors.New("no context specified")
    29  				}
    30  				opts.refs = []string{dockerCli.CurrentContext()}
    31  			}
    32  			return runInspect(dockerCli, opts)
    33  		},
    34  	}
    35  
    36  	flags := cmd.Flags()
    37  	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
    38  	return cmd
    39  }
    40  
    41  func runInspect(dockerCli command.Cli, opts inspectOptions) error {
    42  	getRefFunc := func(ref string) (interface{}, []byte, error) {
    43  		c, err := dockerCli.ContextStore().GetMetadata(ref)
    44  		if err != nil {
    45  			return nil, nil, err
    46  		}
    47  		tlsListing, err := dockerCli.ContextStore().ListTLSFiles(ref)
    48  		if err != nil {
    49  			return nil, nil, err
    50  		}
    51  		return contextWithTLSListing{
    52  			Metadata:    c,
    53  			TLSMaterial: tlsListing,
    54  			Storage:     dockerCli.ContextStore().GetStorageInfo(ref),
    55  		}, nil, nil
    56  	}
    57  	return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
    58  }
    59  
    60  type contextWithTLSListing struct {
    61  	store.Metadata
    62  	TLSMaterial map[string]store.EndpointFiles
    63  	Storage     store.StorageInfo
    64  }