Add --format flag for docker plugin ls

This fix tries to address the enhancement discussed in 28735 to add
`--format` for the output of `docker plugin ls`.

This fix
1. Add `--format` and `--quiet` flags to `docker plugin ls`
2. Convert the current implementation to use `formatter`, consistent with
   other docker list commands.
3. Add `pluginsFormat` for config.json.

Related docs has been updated.

Several unit tests have been added to cover the changes.

This fix is related to 28708 and 28735.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2016-11-22 16:23:21 -08:00
parent ca5bd1c106
commit c799b20f5b
4 changed files with 294 additions and 21 deletions

View File

@ -1,20 +1,17 @@
package plugin
import (
"fmt"
"strings"
"text/tabwriter"
"github.com/docker/docker/cli"
"github.com/docker/docker/cli/command"
"github.com/docker/docker/pkg/stringid"
"github.com/docker/docker/pkg/stringutils"
"github.com/docker/docker/cli/command/formatter"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type listOptions struct {
quiet bool
noTrunc bool
format string
}
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
@ -32,7 +29,9 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags()
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display plugin IDs")
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
flags.StringVar(&opts.format, "format", "", "Pretty-print plugins using a Go template")
return cmd
}
@ -43,21 +42,19 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error {
return err
}
w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
fmt.Fprintf(w, "ID \tNAME \tDESCRIPTION\tENABLED")
fmt.Fprintf(w, "\n")
for _, p := range plugins {
id := p.ID
desc := strings.Replace(p.Config.Description, "\n", " ", -1)
desc = strings.Replace(desc, "\r", " ", -1)
if !opts.noTrunc {
id = stringid.TruncateID(p.ID)
desc = stringutils.Ellipsis(desc, 45)
format := opts.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().PluginsFormat) > 0 && !opts.quiet {
format = dockerCli.ConfigFile().PluginsFormat
} else {
format = formatter.TableFormatKey
}
fmt.Fprintf(w, "%s\t%s\t%s\t%v\n", id, p.Name, desc, p.Enabled)
}
w.Flush()
return nil
pluginsCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewPluginFormat(format, opts.quiet),
Trunc: !opts.noTrunc,
}
return formatter.PluginWrite(pluginsCtx, plugins)
}