github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/config/inspect.go (about)

     1  package config
     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  // InspectOptions contains options for the docker config inspect command.
    15  type InspectOptions struct {
    16  	Names  []string
    17  	Format string
    18  	Pretty bool
    19  }
    20  
    21  func newConfigInspectCommand(dockerCli command.Cli) *cobra.Command {
    22  	opts := InspectOptions{}
    23  	cmd := &cobra.Command{
    24  		Use:   "inspect [OPTIONS] CONFIG [CONFIG...]",
    25  		Short: "Display detailed information on one or more configs",
    26  		Args:  cli.RequiresMinArgs(1),
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			opts.Names = args
    29  			return RunConfigInspect(dockerCli, opts)
    30  		},
    31  	}
    32  
    33  	cmd.Flags().StringVarP(&opts.Format, "format", "f", "", "Format the output using the given Go template")
    34  	cmd.Flags().BoolVar(&opts.Pretty, "pretty", false, "Print the information in a human friendly format")
    35  	return cmd
    36  }
    37  
    38  // RunConfigInspect inspects the given Swarm config.
    39  func RunConfigInspect(dockerCli command.Cli, opts InspectOptions) error {
    40  	client := dockerCli.Client()
    41  	ctx := context.Background()
    42  
    43  	if opts.Pretty {
    44  		opts.Format = "pretty"
    45  	}
    46  
    47  	getRef := func(id string) (interface{}, []byte, error) {
    48  		return client.ConfigInspectWithRaw(ctx, id)
    49  	}
    50  	f := opts.Format
    51  
    52  	// check if the user is trying to apply a template to the pretty format, which
    53  	// is not supported
    54  	if strings.HasPrefix(f, "pretty") && f != "pretty" {
    55  		return fmt.Errorf("Cannot supply extra formatting options to the pretty template")
    56  	}
    57  
    58  	configCtx := formatter.Context{
    59  		Output: dockerCli.Out(),
    60  		Format: NewFormat(f, false),
    61  	}
    62  
    63  	if err := InspectFormatWrite(configCtx, opts.Names, getRef); err != nil {
    64  		return cli.StatusError{StatusCode: 1, Status: err.Error()}
    65  	}
    66  	return nil
    67  
    68  }