list.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package plugin
  2. import (
  3. "github.com/docker/docker/cli"
  4. "github.com/docker/docker/cli/command"
  5. "github.com/docker/docker/cli/command/formatter"
  6. "github.com/docker/docker/opts"
  7. "github.com/spf13/cobra"
  8. "golang.org/x/net/context"
  9. )
  10. type listOptions struct {
  11. quiet bool
  12. noTrunc bool
  13. format string
  14. filter opts.FilterOpt
  15. }
  16. func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
  17. opts := listOptions{filter: opts.NewFilterOpt()}
  18. cmd := &cobra.Command{
  19. Use: "ls [OPTIONS]",
  20. Short: "List plugins",
  21. Aliases: []string{"list"},
  22. Args: cli.NoArgs,
  23. RunE: func(cmd *cobra.Command, args []string) error {
  24. return runList(dockerCli, opts)
  25. },
  26. }
  27. flags := cmd.Flags()
  28. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display plugin IDs")
  29. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
  30. flags.StringVar(&opts.format, "format", "", "Pretty-print plugins using a Go template")
  31. flags.VarP(&opts.filter, "filter", "f", "Provide filter values (e.g. 'enabled=true')")
  32. return cmd
  33. }
  34. func runList(dockerCli *command.DockerCli, opts listOptions) error {
  35. plugins, err := dockerCli.Client().PluginList(context.Background(), opts.filter.Value())
  36. if err != nil {
  37. return err
  38. }
  39. format := opts.format
  40. if len(format) == 0 {
  41. if len(dockerCli.ConfigFile().PluginsFormat) > 0 && !opts.quiet {
  42. format = dockerCli.ConfigFile().PluginsFormat
  43. } else {
  44. format = formatter.TableFormatKey
  45. }
  46. }
  47. pluginsCtx := formatter.Context{
  48. Output: dockerCli.Out(),
  49. Format: formatter.NewPluginFormat(format, opts.quiet),
  50. Trunc: !opts.noTrunc,
  51. }
  52. return formatter.PluginWrite(pluginsCtx, plugins)
  53. }