list.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/stringid"
  9. "github.com/docker/docker/pkg/stringutils"
  10. "github.com/spf13/cobra"
  11. "golang.org/x/net/context"
  12. )
  13. type listOptions struct {
  14. noTrunc bool
  15. }
  16. func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
  17. var opts listOptions
  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.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
  29. return cmd
  30. }
  31. func runList(dockerCli *command.DockerCli, opts listOptions) error {
  32. plugins, err := dockerCli.Client().PluginList(context.Background())
  33. if err != nil {
  34. return err
  35. }
  36. w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
  37. fmt.Fprintf(w, "ID \tNAME \tTAG \tDESCRIPTION\tENABLED")
  38. fmt.Fprintf(w, "\n")
  39. for _, p := range plugins {
  40. id := p.ID
  41. desc := strings.Replace(p.Config.Description, "\n", " ", -1)
  42. desc = strings.Replace(desc, "\r", " ", -1)
  43. if !opts.noTrunc {
  44. id = stringid.TruncateID(p.ID)
  45. desc = stringutils.Ellipsis(desc, 45)
  46. }
  47. fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%v\n", id, p.Name, p.Tag, desc, p.Enabled)
  48. }
  49. w.Flush()
  50. return nil
  51. }