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