diff options
author | Jhon Honce <jhonce@redhat.com> | 2020-10-08 13:15:09 -0700 |
---|---|---|
committer | Jhon Honce <jhonce@redhat.com> | 2020-10-08 14:55:58 -0700 |
commit | e9b667bb5f9c72fd9903bfa5efd081ad24a2482b (patch) | |
tree | dc4712e090d1b7a3429a251ad155e01de388da42 /cmd/podman/volumes/list.go | |
parent | 0afbe2d15203447370b495c2ba2c960059cf7df4 (diff) | |
download | podman-e9b667bb5f9c72fd9903bfa5efd081ad24a2482b.tar.gz podman-e9b667bb5f9c72fd9903bfa5efd081ad24a2482b.tar.bz2 podman-e9b667bb5f9c72fd9903bfa5efd081ad24a2482b.zip |
Port V1 --format table to V2 podman
* volume ls
* container ps
* updated broken tests when skip removed
Signed-off-by: Jhon Honce <jhonce@redhat.com>
Diffstat (limited to 'cmd/podman/volumes/list.go')
-rw-r--r-- | cmd/podman/volumes/list.go | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/cmd/podman/volumes/list.go b/cmd/podman/volumes/list.go index d198e51a7..18765a499 100644 --- a/cmd/podman/volumes/list.go +++ b/cmd/podman/volumes/list.go @@ -3,13 +3,14 @@ package volumes import ( "context" "fmt" - "io" "os" "strings" "text/tabwriter" "text/template" + "github.com/containers/podman/v2/cmd/podman/parse" "github.com/containers/podman/v2/cmd/podman/registry" + "github.com/containers/podman/v2/cmd/podman/report" "github.com/containers/podman/v2/cmd/podman/validate" "github.com/containers/podman/v2/pkg/domain/entities" "github.com/pkg/errors" @@ -55,7 +56,6 @@ func init() { } func list(cmd *cobra.Command, args []string) error { - var w io.Writer = os.Stdout if cliOpts.Quiet && cmd.Flag("format").Changed { return errors.New("quiet and format flags cannot be used together") } @@ -73,40 +73,40 @@ func list(cmd *cobra.Command, args []string) error { if err != nil { return err } - if cliOpts.Format == "json" { - return outputJSON(responses) - } - if len(responses) < 1 { + switch { + case parse.MatchesJSONFormat(cliOpts.Format): + return outputJSON(responses) + case len(responses) < 1: return nil } - // "\t" from the command line is not being recognized as a tab - // replacing the string "\t" to a tab character if the user passes in "\t" - cliOpts.Format = strings.Replace(cliOpts.Format, `\t`, "\t", -1) + return outputTemplate(cmd, responses) +} + +func outputTemplate(cmd *cobra.Command, responses []*entities.VolumeListReport) error { + headers := report.Headers(entities.VolumeListReport{}, map[string]string{ + "Name": "VOLUME NAME", + }) + + row := report.NormalizeFormat(cliOpts.Format) if cliOpts.Quiet { - cliOpts.Format = "{{.Name}}\n" + row = "{{.Name}}\n" } - headers := "DRIVER\tVOLUME NAME\n" - row := cliOpts.Format - if !strings.HasSuffix(cliOpts.Format, "\n") { - row += "\n" - } - format := "{{range . }}" + row + "{{end}}" - if !cliOpts.Quiet && !cmd.Flag("format").Changed { - w = tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0) - format = headers + format - } - tmpl, err := template.New("listVolume").Parse(format) + row = "{{range . }}" + row + "{{end}}" + + tmpl, err := template.New("list volume").Parse(row) if err != nil { return err } - if err := tmpl.Execute(w, responses); err != nil { - return err - } - if flusher, ok := w.(interface{ Flush() error }); ok { - return flusher.Flush() + w := tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0) + defer w.Flush() + + if !cliOpts.Quiet && !cmd.Flag("format").Changed { + if err := tmpl.Execute(w, headers); err != nil { + return errors.Wrapf(err, "failed to write report column headers") + } } - return nil + return tmpl.Execute(w, responses) } func outputJSON(vols []*entities.VolumeListReport) error { |