github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/secret/inspect.go (about)

     1  package secret
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/docker/cli/cli"
     9  	"github.com/docker/cli/cli/command"
    10  	"github.com/docker/cli/cli/command/formatter"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type inspectOptions struct {
    15  	names  []string
    16  	format string
    17  	pretty bool
    18  }
    19  
    20  func newSecretInspectCommand(dockerCli command.Cli) *cobra.Command {
    21  	opts := inspectOptions{}
    22  	cmd := &cobra.Command{
    23  		Use:   "inspect [OPTIONS] SECRET [SECRET...]",
    24  		Short: "Display detailed information on one or more secrets",
    25  		Args:  cli.RequiresMinArgs(1),
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			opts.names = args
    28  			return runSecretInspect(dockerCli, opts)
    29  		},
    30  	}
    31  
    32  	cmd.Flags().StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
    33  	cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
    34  	return cmd
    35  }
    36  
    37  func runSecretInspect(dockerCli command.Cli, opts inspectOptions) error {
    38  	client := dockerCli.Client()
    39  	ctx := context.Background()
    40  
    41  	if opts.pretty {
    42  		opts.format = "pretty"
    43  	}
    44  
    45  	getRef := func(id string) (interface{}, []byte, error) {
    46  		return client.SecretInspectWithRaw(ctx, id)
    47  	}
    48  	f := opts.format
    49  
    50  	// check if the user is trying to apply a template to the pretty format, which
    51  	// is not supported
    52  	if strings.HasPrefix(f, "pretty") && f != "pretty" {
    53  		return fmt.Errorf("Cannot supply extra formatting options to the pretty template")
    54  	}
    55  
    56  	secretCtx := formatter.Context{
    57  		Output: dockerCli.Out(),
    58  		Format: NewFormat(f, false),
    59  	}
    60  
    61  	if err := InspectFormatWrite(secretCtx, opts.names, getRef); err != nil {
    62  		return cli.StatusError{StatusCode: 1, Status: err.Error()}
    63  	}
    64  	return nil
    65  }