2016-09-08 13:11:39 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2018-06-29 12:15:43 -04:00
|
|
|
"sort"
|
2018-05-03 21:02:44 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2020-08-28 08:35:09 -04:00
|
|
|
"github.com/fvbommel/sortorder"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
2016-11-22 19:23:21 -05:00
|
|
|
quiet bool
|
2016-09-08 13:11:39 -04:00
|
|
|
noTrunc bool
|
2016-11-22 19:23:21 -05:00
|
|
|
format string
|
2016-11-23 07:58:15 -05:00
|
|
|
filter opts.FilterOpt
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := listOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Short: "List plugins",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runList(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display plugin IDs")
|
|
|
|
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
|
|
|
|
flags.StringVar(&options.format, "format", "", "Pretty-print plugins using a Go template")
|
|
|
|
flags.VarP(&options.filter, "filter", "f", "Provide filter values (e.g. 'enabled=true')")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func runList(dockerCli command.Cli, options listOptions) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
plugins, err := dockerCli.Client().PluginList(context.Background(), options.filter.Value())
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-29 12:15:43 -04:00
|
|
|
sort.Slice(plugins, func(i, j int) bool {
|
|
|
|
return sortorder.NaturalLess(plugins[i].Name, plugins[j].Name)
|
|
|
|
})
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
format := options.format
|
2016-11-22 19:23:21 -05:00
|
|
|
if len(format) == 0 {
|
2017-05-15 08:45:19 -04:00
|
|
|
if len(dockerCli.ConfigFile().PluginsFormat) > 0 && !options.quiet {
|
2016-11-22 19:23:21 -05:00
|
|
|
format = dockerCli.ConfigFile().PluginsFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-11-22 19:23:21 -05:00
|
|
|
pluginsCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2018-10-23 11:05:44 -04:00
|
|
|
Format: NewFormat(format, options.quiet),
|
2017-05-15 08:45:19 -04:00
|
|
|
Trunc: !options.noTrunc,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2018-10-23 11:05:44 -04:00
|
|
|
return FormatWrite(pluginsCtx, plugins)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|