list.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package plugin
  2. import (
  3. "fmt"
  4. "strings"
  5. "text/tabwriter"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/cli/command"
  8. "github.com/docker/docker/pkg/stringutils"
  9. "github.com/spf13/cobra"
  10. "golang.org/x/net/context"
  11. )
  12. type listOptions struct {
  13. noTrunc bool
  14. }
  15. func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
  16. var opts listOptions
  17. cmd := &cobra.Command{
  18. Use: "ls [OPTIONS]",
  19. Short: "List plugins",
  20. Aliases: []string{"list"},
  21. Args: cli.NoArgs,
  22. RunE: func(cmd *cobra.Command, args []string) error {
  23. return runList(dockerCli, opts)
  24. },
  25. }
  26. flags := cmd.Flags()
  27. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
  28. return cmd
  29. }
  30. func runList(dockerCli *command.DockerCli, opts listOptions) error {
  31. plugins, err := dockerCli.Client().PluginList(context.Background())
  32. if err != nil {
  33. return err
  34. }
  35. w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
  36. fmt.Fprintf(w, "NAME \tTAG \tDESCRIPTION\tENABLED")
  37. fmt.Fprintf(w, "\n")
  38. for _, p := range plugins {
  39. desc := strings.Replace(p.Config.Description, "\n", " ", -1)
  40. desc = strings.Replace(desc, "\r", " ", -1)
  41. if !opts.noTrunc {
  42. desc = stringutils.Ellipsis(desc, 45)
  43. }
  44. fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", p.Name, p.Tag, desc, p.Enabled)
  45. }
  46. w.Flush()
  47. return nil
  48. }