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