Add format to secret ls

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>
Upstream-commit: 1bac314da5
Component: cli
This commit is contained in:
Boaz Shuster
2017-03-05 13:11:04 +02:00
parent 4009dade43
commit bbac6dc261
4 changed files with 180 additions and 25 deletions

View File

@ -1,20 +1,17 @@
package secret
import (
"fmt"
"text/tabwriter"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/cli"
"github.com/docker/docker/cli/command"
"github.com/docker/go-units"
"github.com/docker/docker/cli/command/formatter"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type listOptions struct {
quiet bool
quiet bool
format string
}
func newSecretListCommand(dockerCli *command.DockerCli) *cobra.Command {
@ -32,6 +29,7 @@ func newSecretListCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags()
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
flags.StringVarP(&opts.format, "format", "", "", "Pretty-print secrets using a Go template")
return cmd
}
@ -44,25 +42,17 @@ func runSecretList(dockerCli *command.DockerCli, opts listOptions) error {
if err != nil {
return err
}
w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
if opts.quiet {
for _, s := range secrets {
fmt.Fprintf(w, "%s\n", s.ID)
}
} else {
fmt.Fprintf(w, "ID\tNAME\tCREATED\tUPDATED")
fmt.Fprintf(w, "\n")
for _, s := range secrets {
created := units.HumanDuration(time.Now().UTC().Sub(s.Meta.CreatedAt)) + " ago"
updated := units.HumanDuration(time.Now().UTC().Sub(s.Meta.UpdatedAt)) + " ago"
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", s.ID, s.Spec.Annotations.Name, created, updated)
format := opts.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().SecretFormat) > 0 && !opts.quiet {
format = dockerCli.ConfigFile().SecretFormat
} else {
format = formatter.TableFormatKey
}
}
w.Flush()
return nil
secretCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewSecretFormat(format, opts.quiet),
}
return formatter.SecretWrite(secretCtx, secrets)
}