summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-10-14 13:07:11 -0400
committerGitHub <noreply@github.com>2020-10-14 13:07:11 -0400
commite3eb6fd0e4115162e10caf6ae2196fd8774657e0 (patch)
tree9c75804b98eefa34a2cc924c8ccb2b8fc9c40d01 /cmd
parentd30b4b7aa5076c3192faada7d408f039a40414eb (diff)
parenteb4a746efcb9e76e29942461b97da797fd67109f (diff)
downloadpodman-e3eb6fd0e4115162e10caf6ae2196fd8774657e0.tar.gz
podman-e3eb6fd0e4115162e10caf6ae2196fd8774657e0.tar.bz2
podman-e3eb6fd0e4115162e10caf6ae2196fd8774657e0.zip
Merge pull request #7987 from jwhonce/jira/run-898-5
Restore --format table support
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podman/containers/ps.go3
-rw-r--r--cmd/podman/parse/json.go2
-rw-r--r--cmd/podman/parse/json_test.go2
-rw-r--r--cmd/podman/system/df.go111
-rw-r--r--cmd/podman/system/events.go74
-rw-r--r--cmd/podman/system/info.go21
-rw-r--r--cmd/podman/system/version.go50
-rw-r--r--cmd/podman/volumes/inspect.go21
8 files changed, 138 insertions, 146 deletions
diff --git a/cmd/podman/containers/ps.go b/cmd/podman/containers/ps.go
index 8082a74c2..65bfc97da 100644
--- a/cmd/podman/containers/ps.go
+++ b/cmd/podman/containers/ps.go
@@ -11,7 +11,6 @@ import (
"time"
tm "github.com/buger/goterm"
- "github.com/containers/buildah/pkg/formats"
"github.com/containers/podman/v2/cmd/podman/parse"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/cmd/podman/report"
@@ -93,7 +92,7 @@ func checkFlags(c *cobra.Command) error {
if listOpts.Size || listOpts.Namespace {
return errors.Errorf("quiet conflicts with size and namespace")
}
- if c.Flag("format").Changed && listOpts.Format != formats.JSONString {
+ if c.Flag("format").Changed && !parse.MatchesJSONFormat(listOpts.Format) {
// Quiet is overridden by Go template output.
listOpts.Quiet = false
}
diff --git a/cmd/podman/parse/json.go b/cmd/podman/parse/json.go
index 40ac415db..d7486d0b1 100644
--- a/cmd/podman/parse/json.go
+++ b/cmd/podman/parse/json.go
@@ -2,7 +2,7 @@ package parse
import "regexp"
-var jsonFormatRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*( \.)?\s*}})\s*$`)
+var jsonFormatRegex = regexp.MustCompile(`^\s*(json|{{\s*json\s*(\.)?\s*}})\s*$`)
// MatchesJSONFormat test CLI --format string to be a JSON request
func MatchesJSONFormat(s string) bool {
diff --git a/cmd/podman/parse/json_test.go b/cmd/podman/parse/json_test.go
index ec3b5664b..b3b29c93c 100644
--- a/cmd/podman/parse/json_test.go
+++ b/cmd/podman/parse/json_test.go
@@ -27,7 +27,7 @@ func TestMatchesJSONFormat(t *testing.T) {
{"json . }}", false},
{"{{.ID }} json .", false},
{"json .", false},
- {"{{json.}}", false},
+ {"{{json.}}", true},
}
for _, tt := range tests {
diff --git a/cmd/podman/system/df.go b/cmd/podman/system/df.go
index b262c8478..da7bbed02 100644
--- a/cmd/podman/system/df.go
+++ b/cmd/podman/system/df.go
@@ -2,15 +2,14 @@ package system
import (
"fmt"
- "io"
"os"
- "strconv"
"strings"
"text/tabwriter"
"text/template"
"time"
"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/docker/go-units"
@@ -52,35 +51,21 @@ func df(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
+
+ w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
+
if dfOptions.Verbose {
- return printVerbose(reports)
+ return printVerbose(cmd, w, reports)
}
- return printSummary(reports, dfOptions.Format)
+ return printSummary(w, cmd, reports)
}
-func printSummary(reports *entities.SystemDfReport, userFormat string) error {
-
+func printSummary(w *tabwriter.Writer, cmd *cobra.Command, reports *entities.SystemDfReport) error {
var (
dfSummaries []*dfSummary
active int
size, reclaimable int64
- format = "{{.Type}}\t{{.Total}}\t{{.Active}}\t{{.Size}}\t{{.Reclaimable}}\n"
- w io.Writer = os.Stdout
)
-
- // Images
- if len(userFormat) > 0 {
- if !strings.HasSuffix(userFormat, `\n`) {
- userFormat += `\n`
- }
- // should be Unquoto from cmd line
- userFormat, err := strconv.Unquote(`"` + userFormat + `"`)
- if err != nil {
- return err
- }
- format = userFormat
- }
-
for _, i := range reports.Images {
if i.Containers > 0 {
active++
@@ -90,7 +75,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error {
reclaimable += i.Size
}
}
-
imageSummary := dfSummary{
Type: "Images",
Total: len(reports.Images),
@@ -101,7 +85,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error {
dfSummaries = append(dfSummaries, &imageSummary)
// Containers
-
var (
conActive int
conSize, conReclaimable int64
@@ -114,7 +97,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error {
}
conSize += c.RWSize
}
-
containerSummary := dfSummary{
Type: "Containers",
Total: len(reports.Containers),
@@ -122,7 +104,6 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error {
size: conSize,
reclaimable: conReclaimable,
}
-
dfSummaries = append(dfSummaries, &containerSummary)
// Volumes
@@ -143,78 +124,94 @@ func printSummary(reports *entities.SystemDfReport, userFormat string) error {
size: volumesSize,
reclaimable: volumesReclaimable,
}
-
dfSummaries = append(dfSummaries, &volumeSummary)
- headers := "TYPE\tTOTAL\tACTIVE\tSIZE\tRECLAIMABLE\n"
- format = "{{range . }}" + format + "{{end}}"
- if len(userFormat) == 0 {
- format = headers + format
+ // need to give un-exported fields
+ hdrs := report.Headers(dfSummary{}, map[string]string{
+ "Size": "SIZE",
+ "Reclaimable": "RECLAIMABLE",
+ })
+
+ row := "{{.Type}}\t{{.Total}}\t{{.Active}}\t{{.Size}}\t{{.Reclaimable}}\n"
+ if cmd.Flags().Changed("format") {
+ row = report.NormalizeFormat(dfOptions.Format)
}
- return writeTemplate(w, format, dfSummaries)
+ row = "{{range . }}" + row + "{{end}}"
+
+ return writeTemplate(cmd, w, hdrs, row, dfSummaries)
}
-func printVerbose(reports *entities.SystemDfReport) error {
- var (
- w io.Writer = os.Stdout
- )
+func printVerbose(cmd *cobra.Command, w *tabwriter.Writer, reports *entities.SystemDfReport) error {
+ defer w.Flush()
// Images
- fmt.Print("\nImages space usage:\n\n")
+ fmt.Fprint(w, "Images space usage:\n\n")
// convert to dfImage for output
dfImages := make([]*dfImage, 0, len(reports.Images))
for _, d := range reports.Images {
dfImages = append(dfImages, &dfImage{SystemDfImageReport: d})
}
- imageHeaders := "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tSIZE\tSHARED SIZE\tUNIQUE SIZE\tCONTAINERS\n"
+ hdrs := report.Headers(entities.SystemDfImageReport{}, map[string]string{
+ "ImageID": "IMAGE ID",
+ "SharedSize": "SHARED SIZE",
+ "UniqueSize": "UNIQUE SIZE",
+ })
imageRow := "{{.Repository}}\t{{.Tag}}\t{{.ImageID}}\t{{.Created}}\t{{.Size}}\t{{.SharedSize}}\t{{.UniqueSize}}\t{{.Containers}}\n"
- format := imageHeaders + "{{range . }}" + imageRow + "{{end}}"
- if err := writeTemplate(w, format, dfImages); err != nil {
+ format := "{{range . }}" + imageRow + "{{end}}"
+ if err := writeTemplate(cmd, w, hdrs, format, dfImages); err != nil {
return nil
}
// Containers
- fmt.Print("\nContainers space usage:\n\n")
+ fmt.Fprint(w, "\nContainers space usage:\n\n")
// convert to dfContainers for output
dfContainers := make([]*dfContainer, 0, len(reports.Containers))
for _, d := range reports.Containers {
dfContainers = append(dfContainers, &dfContainer{SystemDfContainerReport: d})
}
- containerHeaders := "CONTAINER ID\tIMAGE\tCOMMAND\tLOCAL VOLUMES\tSIZE\tCREATED\tSTATUS\tNAMES\n"
+ hdrs = report.Headers(entities.SystemDfContainerReport{}, map[string]string{
+ "ContainerID": "CONTAINER ID",
+ "LocalVolumes": "LOCAL VOLUMES",
+ "RWSize": "SIZE",
+ })
containerRow := "{{.ContainerID}}\t{{.Image}}\t{{.Command}}\t{{.LocalVolumes}}\t{{.RWSize}}\t{{.Created}}\t{{.Status}}\t{{.Names}}\n"
- format = containerHeaders + "{{range . }}" + containerRow + "{{end}}"
- if err := writeTemplate(w, format, dfContainers); err != nil {
+ format = "{{range . }}" + containerRow + "{{end}}"
+ if err := writeTemplate(cmd, w, hdrs, format, dfContainers); err != nil {
return nil
}
// Volumes
- fmt.Print("\nLocal Volumes space usage:\n\n")
+ fmt.Fprint(w, "\nLocal Volumes space usage:\n\n")
dfVolumes := make([]*dfVolume, 0, len(reports.Volumes))
// convert to dfVolume for output
for _, d := range reports.Volumes {
dfVolumes = append(dfVolumes, &dfVolume{SystemDfVolumeReport: d})
}
- volumeHeaders := "VOLUME NAME\tLINKS\tSIZE\n"
+ hdrs = report.Headers(entities.SystemDfVolumeReport{}, map[string]string{
+ "VolumeName": "VOLUME NAME",
+ })
volumeRow := "{{.VolumeName}}\t{{.Links}}\t{{.Size}}\n"
- format = volumeHeaders + "{{range . }}" + volumeRow + "{{end}}"
- return writeTemplate(w, format, dfVolumes)
+ format = "{{range . }}" + volumeRow + "{{end}}"
+ return writeTemplate(cmd, w, hdrs, format, dfVolumes)
}
-func writeTemplate(w io.Writer, format string, output interface{}) error {
- tmpl, err := template.New("dfout").Parse(format)
+func writeTemplate(cmd *cobra.Command, w *tabwriter.Writer, hdrs []map[string]string, format string,
+ output interface{}) error {
+ defer w.Flush()
+
+ tmpl, err := template.New("df").Parse(format)
if err != nil {
return err
}
- w = tabwriter.NewWriter(w, 8, 2, 2, ' ', 0) //nolint
- if err := tmpl.Execute(w, output); err != nil {
- return err
- }
- if flusher, ok := w.(interface{ Flush() error }); ok {
- return flusher.Flush()
+
+ if !cmd.Flags().Changed("format") {
+ if err := tmpl.Execute(w, hdrs); err != nil {
+ return err
+ }
}
- return nil
+ return tmpl.Execute(w, output)
}
type dfImage struct {
diff --git a/cmd/podman/system/events.go b/cmd/podman/system/events.go
index 04e948f30..aaf572873 100644
--- a/cmd/podman/system/events.go
+++ b/cmd/podman/system/events.go
@@ -1,13 +1,12 @@
package system
import (
- "bufio"
"context"
+ "fmt"
"os"
- "strings"
"text/template"
- "github.com/containers/buildah/pkg/formats"
+ "github.com/containers/podman/v2/cmd/podman/parse"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/cmd/podman/validate"
"github.com/containers/podman/v2/libpod/events"
@@ -28,6 +27,7 @@ var (
RunE: eventsCmd,
Example: `podman events
podman events --filter event=create
+ podman events --format {{.Image}}
podman events --since 1h30s`,
}
)
@@ -51,60 +51,54 @@ func init() {
_ = flags.MarkHidden("stream")
}
-func eventsCmd(cmd *cobra.Command, args []string) error {
- var (
- err error
- eventsError error
- tmpl *template.Template
- )
- if strings.Join(strings.Fields(eventFormat), "") == "{{json.}}" {
- eventFormat = formats.JSONString
- }
- if eventFormat != formats.JSONString {
- tmpl, err = template.New("events").Parse(eventFormat)
- if err != nil {
- return err
- }
- }
+func eventsCmd(cmd *cobra.Command, _ []string) error {
if len(eventOptions.Since) > 0 || len(eventOptions.Until) > 0 {
eventOptions.FromStart = true
}
- eventChannel := make(chan *events.Event)
+ eventChannel := make(chan *events.Event, 1)
eventOptions.EventChan = eventChannel
+ errChannel := make(chan error)
+
+ var (
+ tmpl *template.Template
+ doJSON bool
+ )
+
+ if cmd.Flags().Changed("format") {
+ doJSON = parse.MatchesJSONFormat(eventFormat)
+ if !doJSON {
+ var err error
+ tmpl, err = template.New("events").Parse(eventFormat)
+ if err != nil {
+ return err
+ }
+ }
+ }
go func() {
- eventsError = registry.ContainerEngine().Events(context.Background(), eventOptions)
+ err := registry.ContainerEngine().Events(context.Background(), eventOptions)
+ errChannel <- err
}()
- if eventsError != nil {
- return eventsError
- }
- w := bufio.NewWriter(os.Stdout)
for event := range eventChannel {
switch {
- case eventFormat == formats.JSONString:
+ case event == nil:
+ // no-op
+ case doJSON:
jsonStr, err := event.ToJSONString()
if err != nil {
return errors.Wrapf(err, "unable to format json")
}
- if _, err := w.Write([]byte(jsonStr)); err != nil {
- return err
- }
- case len(eventFormat) > 0:
- if err := tmpl.Execute(w, event); err != nil {
+ fmt.Println(jsonStr)
+ case cmd.Flags().Changed("format"):
+ if err := tmpl.Execute(os.Stdout, event); err != nil {
return err
}
+ fmt.Println("")
default:
- if _, err := w.Write([]byte(event.ToHumanReadable())); err != nil {
- return err
- }
- }
- if _, err := w.Write([]byte("\n")); err != nil {
- return err
- }
- if err := w.Flush(); err != nil {
- return err
+ fmt.Println(event.ToHumanReadable())
}
}
- return nil
+
+ return <-errChannel
}
diff --git a/cmd/podman/system/info.go b/cmd/podman/system/info.go
index 3e3c99488..ee720abf8 100644
--- a/cmd/podman/system/info.go
+++ b/cmd/podman/system/info.go
@@ -69,26 +69,25 @@ func info(cmd *cobra.Command, args []string) error {
return err
}
- if parse.MatchesJSONFormat(inFormat) {
+ switch {
+ case parse.MatchesJSONFormat(inFormat):
b, err := json.MarshalIndent(info, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
- return nil
- }
- if !cmd.Flag("format").Changed {
+ case cmd.Flags().Changed("format"):
+ tmpl, err := template.New("info").Parse(inFormat)
+ if err != nil {
+ return err
+ }
+ return tmpl.Execute(os.Stdout, info)
+ default:
b, err := yaml.Marshal(info)
if err != nil {
return err
}
fmt.Println(string(b))
- return nil
- }
- tmpl, err := template.New("info").Parse(inFormat)
- if err != nil {
- return err
}
- err = tmpl.Execute(os.Stdout, info)
- return err
+ return nil
}
diff --git a/cmd/podman/system/version.go b/cmd/podman/system/version.go
index 9da7da54a..4f47c5fba 100644
--- a/cmd/podman/system/version.go
+++ b/cmd/podman/system/version.go
@@ -6,10 +6,11 @@ import (
"os"
"strings"
"text/tabwriter"
+ "text/template"
- "github.com/containers/buildah/pkg/formats"
"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/libpod/define"
"github.com/containers/podman/v2/pkg/domain/entities"
@@ -41,31 +42,38 @@ func version(cmd *cobra.Command, args []string) error {
return err
}
- switch {
- case parse.MatchesJSONFormat(versionFormat):
+ if parse.MatchesJSONFormat(versionFormat) {
s, err := json.MarshalToString(versions)
if err != nil {
return err
}
- _, err = io.WriteString(os.Stdout, s+"\n")
- return err
- case cmd.Flag("format").Changed:
- out := formats.StdoutTemplate{Output: versions, Template: versionFormat}
- err := out.Out()
+ fmt.Println(s)
+ return nil
+ }
+
+ w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
+ defer w.Flush()
+
+ if cmd.Flag("format").Changed {
+ row := report.NormalizeFormat(versionFormat)
+ tmpl, err := template.New("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
- versionFormat = strings.Replace(versionFormat, ".Server.", ".", 1)
- out = formats.StdoutTemplate{Output: versions.Client, Template: versionFormat}
- if err1 := out.Out(); err1 != nil {
+ row = strings.Replace(row, ".Server.", ".", 1)
+ tmpl, err := template.New("version 1.0.0").Parse(row)
+ if err != nil {
+ return err
+ }
+ if err := tmpl.Execute(w, versions.Client); err != nil {
return err
}
}
return nil
}
- w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
- defer w.Flush()
-
if versions.Server != nil {
if _, err := fmt.Fprintf(w, "Client:\n"); err != nil {
return err
@@ -81,13 +89,13 @@ func version(cmd *cobra.Command, args []string) error {
return nil
}
-func formatVersion(writer io.Writer, version *define.Version) {
- fmt.Fprintf(writer, "Version:\t%s\n", version.Version)
- fmt.Fprintf(writer, "API Version:\t%s\n", version.APIVersion)
- fmt.Fprintf(writer, "Go Version:\t%s\n", version.GoVersion)
+func formatVersion(w io.Writer, version *define.Version) {
+ fmt.Fprintf(w, "Version:\t%s\n", version.Version)
+ fmt.Fprintf(w, "API Version:\t%s\n", version.APIVersion)
+ fmt.Fprintf(w, "Go Version:\t%s\n", version.GoVersion)
if version.GitCommit != "" {
- fmt.Fprintf(writer, "Git Commit:\t%s\n", version.GitCommit)
+ fmt.Fprintf(w, "Git Commit:\t%s\n", version.GitCommit)
}
- fmt.Fprintf(writer, "Built:\t%s\n", version.BuiltTime)
- fmt.Fprintf(writer, "OS/Arch:\t%s\n", version.OsArch)
+ fmt.Fprintf(w, "Built:\t%s\n", version.BuiltTime)
+ fmt.Fprintf(w, "OS/Arch:\t%s\n", version.OsArch)
}
diff --git a/cmd/podman/volumes/inspect.go b/cmd/podman/volumes/inspect.go
index ce24ac4e5..8d1350228 100644
--- a/cmd/podman/volumes/inspect.go
+++ b/cmd/podman/volumes/inspect.go
@@ -3,11 +3,11 @@ package volumes
import (
"fmt"
"os"
- "strings"
"text/template"
- "github.com/containers/buildah/pkg/formats"
+ "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/pkg/domain/entities"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -53,26 +53,21 @@ func inspect(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
- switch inspectFormat {
- case "", formats.JSONString:
+
+ switch {
+ case parse.MatchesJSONFormat(inspectFormat), inspectFormat == "":
jsonOut, err := json.MarshalIndent(responses, "", " ")
if err != nil {
return errors.Wrapf(err, "error marshalling inspect JSON")
}
fmt.Println(string(jsonOut))
default:
- if !strings.HasSuffix(inspectFormat, "\n") {
- inspectFormat += "\n"
- }
- format := "{{range . }}" + inspectFormat + "{{end}}"
- tmpl, err := template.New("volumeInspect").Parse(format)
+ row := "{{range . }}" + report.NormalizeFormat(inspectFormat) + "{{end}}"
+ tmpl, err := template.New("volumeInspect").Parse(row)
if err != nil {
return err
}
- if err := tmpl.Execute(os.Stdout, responses); err != nil {
- return err
- }
+ return tmpl.Execute(os.Stdout, responses)
}
return nil
-
}