github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/context/inspect.go (about)

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