github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/config/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 config
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"strings"
    10  
    11  	"github.com/docker/cli/cli"
    12  	"github.com/docker/cli/cli/command"
    13  	"github.com/docker/cli/cli/command/formatter"
    14  	flagsHelper "github.com/docker/cli/cli/flags"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  // InspectOptions contains options for the docker config inspect command.
    19  type InspectOptions struct {
    20  	Names  []string
    21  	Format string
    22  	Pretty bool
    23  }
    24  
    25  func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
    26  	opts := InspectOptions{}
    27  	cmd := &cobra.Command{
    28  		Use:   "inspect [OPTIONS] CONFIG [CONFIG...]",
    29  		Short: "Display detailed information on one or more configs",
    30  		Args:  cli.RequiresMinArgs(1),
    31  		RunE: func(cmd *cobra.Command, args []string) error {
    32  			opts.Names = args
    33  			return RunConfigInspect(cmd.Context(), dockerCli, opts)
    34  		},
    35  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    36  			return completeNames(dockerCli)(cmd, args, toComplete)
    37  		},
    38  	}
    39  
    40  	cmd.Flags().StringVarP(&opts.Format, "format", "f", "", flagsHelper.InspectFormatHelp)
    41  	cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format")
    42  	return cmd
    43  }
    44  
    45  // RunConfigInspect inspects the given Swarm config.
    46  func RunConfigInspect(ctx context.Context, dockerCli command.Cli, opts InspectOptions) error {
    47  	client := dockerCli.Client()
    48  
    49  	if opts.Pretty {
    50  		opts.Format = "pretty"
    51  	}
    52  
    53  	getRef := func(id string) (any, []byte, error) {
    54  		return client.ConfigInspectWithRaw(ctx, id)
    55  	}
    56  	f := opts.Format
    57  
    58  	// check if the user is trying to apply a template to the pretty format, which
    59  	// is not supported
    60  	if strings.HasPrefix(f, "pretty") && f != "pretty" {
    61  		return errors.New("cannot supply extra formatting options to the pretty template")
    62  	}
    63  
    64  	configCtx := formatter.Context{
    65  		Output: dockerCli.Out(),
    66  		Format: NewFormat(f, false),
    67  	}
    68  
    69  	if err := InspectFormatWrite(configCtx, opts.Names, getRef); err != nil {
    70  		return cli.StatusError{StatusCode: 1, Status: err.Error()}
    71  	}
    72  	return nil
    73  }