github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/plugin/list.go (about)

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