github.com/kim0/docker@v0.6.2-0.20161130212042-4addda3f07e7/cli/command/plugin/list.go (about)

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