Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore --format table... #7975

Merged
merged 1 commit into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/podman/generate/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"

"github.com/containers/podman/v2/cmd/podman/parse"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/cmd/podman/utils"
"github.com/containers/podman/v2/pkg/domain/entities"
Expand Down Expand Up @@ -97,10 +98,10 @@ func systemd(cmd *cobra.Command, args []string) error {
}
}

switch format {
case "json":
switch {
case parse.MatchesJSONFormat(format):
return printJSON(report.Units)
case "":
case format == "":
return printDefault(report.Units)
default:
return errors.Errorf("unknown --format argument: %s", format)
Expand Down
38 changes: 20 additions & 18 deletions cmd/podman/networks/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package network
import (
"encoding/json"
"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/pkg/domain/entities"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -39,31 +40,32 @@ func init() {
flags.StringVarP(&networkInspectOptions.Format, "format", "f", "", "Pretty-print network to JSON or using a Go template")
}

func networkInspect(cmd *cobra.Command, args []string) error {
func networkInspect(_ *cobra.Command, args []string) error {
responses, err := registry.ContainerEngine().NetworkInspect(registry.Context(), args, entities.NetworkInspectOptions{})
if err != nil {
return err
}
b, err := json.MarshalIndent(responses, "", " ")
if err != nil {
return err
}
if strings.ToLower(networkInspectOptions.Format) == "json" || networkInspectOptions.Format == "" {
fmt.Println(string(b))
} else {
var w io.Writer = os.Stdout
//There can be more than 1 in the inspect output.
format := "{{range . }}" + networkInspectOptions.Format + "{{end}}"
tmpl, err := template.New("inspectNetworks").Parse(format)

switch {
case parse.MatchesJSONFormat(networkInspectOptions.Format) || networkInspectOptions.Format == "":
b, err := json.MarshalIndent(responses, "", " ")
if err != nil {
return err
}
if err := tmpl.Execute(w, responses); err != nil {
fmt.Println(string(b))
default:
row := report.NormalizeFormat(networkInspectOptions.Format)
// There can be more than 1 in the inspect output.
row = "{{range . }}" + row + "{{end}}"
tmpl, err := template.New("inspectNetworks").Parse(row)
if err != nil {
return err
}
if flusher, ok := w.(interface{ Flush() error }); ok {
return flusher.Flush()
}

w := tabwriter.NewWriter(os.Stdout, 8, 2, 0, ' ', 0)
defer w.Flush()

return tmpl.Execute(w, responses)
}
return nil
}
108 changes: 51 additions & 57 deletions cmd/podman/pods/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package pods
import (
"context"
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"
"time"

"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/docker/go-units"
Expand All @@ -34,10 +35,9 @@ var (
)

var (
defaultHeaders = "POD ID\tNAME\tSTATUS\tCREATED"
inputFilters []string
noTrunc bool
psInput entities.PodPSOptions
inputFilters []string
noTrunc bool
psInput entities.PodPSOptions
)

func init() {
Expand All @@ -62,11 +62,6 @@ func init() {
}

func pods(cmd *cobra.Command, _ []string) error {
var (
w io.Writer = os.Stdout
row string
)

if psInput.Quiet && len(psInput.Format) > 0 {
return errors.New("quiet and format cannot be used together")
}
Expand All @@ -89,80 +84,79 @@ func pods(cmd *cobra.Command, _ []string) error {
return err
}

if psInput.Format == "json" {
switch {
case parse.MatchesJSONFormat(psInput.Format):
b, err := json.MarshalIndent(responses, "", " ")
if err != nil {
return err
}
fmt.Println(string(b))
return nil
case psInput.Quiet:
for _, p := range responses {
fmt.Println(p.Id)
}
return nil
}

// Formatted output below
lpr := make([]ListPodReporter, 0, len(responses))
for _, r := range responses {
lpr = append(lpr, ListPodReporter{r})
}
headers, row := createPodPsOut()
if psInput.Quiet {
row = "{{.Id}}\n"
}
if cmd.Flag("format").Changed {
row = psInput.Format
if !strings.HasPrefix(row, "\n") {
row += "\n"
}
}
format := "{{range . }}" + row + "{{end}}"
if !psInput.Quiet && !cmd.Flag("format").Changed {
format = headers + format

headers := report.Headers(ListPodReporter{}, map[string]string{
"ContainerIds": "IDS",
"ContainerNames": "NAMES",
"ContainerStatuses": "STATUS",
"Namespace": "NAMESPACES",
"NumberOfContainers": "# OF CONTAINERS",
"InfraId": "INFRA ID",
})
row := podPsFormat()
if cmd.Flags().Changed("format") {
row = report.NormalizeFormat(psInput.Format)
}
tmpl, err := template.New("listPods").Parse(format)
row = "{{range . }}" + row + "{{end}}"

tmpl, err := template.New("listPods").Parse(row)
if err != nil {
return err
}
if !psInput.Quiet {
w = tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
}
if err := tmpl.Execute(w, lpr); err != nil {
return err
}
if flusher, ok := w.(interface{ Flush() error }); ok {
return flusher.Flush()
w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
defer w.Flush()

if !psInput.Quiet && !cmd.Flag("format").Changed {
if err := tmpl.Execute(w, headers); err != nil {
return err
}
}
return nil
return tmpl.Execute(w, lpr)
}

func createPodPsOut() (string, string) {
var row string
headers := defaultHeaders
row += "{{.Id}}"

row += "\t{{.Name}}\t{{.Status}}\t{{.Created}}"
func podPsFormat() string {
row := []string{"{{.Id}}", "{{.Name}}", "{{.Status}}", "{{.Created}}}"}

if psInput.CtrIds {
headers += "\tIDS"
row += "\t{{.ContainerIds}}"
row = append(row, "{{.ContainerIds}}")
}

if psInput.CtrNames {
headers += "\tNAMES"
row += "\t{{.ContainerNames}}"
row = append(row, "{{.ContainerNames}}")
}

if psInput.CtrStatus {
headers += "\tSTATUS"
row += "\t{{.ContainerStatuses}}"
row = append(row, "{{.ContainerStatuses}}")
}

if psInput.Namespace {
headers += "\tCGROUP\tNAMESPACES"
row += "\t{{.Cgroup}}\t{{.Namespace}}"
row = append(row, "{{.Cgroup}}", "{{.Namespace}}")
}
if !psInput.CtrStatus && !psInput.CtrNames && !psInput.CtrIds {
headers += "\t# OF CONTAINERS"
row += "\t{{.NumberOfContainers}}"

if !psInput.CtrStatus && !psInput.CtrNames && !psInput.CtrIds {
row = append(row, "{{.NumberOfContainers}}")
}
headers += "\tINFRA ID\n"
row += "\t{{.InfraId}}\n"
return headers, row
return strings.Join(row, "\t") + "\n"
}

// ListPodReporter is a struct for pod ps output
Expand All @@ -180,7 +174,7 @@ func (l ListPodReporter) Labels() map[string]string {
return l.ListPodsReport.Labels
}

// NumberofContainers returns an int representation for
// NumberOfContainers returns an int representation for
// the number of containers belonging to the pod
func (l ListPodReporter) NumberOfContainers() int {
return len(l.Containers)
Expand All @@ -192,7 +186,7 @@ func (l ListPodReporter) ID() string {
}

// Id returns the Pod id
func (l ListPodReporter) Id() string { //nolint
func (l ListPodReporter) Id() string { // nolint
if noTrunc {
return l.ListPodsReport.Id
}
Expand All @@ -206,7 +200,7 @@ func (l ListPodReporter) InfraID() string {

// InfraId returns the infra container id for the pod
// depending on trunc
func (l ListPodReporter) InfraId() string { //nolint
func (l ListPodReporter) InfraId() string { // nolint
if len(l.ListPodsReport.InfraId) == 0 {
return ""
}
Expand Down
Loading