diff options
author | Jhon Honce <jhonce@redhat.com> | 2021-06-15 10:08:34 -0700 |
---|---|---|
committer | Matthew Heon <mheon@redhat.com> | 2021-06-25 09:33:06 -0400 |
commit | 5dabff27db83a0bdbea0e736050f5e3cb2c1d95e (patch) | |
tree | 401d590ad7d739a543c99486ddbe6105597cf272 /cmd/podman | |
parent | 647c2024e24abdab28480ec2f99bddb4a0a5a6d0 (diff) | |
download | podman-5dabff27db83a0bdbea0e736050f5e3cb2c1d95e.tar.gz podman-5dabff27db83a0bdbea0e736050f5e3cb2c1d95e.tar.bz2 podman-5dabff27db83a0bdbea0e736050f5e3cb2c1d95e.zip |
Scrub podman commands to use report package
Refactor podman commands that have drifted from using
c/common report pkg. Report pkg is needed to implement
go template functions.
Removed obsolete code from podman which exists in c/common.
Latest template library added default newlines and method to
remove them. Incorporated needed changes in c/common PR below.
Depends on https://github.com/containers/common/pull/624
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1855983
Signed-off-by: Jhon Honce <jhonce@redhat.com>
<MH: Fixed cherry-pick conflicts>
Signed-off-by: Matthew Heon <mheon@redhat.com>
Diffstat (limited to 'cmd/podman')
26 files changed, 176 insertions, 193 deletions
diff --git a/cmd/podman/containers/mount.go b/cmd/podman/containers/mount.go index fd5a279d2..155d600cc 100644 --- a/cmd/podman/containers/mount.go +++ b/cmd/podman/containers/mount.go @@ -3,8 +3,6 @@ package containers import ( "fmt" "os" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" @@ -118,12 +116,16 @@ func mount(_ *cobra.Command, args []string) error { mrs = append(mrs, mountReporter{r}) } - format := "{{range . }}{{.ID}}\t{{.Path}}\n{{end}}" - tmpl, err := template.New("mounts").Parse(format) + format := "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}" + tmpl, err := report.NewTemplate("mounts").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() return tmpl.Execute(w, mrs) } diff --git a/cmd/podman/containers/ps.go b/cmd/podman/containers/ps.go index 3c0162676..ad4de6897 100644 --- a/cmd/podman/containers/ps.go +++ b/cmd/podman/containers/ps.go @@ -6,15 +6,12 @@ import ( "sort" "strconv" "strings" - "text/tabwriter" - "text/template" "time" tm "github.com/buger/goterm" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/utils" "github.com/containers/podman/v3/cmd/podman/validate" @@ -228,18 +225,20 @@ func ps(cmd *cobra.Command, _ []string) error { hdrs, format := createPsOut() if cmd.Flags().Changed("format") { format = report.NormalizeFormat(listOpts.Format) - format = parse.EnforceRange(format) + format = report.EnforceRange(format) } ns := strings.NewReplacer(".Namespaces.", ".") format = ns.Replace(format) - tmpl, err := template.New("listContainers"). - Funcs(template.FuncMap(report.DefaultFuncs)). - Parse(format) + tmpl, err := report.NewTemplate("list").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() headers := func() error { return nil } @@ -322,7 +321,7 @@ func createPsOut() ([]map[string]string, string) { row += "\t{{.Size}}" } } - return hdrs, "{{range .}}" + row + "\n{{end}}" + return hdrs, "{{range .}}" + row + "\n{{end -}}" } type psReporter struct { diff --git a/cmd/podman/containers/stats.go b/cmd/podman/containers/stats.go index 7160f1ba8..37abb70f4 100644 --- a/cmd/podman/containers/stats.go +++ b/cmd/podman/containers/stats.go @@ -3,13 +3,10 @@ package containers import ( "fmt" "os" - "text/tabwriter" - "text/template" tm "github.com/buger/goterm" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/libpod/define" @@ -172,13 +169,17 @@ func outputStats(reports []define.ContainerStats) error { if len(statsOptions.Format) > 0 { format = report.NormalizeFormat(statsOptions.Format) } - format = parse.EnforceRange(format) + format = report.EnforceRange(format) - tmpl, err := template.New("stats").Parse(format) + tmpl, err := report.NewTemplate("stats").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() if len(statsOptions.Format) < 1 { diff --git a/cmd/podman/containers/top.go b/cmd/podman/containers/top.go index 03cee5d56..abb3b8455 100644 --- a/cmd/podman/containers/top.go +++ b/cmd/podman/containers/top.go @@ -5,8 +5,8 @@ import ( "fmt" "os" "strings" - "text/tabwriter" + "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" @@ -79,7 +79,7 @@ func init() { validate.AddLatestFlag(containerTopCommand, &topOptions.Latest) } -func top(cmd *cobra.Command, args []string) error { +func top(_ *cobra.Command, args []string) error { if topOptions.ListDescriptors { descriptors, err := util.GetContainerPidInformationDescriptors() if err != nil { @@ -105,7 +105,11 @@ func top(cmd *cobra.Command, args []string) error { return err } - w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0) + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } + for _, proc := range topResponse.Value { if _, err := fmt.Fprintln(w, proc); err != nil { return err diff --git a/cmd/podman/images/history.go b/cmd/podman/images/history.go index 16be0bb19..e9aa88a20 100644 --- a/cmd/podman/images/history.go +++ b/cmd/podman/images/history.go @@ -5,14 +5,11 @@ import ( "fmt" "os" "strings" - "text/tabwriter" - "text/template" "time" "unicode" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/pkg/domain/entities" "github.com/docker/go-units" @@ -127,13 +124,17 @@ func history(cmd *cobra.Command, args []string) error { case opts.quiet: row = "{{.ID}}\n" } - format := parse.EnforceRange(row) + format := report.EnforceRange(row) - tmpl, err := template.New("report").Parse(format) + tmpl, err := report.NewTemplate("history").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() if !opts.quiet && !cmd.Flags().Changed("format") { diff --git a/cmd/podman/images/list.go b/cmd/podman/images/list.go index 132af858b..e624c296b 100644 --- a/cmd/podman/images/list.go +++ b/cmd/podman/images/list.go @@ -5,8 +5,6 @@ import ( "os" "sort" "strings" - "text/tabwriter" - "text/template" "time" "unicode" @@ -14,7 +12,6 @@ import ( "github.com/containers/common/pkg/report" "github.com/containers/image/v5/docker/reference" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/pkg/domain/entities" "github.com/docker/go-units" @@ -126,7 +123,7 @@ func images(cmd *cobra.Command, args []string) error { case listFlag.quiet: return writeID(imgs) default: - if cmd.Flags().Changed("format") && !parse.HasTable(listFlag.format) { + if cmd.Flags().Changed("format") && !report.HasTable(listFlag.format) { listFlag.noHeading = true } return writeTemplate(imgs) @@ -181,20 +178,23 @@ func writeTemplate(imgs []imageReporter) error { "ReadOnly": "R/O", }) - var row string + var format string if listFlag.format == "" { - row = lsFormatFromFlags(listFlag) + format = lsFormatFromFlags(listFlag) } else { - row = report.NormalizeFormat(listFlag.format) + format = report.NormalizeFormat(listFlag.format) + format = report.EnforceRange(format) } - format := parse.EnforceRange(row) - tmpl, err := template.New("list").Parse(format) + tmpl, err := report.NewTemplate("list").Parse(format) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } defer w.Flush() if !listFlag.noHeading { @@ -323,7 +323,7 @@ func lsFormatFromFlags(flags listFlagType) string { row = append(row, "{{.ReadOnly}}") } - return strings.Join(row, "\t") + "\n" + return "{{range . }}" + strings.Join(row, "\t") + "\n{{end -}}" } type imageReporter struct { diff --git a/cmd/podman/images/mount.go b/cmd/podman/images/mount.go index a098aac63..3a38a7a19 100644 --- a/cmd/podman/images/mount.go +++ b/cmd/podman/images/mount.go @@ -3,8 +3,6 @@ package images import ( "fmt" "os" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" @@ -99,13 +97,18 @@ func mount(cmd *cobra.Command, args []string) error { mrs = append(mrs, mountReporter{r}) } - row := "{{range . }}{{.ID}}\t{{.Path}}\n{{end}}" - tmpl, err := template.New("mounts").Parse(row) + row := "{{range . }}{{.ID}}\t{{.Path}}\n{{end -}}" + tmpl, err := report.NewTemplate("mounts").Parse(row) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() + return tmpl.Execute(w, mrs) } diff --git a/cmd/podman/images/search.go b/cmd/podman/images/search.go index a3cfa983f..1bff9aed4 100644 --- a/cmd/podman/images/search.go +++ b/cmd/podman/images/search.go @@ -3,14 +3,11 @@ package images import ( "fmt" "os" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/auth" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" "github.com/containers/image/v5/types" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/pkg/domain/entities" "github.com/pkg/errors" @@ -163,18 +160,22 @@ func imageSearch(cmd *cobra.Command, args []string) error { case report.IsJSON(searchOptions.Format): return printArbitraryJSON(searchReport) case cmd.Flags().Changed("format"): - renderHeaders = parse.HasTable(searchOptions.Format) + renderHeaders = report.HasTable(searchOptions.Format) row = report.NormalizeFormat(searchOptions.Format) default: row = "{{.Index}}\t{{.Name}}\t{{.Description}}\t{{.Stars}}\t{{.Official}}\t{{.Automated}}\n" } - format := parse.EnforceRange(row) + format := report.EnforceRange(row) - tmpl, err := template.New("search").Parse(format) + tmpl, err := report.NewTemplate("search").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() if renderHeaders { diff --git a/cmd/podman/images/trust_show.go b/cmd/podman/images/trust_show.go index ed9aecdb7..44ecf52f7 100644 --- a/cmd/podman/images/trust_show.go +++ b/cmd/podman/images/trust_show.go @@ -3,9 +3,8 @@ package images import ( "fmt" "os" - "text/tabwriter" - "text/template" + "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/pkg/domain/entities" @@ -45,16 +44,16 @@ func init() { } func showTrust(cmd *cobra.Command, args []string) error { - report, err := registry.ImageEngine().ShowTrust(registry.Context(), args, showTrustOptions) + trust, err := registry.ImageEngine().ShowTrust(registry.Context(), args, showTrustOptions) if err != nil { return err } if showTrustOptions.Raw { - fmt.Println(string(report.Raw)) + fmt.Println(string(trust.Raw)) return nil } if showTrustOptions.JSON { - b, err := json.MarshalIndent(report.Policies, "", " ") + b, err := json.MarshalIndent(trust.Policies, "", " ") if err != nil { return err } @@ -62,14 +61,18 @@ func showTrust(cmd *cobra.Command, args []string) error { return nil } - row := "{{.RepoName}}\t{{.Type}}\t{{.GPGId}}\t{{.SignatureStore}}\n" - format := "{{range . }}" + row + "{{end}}" - tmpl, err := template.New("listContainers").Parse(format) + format := "{{range . }}{{.RepoName}}\t{{.Type}}\t{{.GPGId}}\t{{.SignatureStore}}\n{{end -}}" + tmpl, err := report.NewTemplate("list").Parse(format) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) - if err := tmpl.Execute(w, report.Policies); err != nil { + + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } + + if err := tmpl.Execute(w, trust.Policies); err != nil { return err } if err := w.Flush(); err != nil { diff --git a/cmd/podman/inspect/inspect.go b/cmd/podman/inspect/inspect.go index 351684af1..bd3060882 100644 --- a/cmd/podman/inspect/inspect.go +++ b/cmd/podman/inspect/inspect.go @@ -7,7 +7,6 @@ import ( "os" "regexp" "strings" - "text/tabwriter" "text/template" "github.com/containers/common/pkg/completion" @@ -217,7 +216,7 @@ func (i *inspector) inspect(namesOrIDs []string) error { err = printJSON(data) default: row := inspectNormalize(i.options.Format) - row = "{{range . }}" + report.NormalizeFormat(row) + "{{end}}" + row = "{{range . }}" + report.NormalizeFormat(row) + "{{end -}}" err = printTmpl(tmpType, row, data) } if err != nil { @@ -250,7 +249,11 @@ func printTmpl(typ, row string, data []interface{}) error { if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } return t.Execute(w, data) } diff --git a/cmd/podman/machine/list.go b/cmd/podman/machine/list.go index af4e2c807..4d52e3f61 100644 --- a/cmd/podman/machine/list.go +++ b/cmd/podman/machine/list.go @@ -5,14 +5,11 @@ package machine import ( "os" "sort" - "text/tabwriter" - "text/template" "time" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/config" "github.com/containers/common/pkg/report" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/pkg/domain/entities" @@ -95,16 +92,20 @@ func outputTemplate(cmd *cobra.Command, responses []*machineReporter) error { }) row := report.NormalizeFormat(listFlag.format) - format := parse.EnforceRange(row) + format := report.EnforceRange(row) - tmpl, err := template.New("list machines").Parse(format) + tmpl, err := report.NewTemplate("list").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0) defer w.Flush() - if cmd.Flags().Changed("format") && !parse.HasTable(listFlag.format) { + if cmd.Flags().Changed("format") && !report.HasTable(listFlag.format) { listFlag.noHeading = true } diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go index e1b182cbf..cbfd9363b 100644 --- a/cmd/podman/networks/list.go +++ b/cmd/podman/networks/list.go @@ -4,8 +4,6 @@ import ( "fmt" "os" "strings" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" @@ -134,11 +132,15 @@ func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) er } format = report.EnforceRange(row) - tmpl, err := template.New("listNetworks").Parse(format) + tmpl, err := report.NewTemplate("list").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() noHeading, _ := cmd.Flags().GetBool("noheading") diff --git a/cmd/podman/parse/template.go b/cmd/podman/parse/template.go deleted file mode 100644 index 0b80f1b3a..000000000 --- a/cmd/podman/parse/template.go +++ /dev/null @@ -1,22 +0,0 @@ -package parse - -import ( - "regexp" - "strings" -) - -var rangeRegex = regexp.MustCompile(`{{\s*range\s*\.\s*}}.*{{\s*end\s*}}`) - -// TODO move to github.com/containers/common/pkg/report -// EnforceRange ensures that the format string contains a range -func EnforceRange(format string) string { - if !rangeRegex.MatchString(format) { - return "{{range .}}" + format + "{{end}}" - } - return format -} - -// EnforceRange ensures that the format string contains a range -func HasTable(format string) bool { - return strings.HasPrefix(format, "table ") -} diff --git a/cmd/podman/parse/template_test.go b/cmd/podman/parse/template_test.go deleted file mode 100644 index 7880d9bec..000000000 --- a/cmd/podman/parse/template_test.go +++ /dev/null @@ -1,30 +0,0 @@ -package parse - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestEnforceRange(t *testing.T) { - tests := []struct { - input string - expected string - }{ - {"{{range .}}{{.ID}}{{end}}", "{{range .}}{{.ID}}{{end}}"}, - {"{{.ID}}", "{{range .}}{{.ID}}{{end}}"}, - {"{{ range . }}{{ .ID }}{{ end }}", "{{ range . }}{{ .ID }}{{ end }}"}, - // EnforceRange does not verify syntax or semantics, that will happen later - {"{{range .}}{{.ID}}", "{{range .}}{{range .}}{{.ID}}{{end}}"}, - {".ID", "{{range .}}.ID{{end}}"}, - } - - for _, tc := range tests { - tc := tc - label := "TestEnforceRange_" + tc.input - t.Run(label, func(t *testing.T) { - t.Parallel() - assert.Equal(t, tc.expected, EnforceRange(tc.input)) - }) - } -} diff --git a/cmd/podman/pods/inspect.go b/cmd/podman/pods/inspect.go index c66b81adb..597867395 100644 --- a/cmd/podman/pods/inspect.go +++ b/cmd/podman/pods/inspect.go @@ -3,8 +3,6 @@ package pods import ( "context" "os" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" @@ -74,11 +72,14 @@ func inspect(cmd *cobra.Command, args []string) error { row := report.NormalizeFormat(inspectOptions.Format) - t, err := template.New("pod inspect").Parse(row) + t, err := report.NewTemplate("inspect").Parse(row) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } return t.Execute(w, *responses) } diff --git a/cmd/podman/pods/ps.go b/cmd/podman/pods/ps.go index beaeda871..2c8e48c1c 100644 --- a/cmd/podman/pods/ps.go +++ b/cmd/podman/pods/ps.go @@ -6,14 +6,11 @@ import ( "os" "sort" "strings" - "text/tabwriter" - "text/template" "time" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/pkg/domain/entities" @@ -132,20 +129,24 @@ func pods(cmd *cobra.Command, _ []string) error { renderHeaders := true row := podPsFormat() if cmd.Flags().Changed("format") { - renderHeaders = parse.HasTable(psInput.Format) + renderHeaders = report.HasTable(psInput.Format) row = report.NormalizeFormat(psInput.Format) } noHeading, _ := cmd.Flags().GetBool("noheading") if noHeading { renderHeaders = false } - format := parse.EnforceRange(row) + format := report.EnforceRange(row) - tmpl, err := template.New("listPods").Parse(format) + tmpl, err := report.NewTemplate("list").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) defer w.Flush() if renderHeaders { diff --git a/cmd/podman/pods/stats.go b/cmd/podman/pods/stats.go index 97147275e..d2dc63ae3 100644 --- a/cmd/podman/pods/stats.go +++ b/cmd/podman/pods/stats.go @@ -4,14 +4,11 @@ import ( "context" "fmt" "os" - "text/tabwriter" - "text/template" "time" "github.com/buger/goterm" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/pkg/domain/entities" @@ -124,8 +121,12 @@ func printJSONPodStats(stats []*entities.PodStatsReport) error { return nil } -func printPodStatsLines(stats []*entities.PodStatsReport) { - w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) +func printPodStatsLines(stats []*entities.PodStatsReport) error { + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } + outFormat := "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" fmt.Fprintf(w, outFormat, "POD", "CID", "NAME", "CPU %", "MEM USAGE/ LIMIT", "MEM %", "NET IO", "BLOCK IO", "PIDS") if len(stats) == 0 { @@ -135,7 +136,7 @@ func printPodStatsLines(stats []*entities.PodStatsReport) { fmt.Fprintf(w, outFormat, i.Pod, i.CID, i.Name, i.CPU, i.MemUsage, i.Mem, i.NetIO, i.BlockIO, i.PIDS) } } - w.Flush() + return w.Flush() } func printFormattedPodStatsLines(headerNames []map[string]string, row string, stats []*entities.PodStatsReport) error { @@ -143,13 +144,17 @@ func printFormattedPodStatsLines(headerNames []map[string]string, row string, st return nil } - row = parse.EnforceRange(row) + row = report.EnforceRange(row) + + tmpl, err := report.NewTemplate("stats").Parse(row) + if err != nil { + return err + } - tmpl, err := template.New("pod stats").Parse(row) + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) defer w.Flush() if err := tmpl.Execute(w, headerNames); err != nil { diff --git a/cmd/podman/pods/top.go b/cmd/podman/pods/top.go index be8aab761..f1b7a1b53 100644 --- a/cmd/podman/pods/top.go +++ b/cmd/podman/pods/top.go @@ -5,8 +5,8 @@ import ( "fmt" "os" "strings" - "text/tabwriter" + "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" @@ -83,7 +83,11 @@ func top(_ *cobra.Command, args []string) error { return err } - w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0) + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } + for _, proc := range topResponse.Value { if _, err := fmt.Fprintln(w, proc); err != nil { return err diff --git a/cmd/podman/secrets/inspect.go b/cmd/podman/secrets/inspect.go index bcb1adb5e..7d44dc075 100644 --- a/cmd/podman/secrets/inspect.go +++ b/cmd/podman/secrets/inspect.go @@ -4,13 +4,10 @@ import ( "context" "encoding/json" "fmt" - "html/template" "os" - "text/tabwriter" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/pkg/domain/entities" "github.com/pkg/errors" @@ -53,13 +50,17 @@ func inspect(cmd *cobra.Command, args []string) error { if cmd.Flags().Changed("format") { row := report.NormalizeFormat(format) - formatted := parse.EnforceRange(row) + formatted := report.EnforceRange(row) - tmpl, err := template.New("inspect secret").Parse(formatted) + tmpl, err := report.NewTemplate("inspect").Parse(formatted) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0) defer w.Flush() tmpl.Execute(w, inspected) } else { diff --git a/cmd/podman/secrets/list.go b/cmd/podman/secrets/list.go index ba7065d61..29d364a1b 100644 --- a/cmd/podman/secrets/list.go +++ b/cmd/podman/secrets/list.go @@ -2,15 +2,12 @@ package secrets import ( "context" - "html/template" "os" - "text/tabwriter" "time" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/pkg/domain/entities" @@ -76,16 +73,20 @@ func outputTemplate(cmd *cobra.Command, responses []*entities.SecretListReport) }) row := report.NormalizeFormat(listFlag.format) - format := parse.EnforceRange(row) + format := report.EnforceRange(row) - tmpl, err := template.New("list secret").Parse(format) + tmpl, err := report.NewTemplate("list").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0) defer w.Flush() - if cmd.Flags().Changed("format") && !parse.HasTable(listFlag.format) { + if cmd.Flags().Changed("format") && !report.HasTable(listFlag.format) { listFlag.noHeading = true } diff --git a/cmd/podman/system/connection/list.go b/cmd/podman/system/connection/list.go index fe7026ae3..15f007920 100644 --- a/cmd/podman/system/connection/list.go +++ b/cmd/podman/system/connection/list.go @@ -2,11 +2,10 @@ package connection import ( "os" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/config" + "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/system" "github.com/containers/podman/v3/cmd/podman/validate" @@ -76,13 +75,16 @@ func list(_ *cobra.Command, _ []string) error { } // TODO: Allow user to override format - format := "{{range . }}{{.Name}}\t{{.Identity}}\t{{.URI}}\n{{end}}" - tmpl, err := template.New("connection").Parse(format) + format := "{{range . }}{{.Name}}\t{{.Identity}}\t{{.URI}}\n{{end -}}" + tmpl, err := report.NewTemplate("list").Parse(format) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } defer w.Flush() _ = tmpl.Execute(w, hdrs) diff --git a/cmd/podman/system/df.go b/cmd/podman/system/df.go index 5e179a82d..f025bcc27 100644 --- a/cmd/podman/system/df.go +++ b/cmd/podman/system/df.go @@ -4,13 +4,10 @@ import ( "fmt" "os" "strings" - "text/tabwriter" - "text/template" "time" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/pkg/domain/entities" @@ -58,7 +55,10 @@ func df(cmd *cobra.Command, args []string) error { return err } - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } if dfOptions.Verbose { return printVerbose(w, cmd, reports) @@ -66,7 +66,7 @@ func df(cmd *cobra.Command, args []string) error { return printSummary(w, cmd, reports) } -func printSummary(w *tabwriter.Writer, cmd *cobra.Command, reports *entities.SystemDfReport) error { +func printSummary(w *report.Writer, cmd *cobra.Command, reports *entities.SystemDfReport) error { var ( dfSummaries []*dfSummary active int @@ -144,7 +144,7 @@ func printSummary(w *tabwriter.Writer, cmd *cobra.Command, reports *entities.Sys return writeTemplate(w, cmd, hdrs, row, dfSummaries) } -func printVerbose(w *tabwriter.Writer, cmd *cobra.Command, reports *entities.SystemDfReport) error { +func printVerbose(w *report.Writer, cmd *cobra.Command, reports *entities.SystemDfReport) error { defer w.Flush() fmt.Fprint(w, "Images space usage:\n\n") @@ -192,11 +192,11 @@ func printVerbose(w *tabwriter.Writer, cmd *cobra.Command, reports *entities.Sys return writeTemplate(w, cmd, hdrs, volumeRow, dfVolumes) } -func writeTemplate(w *tabwriter.Writer, cmd *cobra.Command, hdrs []map[string]string, format string, output interface{}) error { +func writeTemplate(w *report.Writer, cmd *cobra.Command, hdrs []map[string]string, format string, output interface{}) error { defer w.Flush() - format = parse.EnforceRange(format) - tmpl, err := template.New("df").Parse(format) + format = report.EnforceRange(format) + tmpl, err := report.NewTemplate("df").Parse(format) if err != nil { return err } diff --git a/cmd/podman/system/events.go b/cmd/podman/system/events.go index 568610bdc..c014c7fa2 100644 --- a/cmd/podman/system/events.go +++ b/cmd/podman/system/events.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "text/template" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" @@ -76,7 +75,7 @@ func eventsCmd(cmd *cobra.Command, _ []string) error { errChannel := make(chan error) var ( - tmpl *template.Template + tmpl *report.Template doJSON bool ) @@ -84,7 +83,7 @@ func eventsCmd(cmd *cobra.Command, _ []string) error { doJSON = report.IsJSON(eventFormat) if !doJSON { var err error - tmpl, err = template.New("events").Parse(eventFormat) + tmpl, err = report.NewTemplate("events").Parse(eventFormat) if err != nil { return err } diff --git a/cmd/podman/system/info.go b/cmd/podman/system/info.go index 44be4ccec..5917f1aae 100644 --- a/cmd/podman/system/info.go +++ b/cmd/podman/system/info.go @@ -3,7 +3,6 @@ package system import ( "fmt" "os" - "text/template" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" @@ -88,7 +87,7 @@ func info(cmd *cobra.Command, args []string) error { } fmt.Println(string(b)) case cmd.Flags().Changed("format"): - tmpl, err := template.New("info").Parse(inFormat) + tmpl, err := report.NewTemplate("info").Parse(inFormat) if err != nil { return err } diff --git a/cmd/podman/system/version.go b/cmd/podman/system/version.go index ad9fd2a85..a83539589 100644 --- a/cmd/podman/system/version.go +++ b/cmd/podman/system/version.go @@ -5,8 +5,6 @@ import ( "io" "os" "strings" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" @@ -56,19 +54,22 @@ func version(cmd *cobra.Command, args []string) error { return nil } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) + w, err := report.NewWriterDefault(os.Stdout) + if err != nil { + return err + } defer w.Flush() if cmd.Flag("format").Changed { row := report.NormalizeFormat(versionFormat) - tmpl, err := template.New("version 2.0.0").Parse(row) + tmpl, err := report.NewTemplate("version 2.0.0").Parse(row) if err != nil { return err } if err := tmpl.Execute(w, versions); err != nil { // On Failure, assume user is using older version of podman version --format and check client row = strings.Replace(row, ".Server.", ".", 1) - tmpl, err := template.New("version 1.0.0").Parse(row) + tmpl, err := report.NewTemplate("version 1.0.0").Parse(row) if err != nil { return err } diff --git a/cmd/podman/volumes/list.go b/cmd/podman/volumes/list.go index f402afa94..621ea6089 100644 --- a/cmd/podman/volumes/list.go +++ b/cmd/podman/volumes/list.go @@ -5,13 +5,10 @@ import ( "fmt" "os" "strings" - "text/tabwriter" - "text/template" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/libpod/define" @@ -105,13 +102,17 @@ func outputTemplate(cmd *cobra.Command, responses []*entities.VolumeListReport) if cliOpts.Quiet { row = "{{.Name}}\n" } - format := parse.EnforceRange(row) + format := report.EnforceRange(row) - tmpl, err := template.New("list volume").Parse(format) + tmpl, err := report.NewTemplate("list").Parse(format) + if err != nil { + return err + } + + w, err := report.NewWriterDefault(os.Stdout) if err != nil { return err } - w := tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0) defer w.Flush() if !(noHeading || cliOpts.Quiet || cmd.Flag("format").Changed) { |