github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/service/inspect.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/docker/cli/cli"
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/cli/cli/command/formatter"
    10  	"github.com/docker/docker/api/types"
    11  	apiclient "github.com/docker/docker/client"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  type inspectOptions struct {
    17  	refs   []string
    18  	format string
    19  	pretty bool
    20  }
    21  
    22  func newInspectCommand(dockerCli command.Cli) *cobra.Command {
    23  	var opts inspectOptions
    24  
    25  	cmd := &cobra.Command{
    26  		Use:   "inspect [OPTIONS] SERVICE [SERVICE...]",
    27  		Short: "Display detailed information on one or more services",
    28  		Args:  cli.RequiresMinArgs(1),
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			opts.refs = args
    31  
    32  			if opts.pretty && len(opts.format) > 0 {
    33  				return errors.Errorf("--format is incompatible with human friendly format")
    34  			}
    35  			return runInspect(dockerCli, opts)
    36  		},
    37  	}
    38  
    39  	flags := cmd.Flags()
    40  	flags.StringVarP(&opts.format, "format", "f", "", "Format the output using the given Go template")
    41  	flags.BoolVar(&opts.pretty, "pretty", false, "Print the information in a human friendly format")
    42  	return cmd
    43  }
    44  
    45  func runInspect(dockerCli command.Cli, opts inspectOptions) error {
    46  	client := dockerCli.Client()
    47  	ctx := context.Background()
    48  
    49  	if opts.pretty {
    50  		opts.format = "pretty"
    51  	}
    52  
    53  	getRef := func(ref string) (interface{}, []byte, error) {
    54  		// Service inspect shows defaults values in empty fields.
    55  		service, _, err := client.ServiceInspectWithRaw(ctx, ref, types.ServiceInspectOptions{InsertDefaults: true})
    56  		if err == nil || !apiclient.IsErrNotFound(err) {
    57  			return service, nil, err
    58  		}
    59  		return nil, nil, errors.Errorf("Error: no such service: %s", ref)
    60  	}
    61  
    62  	getNetwork := func(ref string) (interface{}, []byte, error) {
    63  		network, _, err := client.NetworkInspectWithRaw(ctx, ref, types.NetworkInspectOptions{Scope: "swarm"})
    64  		if err == nil || !apiclient.IsErrNotFound(err) {
    65  			return network, nil, err
    66  		}
    67  		return nil, nil, errors.Errorf("Error: no such network: %s", ref)
    68  	}
    69  
    70  	f := opts.format
    71  	if len(f) == 0 {
    72  		f = "raw"
    73  		if len(dockerCli.ConfigFile().ServiceInspectFormat) > 0 {
    74  			f = dockerCli.ConfigFile().ServiceInspectFormat
    75  		}
    76  	}
    77  
    78  	// check if the user is trying to apply a template to the pretty format, which
    79  	// is not supported
    80  	if strings.HasPrefix(f, "pretty") && f != "pretty" {
    81  		return errors.Errorf("Cannot supply extra formatting options to the pretty template")
    82  	}
    83  
    84  	serviceCtx := formatter.Context{
    85  		Output: dockerCli.Out(),
    86  		Format: NewFormat(f),
    87  	}
    88  
    89  	if err := InspectFormatWrite(serviceCtx, opts.refs, getRef, getNetwork); err != nil {
    90  		return cli.StatusError{StatusCode: 1, Status: err.Error()}
    91  	}
    92  	return nil
    93  }