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