github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/plugin/list.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"sort"
     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/cli/opts"
    11  	"github.com/spf13/cobra"
    12  	"vbom.ml/util/sortorder"
    13  )
    14  
    15  type listOptions struct {
    16  	quiet   bool
    17  	noTrunc bool
    18  	format  string
    19  	filter  opts.FilterOpt
    20  }
    21  
    22  func newListCommand(dockerCli command.Cli) *cobra.Command {
    23  	options := listOptions{filter: opts.NewFilterOpt()}
    24  
    25  	cmd := &cobra.Command{
    26  		Use:     "ls [OPTIONS]",
    27  		Short:   "List plugins",
    28  		Aliases: []string{"list"},
    29  		Args:    cli.NoArgs,
    30  		RunE: func(cmd *cobra.Command, args []string) error {
    31  			return runList(dockerCli, options)
    32  		},
    33  	}
    34  
    35  	flags := cmd.Flags()
    36  
    37  	flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display plugin IDs")
    38  	flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
    39  	flags.StringVar(&options.format, "format", "", "Pretty-print plugins using a Go template")
    40  	flags.VarP(&options.filter, "filter", "f", "Provide filter values (e.g. 'enabled=true')")
    41  
    42  	return cmd
    43  }
    44  
    45  func runList(dockerCli command.Cli, options listOptions) error {
    46  	plugins, err := dockerCli.Client().PluginList(context.Background(), options.filter.Value())
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	sort.Slice(plugins, func(i, j int) bool {
    52  		return sortorder.NaturalLess(plugins[i].Name, plugins[j].Name)
    53  	})
    54  
    55  	format := options.format
    56  	if len(format) == 0 {
    57  		if len(dockerCli.ConfigFile().PluginsFormat) > 0 && !options.quiet {
    58  			format = dockerCli.ConfigFile().PluginsFormat
    59  		} else {
    60  			format = formatter.TableFormatKey
    61  		}
    62  	}
    63  
    64  	pluginsCtx := formatter.Context{
    65  		Output: dockerCli.Out(),
    66  		Format: NewFormat(format, options.quiet),
    67  		Trunc:  !options.noTrunc,
    68  	}
    69  	return FormatWrite(pluginsCtx, plugins)
    70  }