mirror of
https://github.com/containers/podman.git
synced 2025-06-28 14:29:04 +08:00
Merge pull request #12490 from jwhonce/issues/10974-3
Refactor podman pods to report.Formatter
This commit is contained in:
@ -3,6 +3,7 @@ package pods
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
"os"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/containers/common/pkg/report"
|
"github.com/containers/common/pkg/report"
|
||||||
"github.com/containers/podman/v3/cmd/podman/common"
|
"github.com/containers/podman/v3/cmd/podman/common"
|
||||||
@ -64,25 +65,17 @@ func inspect(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if report.IsJSON(inspectOptions.Format) {
|
if report.IsJSON(inspectOptions.Format) {
|
||||||
json.MarshalIndent(responses, "", " ")
|
|
||||||
enc := json.NewEncoder(os.Stdout)
|
enc := json.NewEncoder(os.Stdout)
|
||||||
enc.SetIndent("", " ")
|
enc.SetIndent("", " ")
|
||||||
return enc.Encode(responses)
|
return enc.Encode(responses)
|
||||||
}
|
}
|
||||||
|
|
||||||
// cmd.Flags().Changed("format") must be true to reach this code
|
// Cannot use report.New() as it enforces {{range .}} for OriginUser templates
|
||||||
row := report.NormalizeFormat(inspectOptions.Format)
|
tmpl := template.New(cmd.Name()).Funcs(template.FuncMap(report.DefaultFuncs))
|
||||||
|
format := report.NormalizeFormat(inspectOptions.Format)
|
||||||
t, err := report.NewTemplate("inspect").Parse(row)
|
tmpl, err = tmpl.Parse(format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return tmpl.Execute(os.Stdout, *responses)
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = t.Execute(w, *responses)
|
|
||||||
w.Flush()
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
@ -110,56 +110,49 @@ func pods(cmd *cobra.Command, _ []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatted output below
|
|
||||||
lpr := make([]ListPodReporter, 0, len(responses))
|
lpr := make([]ListPodReporter, 0, len(responses))
|
||||||
for _, r := range responses {
|
for _, r := range responses {
|
||||||
lpr = append(lpr, ListPodReporter{r})
|
lpr = append(lpr, ListPodReporter{r})
|
||||||
}
|
}
|
||||||
|
|
||||||
headers := report.Headers(ListPodReporter{}, map[string]string{
|
rpt := report.New(os.Stdout, cmd.Name())
|
||||||
"Id": "POD ID",
|
defer rpt.Flush()
|
||||||
"Name": "NAME",
|
|
||||||
"Status": "STATUS",
|
|
||||||
"Labels": "LABELS",
|
|
||||||
"NumberOfContainers": "# OF CONTAINERS",
|
|
||||||
"Created": "CREATED",
|
|
||||||
"InfraID": "INFRA ID",
|
|
||||||
"ContainerIds": "IDS",
|
|
||||||
"ContainerNames": "NAMES",
|
|
||||||
"ContainerStatuses": "STATUS",
|
|
||||||
"Cgroup": "CGROUP",
|
|
||||||
"Namespace": "NAMESPACES",
|
|
||||||
})
|
|
||||||
renderHeaders := true
|
|
||||||
row := podPsFormat()
|
|
||||||
if cmd.Flags().Changed("format") {
|
|
||||||
renderHeaders = report.HasTable(psInput.Format)
|
|
||||||
row = report.NormalizeFormat(psInput.Format)
|
|
||||||
}
|
|
||||||
format := report.EnforceRange(row)
|
|
||||||
|
|
||||||
noHeading, _ := cmd.Flags().GetBool("noheading")
|
if cmd.Flags().Changed("format") {
|
||||||
if noHeading {
|
rpt, err = rpt.Parse(report.OriginUser, psInput.Format)
|
||||||
|
} else {
|
||||||
|
rpt, err = rpt.Parse(report.OriginPodman, podPsFormat())
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
renderHeaders := true
|
||||||
|
if noHeading, _ := cmd.Flags().GetBool("noheading"); noHeading {
|
||||||
renderHeaders = false
|
renderHeaders = false
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := report.NewTemplate("list").Parse(format)
|
if renderHeaders && rpt.RenderHeaders {
|
||||||
if err != nil {
|
headers := report.Headers(ListPodReporter{}, map[string]string{
|
||||||
return err
|
"Id": "POD ID",
|
||||||
}
|
"Name": "NAME",
|
||||||
|
"Status": "STATUS",
|
||||||
|
"Labels": "LABELS",
|
||||||
|
"NumberOfContainers": "# OF CONTAINERS",
|
||||||
|
"Created": "CREATED",
|
||||||
|
"InfraID": "INFRA ID",
|
||||||
|
"ContainerIds": "IDS",
|
||||||
|
"ContainerNames": "NAMES",
|
||||||
|
"ContainerStatuses": "STATUS",
|
||||||
|
"Cgroup": "CGROUP",
|
||||||
|
"Namespace": "NAMESPACES",
|
||||||
|
})
|
||||||
|
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
if err := rpt.Execute(headers); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer w.Flush()
|
|
||||||
|
|
||||||
if renderHeaders {
|
|
||||||
if err := tmpl.Execute(w, headers); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tmpl.Execute(w, lpr)
|
return rpt.Execute(lpr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func podPsFormat() string {
|
func podPsFormat() string {
|
||||||
@ -184,7 +177,7 @@ func podPsFormat() string {
|
|||||||
if !psInput.CtrStatus && !psInput.CtrNames && !psInput.CtrIds {
|
if !psInput.CtrStatus && !psInput.CtrNames && !psInput.CtrIds {
|
||||||
row = append(row, "{{.NumberOfContainers}}")
|
row = append(row, "{{.NumberOfContainers}}")
|
||||||
}
|
}
|
||||||
return strings.Join(row, "\t") + "\n"
|
return "{{range . }}" + strings.Join(row, "\t") + "\n" + "{{end -}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListPodReporter is a struct for pod ps output
|
// ListPodReporter is a struct for pod ps output
|
||||||
|
@ -67,15 +67,22 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
doJSON := report.IsJSON(cmd.Flag("format").Value.String())
|
rpt := report.New(os.Stdout, cmd.Name())
|
||||||
headers := report.Headers(entities.PodStatsReport{}, map[string]string{
|
defer rpt.Flush()
|
||||||
"CPU": "CPU %",
|
|
||||||
"MemUsage": "MEM USAGE/ LIMIT",
|
var err error
|
||||||
"MemUsageBytes": "MEM USAGE/ LIMIT",
|
doJSON := report.IsJSON(statsOptions.Format)
|
||||||
"MEM": "MEM %",
|
if !doJSON {
|
||||||
"NET IO": "NET IO",
|
if cmd.Flags().Changed("format") {
|
||||||
"BlockIO": "BLOCK IO",
|
rpt, err = rpt.Parse(report.OriginUser, statsOptions.Format)
|
||||||
})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
rpt = rpt.Init(os.Stdout, 12, 2, 2, ' ', 0)
|
||||||
|
rpt.Origin = report.OriginPodman
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for ; ; time.Sleep(time.Second) {
|
for ; ; time.Sleep(time.Second) {
|
||||||
reports, err := registry.ContainerEngine().PodStats(context.Background(), args, statsOptions.PodStatsOptions)
|
reports, err := registry.ContainerEngine().PodStats(context.Background(), args, statsOptions.PodStatsOptions)
|
||||||
@ -84,30 +91,26 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
// Print the stats in the requested format and configuration.
|
// Print the stats in the requested format and configuration.
|
||||||
if doJSON {
|
if doJSON {
|
||||||
if err := printJSONPodStats(reports); err != nil {
|
err = printJSONPodStats(reports)
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if !statsOptions.NoReset {
|
if !statsOptions.NoReset {
|
||||||
goterm.Clear()
|
goterm.Clear()
|
||||||
goterm.MoveCursor(1, 1)
|
goterm.MoveCursor(1, 1)
|
||||||
goterm.Flush()
|
goterm.Flush()
|
||||||
}
|
}
|
||||||
if cmd.Flags().Changed("format") {
|
if report.OriginUser == rpt.Origin {
|
||||||
row := report.NormalizeFormat(statsOptions.Format)
|
err = userTemplate(rpt, reports)
|
||||||
row = report.EnforceRange(row)
|
|
||||||
if err := printFormattedPodStatsLines(headers, row, reports); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
printPodStatsLines(reports)
|
err = defaultTemplate(rpt, reports)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if statsOptions.NoStream {
|
if statsOptions.NoStream {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,42 +123,38 @@ func printJSONPodStats(stats []*entities.PodStatsReport) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printPodStatsLines(stats []*entities.PodStatsReport) error {
|
func defaultTemplate(rpt *report.Formatter, 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"
|
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")
|
fmt.Fprintf(rpt.Writer(), outFormat, "POD", "CID", "NAME", "CPU %", "MEM USAGE/ LIMIT", "MEM %", "NET IO", "BLOCK IO", "PIDS")
|
||||||
if len(stats) == 0 {
|
if len(stats) == 0 {
|
||||||
fmt.Fprintf(w, outFormat, "--", "--", "--", "--", "--", "--", "--", "--", "--")
|
fmt.Fprintf(rpt.Writer(), outFormat, "--", "--", "--", "--", "--", "--", "--", "--", "--")
|
||||||
} else {
|
} else {
|
||||||
for _, i := range stats {
|
for _, i := range stats {
|
||||||
fmt.Fprintf(w, outFormat, i.Pod, i.CID, i.Name, i.CPU, i.MemUsage, i.Mem, i.NetIO, i.BlockIO, i.PIDS)
|
fmt.Fprintf(rpt.Writer(), outFormat, i.Pod, i.CID, i.Name, i.CPU, i.MemUsage, i.Mem, i.NetIO, i.BlockIO, i.PIDS)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return w.Flush()
|
return rpt.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
func printFormattedPodStatsLines(headerNames []map[string]string, row string, stats []*entities.PodStatsReport) error {
|
func userTemplate(rpt *report.Formatter, stats []*entities.PodStatsReport) error {
|
||||||
if len(stats) == 0 {
|
if len(stats) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl, err := report.NewTemplate("stats").Parse(row)
|
headers := report.Headers(entities.PodStatsReport{}, map[string]string{
|
||||||
if err != nil {
|
"CPU": "CPU %",
|
||||||
return err
|
"MemUsage": "MEM USAGE/ LIMIT",
|
||||||
}
|
"MemUsageBytes": "MEM USAGE/ LIMIT",
|
||||||
|
"MEM": "MEM %",
|
||||||
|
"NET IO": "NET IO",
|
||||||
|
"BlockIO": "BLOCK IO",
|
||||||
|
})
|
||||||
|
|
||||||
w, err := report.NewWriterDefault(os.Stdout)
|
if err := rpt.Execute(headers); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer w.Flush()
|
if err := rpt.Execute(stats); err != nil {
|
||||||
|
|
||||||
if err := tmpl.Execute(w, headerNames); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return tmpl.Execute(w, stats)
|
return rpt.Flush()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user