Skip to content

Commit

Permalink
Scrub podman commands to use report package
Browse files Browse the repository at this point in the history
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 containers/common#624
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1855983

Signed-off-by: Jhon Honce <[email protected]>

<MH: Fixed cherry-pick conflicts>

Signed-off-by: Matthew Heon <[email protected]>
  • Loading branch information
jwhonce authored and mheon committed Jun 25, 2021
1 parent 647c202 commit 5dabff2
Show file tree
Hide file tree
Showing 28 changed files with 186 additions and 202 deletions.
12 changes: 7 additions & 5 deletions cmd/podman/containers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
17 changes: 8 additions & 9 deletions cmd/podman/containers/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions cmd/podman/containers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions cmd/podman/containers/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions cmd/podman/images/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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") {
Expand Down
22 changes: 11 additions & 11 deletions cmd/podman/images/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import (
"os"
"sort"
"strings"
"text/tabwriter"
"text/template"
"time"
"unicode"

"github.com/containers/common/pkg/completion"
"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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions cmd/podman/images/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down
15 changes: 8 additions & 7 deletions cmd/podman/images/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 13 additions & 10 deletions cmd/podman/images/trust_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -45,31 +44,35 @@ 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
}
fmt.Println(string(b))
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 {
Expand Down
Loading

0 comments on commit 5dabff2

Please sign in to comment.