list.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // +build experimental
  2. package plugin
  3. import (
  4. "fmt"
  5. "strings"
  6. "text/tabwriter"
  7. "github.com/docker/docker/api/client"
  8. "github.com/docker/docker/cli"
  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 *client.DockerCli) *cobra.Command {
  17. var opts listOptions
  18. cmd := &cobra.Command{
  19. Use: "ls",
  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 *client.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, "NAME \tTAG \tDESCRIPTION\tACTIVE")
  38. fmt.Fprintf(w, "\n")
  39. for _, p := range plugins {
  40. desc := strings.Replace(p.Manifest.Description, "\n", " ", -1)
  41. desc = strings.Replace(desc, "\r", " ", -1)
  42. if !opts.noTrunc && len(desc) > 45 {
  43. desc = stringutils.Truncate(desc, 42) + "..."
  44. }
  45. fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", p.Name, p.Tag, desc, p.Active)
  46. }
  47. w.Flush()
  48. return nil
  49. }