Merge pull request #7960 from jwhonce/jira/run-898-2

Restore --format 'table...' to commands
This commit is contained in:
OpenShift Merge Robot 2020-10-08 15:25:48 -04:00 committed by GitHub
commit d7ffcf7298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 17 deletions

View File

@ -4,10 +4,14 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"regexp"
"strings" "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/registry"
"github.com/containers/podman/v2/cmd/podman/report"
"github.com/containers/podman/v2/cmd/podman/validate" "github.com/containers/podman/v2/cmd/podman/validate"
"github.com/containers/podman/v2/pkg/domain/entities" "github.com/containers/podman/v2/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -24,6 +28,9 @@ const (
AllType = "all" AllType = "all"
) )
// Pull in configured json library
var json = registry.JSONLibrary()
// AddInspectFlagSet takes a command and adds the inspect flags and returns an // AddInspectFlagSet takes a command and adds the inspect flags and returns an
// InspectOptions object. // InspectOptions object.
func AddInspectFlagSet(cmd *cobra.Command) *entities.InspectOptions { func AddInspectFlagSet(cmd *cobra.Command) *entities.InspectOptions {
@ -80,7 +87,7 @@ func newInspector(options entities.InspectOptions) (*inspector, error) {
// inspect inspects the specified container/image names or IDs. // inspect inspects the specified container/image names or IDs.
func (i *inspector) inspect(namesOrIDs []string) error { func (i *inspector) inspect(namesOrIDs []string) error {
// data - dumping place for inspection results. // data - dumping place for inspection results.
var data []interface{} //nolint var data []interface{} // nolint
var errs []error var errs []error
ctx := context.Background() ctx := context.Background()
@ -134,15 +141,19 @@ func (i *inspector) inspect(namesOrIDs []string) error {
data = []interface{}{} data = []interface{}{}
} }
var out formats.Writer var err error
if i.options.Format == "json" || i.options.Format == "" { // "" for backwards compat switch {
out = formats.JSONStructArray{Output: data} case parse.MatchesJSONFormat(i.options.Format) || i.options.Format == "":
} else { err = printJSON(data)
out = formats.StdoutTemplateArray{Output: data, Template: inspectFormat(i.options.Format)} default:
row := inspectNormalize(i.options.Format)
row = "{{range . }}" + report.NormalizeFormat(row) + "{{end}}"
err = printTmpl(tmpType, row, data)
} }
if err := out.Out(); err != nil { if err != nil {
logrus.Errorf("Error printing inspect output: %v", err) logrus.Errorf("Error printing inspect output: %v", err)
} }
if len(errs) > 0 { if len(errs) > 0 {
if len(errs) > 1 { if len(errs) > 1 {
for _, err := range errs[1:] { for _, err := range errs[1:] {
@ -154,8 +165,22 @@ func (i *inspector) inspect(namesOrIDs []string) error {
return nil return nil
} }
func printJSON(data []interface{}) error {
enc := json.NewEncoder(os.Stdout)
return enc.Encode(data)
}
func printTmpl(typ, row string, data []interface{}) error {
t, err := template.New(typ + " inspect").Parse(row)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
return t.Execute(w, data)
}
func (i *inspector) inspectAll(ctx context.Context, namesOrIDs []string) ([]interface{}, []error, error) { func (i *inspector) inspectAll(ctx context.Context, namesOrIDs []string) ([]interface{}, []error, error) {
var data []interface{} //nolint var data []interface{} // nolint
allErrs := []error{} allErrs := []error{}
for _, name := range namesOrIDs { for _, name := range namesOrIDs {
ctrData, errs, err := i.containerEngine.ContainerInspect(ctx, []string{name}, i.options) ctrData, errs, err := i.containerEngine.ContainerInspect(ctx, []string{name}, i.options)
@ -179,9 +204,11 @@ func (i *inspector) inspectAll(ctx context.Context, namesOrIDs []string) ([]inte
return data, allErrs, nil return data, allErrs, nil
} }
func inspectFormat(row string) string { func inspectNormalize(row string) string {
m := regexp.MustCompile(`{{\s*\.Id\s*}}`)
row = m.ReplaceAllString(row, "{{.ID}}")
r := strings.NewReplacer( r := strings.NewReplacer(
"{{.Id}}", formats.IDString,
".Src", ".Source", ".Src", ".Source",
".Dst", ".Destination", ".Dst", ".Destination",
".ImageID", ".Image", ".ImageID", ".Image",

View File

@ -3,9 +3,13 @@ package pods
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"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/registry"
"github.com/containers/podman/v2/cmd/podman/report"
"github.com/containers/podman/v2/cmd/podman/validate" "github.com/containers/podman/v2/cmd/podman/validate"
"github.com/containers/podman/v2/pkg/domain/entities" "github.com/containers/podman/v2/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -57,11 +61,19 @@ func inspect(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
var data interface{} = responses
var out formats.Writer = formats.JSONStruct{Output: data} if parse.MatchesJSONFormat(inspectOptions.Format) {
if inspectOptions.Format != "json" { enc := json.NewEncoder(os.Stdout)
out = formats.StdoutTemplate{Output: data, Template: inspectOptions.Format} return enc.Encode(responses)
} }
return out.Out() row := report.NormalizeFormat(inspectOptions.Format)
t, err := template.New("pod inspect").Parse(row)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
return t.Execute(w, *responses)
} }