From 836bfebb4695e31338c0bfc74181e9095b937ef9 Mon Sep 17 00:00:00 2001 From: Parker Van Roy Date: Tue, 13 Oct 2020 10:57:01 -0400 Subject: Refactored file moved --format to templateOut() Rm parse package except JSON Signed-off-by: Parker Van Roy --- cmd/podman/networks/list.go | 63 ++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 23 deletions(-) (limited to 'cmd/podman/networks') diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go index 6d8d35589..25d2c024e 100644 --- a/cmd/podman/networks/list.go +++ b/cmd/podman/networks/list.go @@ -38,6 +38,7 @@ var ( networkListOptions entities.NetworkListOptions filters []string noTrunc bool + defaultListRow = "{{.Name}}\t{{.Version}}\t{{.Plugins}}\n" ) func networkListFlags(flags *pflag.FlagSet) { @@ -78,13 +79,37 @@ func networkList(cmd *cobra.Command, args []string) error { } switch { - case report.IsJSON(networkListOptions.Format): - return jsonOut(responses) + // quiet means we only print the network names case networkListOptions.Quiet: - // quiet means we only print the network names return quietOut(responses) + + // TODO remove references to parse package + case parse.MatchesJSONFormat(networkListOptions.Format): + return jsonOut(responses) + + // table or other format output + default: + return templateOut(responses, cmd) + } +} + +func quietOut(responses []*entities.NetworkListReport) error { + for _, r := range responses { + fmt.Println(r.Name) + } + return nil +} + +func jsonOut(responses []*entities.NetworkListReport) error { + prettyJSON, err := json.MarshalIndent(responses, "", " ") + if err != nil { + return err } + fmt.Println(string(prettyJSON)) + return nil +} +func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) error { nlprs := make([]ListPrintReports, 0, len(responses)) for _, r := range responses { nlprs = append(nlprs, ListPrintReports{r}) @@ -99,13 +124,16 @@ func networkList(cmd *cobra.Command, args []string) error { "Labels": "labels", "ID": "network id", }) - renderHeaders := true - row := "{{.Name}}\t{{.Version}}\t{{.Plugins}}\n" + + renderHeaders := strings.HasPrefix(networkListOptions.Format, "table ") + var row, format string if cmd.Flags().Changed("format") { - renderHeaders = parse.HasTable(networkListOptions.Format) row = report.NormalizeFormat(networkListOptions.Format) + } else { // 'podman network ls' equivalent to 'podman network ls --format="table {{ .Name }} {{ .Version }} {{ .Plugins }}" ' + renderHeaders = true + row = defaultListRow } - format := parse.EnforceRange(row) + format = "{{range .}}" + row + "{{end}}" tmpl, err := template.New("listNetworks").Parse(format) if err != nil { @@ -122,34 +150,22 @@ func networkList(cmd *cobra.Command, args []string) error { return tmpl.Execute(w, nlprs) } -func quietOut(responses []*entities.NetworkListReport) error { - for _, r := range responses { - fmt.Println(r.Name) - } - return nil -} - -func jsonOut(responses []*entities.NetworkListReport) error { - b, err := json.MarshalIndent(responses, "", " ") - if err != nil { - return err - } - fmt.Println(string(b)) - return nil -} - +// ListPrintReports returns the network list report type ListPrintReports struct { *entities.NetworkListReport } +// Version returns the CNI version func (n ListPrintReports) Version() string { return n.CNIVersion } +// Plugins returns the CNI Plugins func (n ListPrintReports) Plugins() string { return network.GetCNIPlugins(n.NetworkConfigList) } +// Labels returns any labels added to a Network func (n ListPrintReports) Labels() string { list := make([]string, 0, len(n.NetworkListReport.Labels)) for k, v := range n.NetworkListReport.Labels { @@ -158,6 +174,7 @@ func (n ListPrintReports) Labels() string { return strings.Join(list, ",") } +// ID returns the Podman Network ID func (n ListPrintReports) ID() string { length := 12 if noTrunc { -- cgit v1.2.3-54-g00ecf From ef549235eb63117d10537e6d54adf448e05b40ad Mon Sep 17 00:00:00 2001 From: Parker Van Roy Date: Mon, 22 Feb 2021 16:11:06 -0500 Subject: Updated based on Jhonce comments Added ID to default removed outdated imports slight refactor Test Network Headers added to network_test.go Signed-off-by: Parker Van Roy --- cmd/podman/networks/list.go | 26 ++++++++++++-------------- cmd/podman/networks/network.go | 3 +++ test/e2e/network_test.go | 7 +++++++ 3 files changed, 22 insertions(+), 14 deletions(-) (limited to 'cmd/podman/networks') diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go index 25d2c024e..2181f850b 100644 --- a/cmd/podman/networks/list.go +++ b/cmd/podman/networks/list.go @@ -1,7 +1,6 @@ package network import ( - "encoding/json" "fmt" "os" "strings" @@ -11,7 +10,6 @@ import ( "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/validate" "github.com/containers/podman/v3/libpod/network" @@ -38,7 +36,6 @@ var ( networkListOptions entities.NetworkListOptions filters []string noTrunc bool - defaultListRow = "{{.Name}}\t{{.Version}}\t{{.Plugins}}\n" ) func networkListFlags(flags *pflag.FlagSet) { @@ -81,23 +78,24 @@ func networkList(cmd *cobra.Command, args []string) error { switch { // quiet means we only print the network names case networkListOptions.Quiet: - return quietOut(responses) + quietOut(responses) - // TODO remove references to parse package - case parse.MatchesJSONFormat(networkListOptions.Format): - return jsonOut(responses) + // JSON output formatting + case report.IsJSON(networkListOptions.Format): + err = jsonOut(responses) // table or other format output default: - return templateOut(responses, cmd) + err = templateOut(responses, cmd) } + + return err } -func quietOut(responses []*entities.NetworkListReport) error { +func quietOut(responses []*entities.NetworkListReport) { for _, r := range responses { fmt.Println(r.Name) } - return nil } func jsonOut(responses []*entities.NetworkListReport) error { @@ -125,15 +123,15 @@ func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) er "ID": "network id", }) - renderHeaders := strings.HasPrefix(networkListOptions.Format, "table ") + renderHeaders := report.HasTable(networkListOptions.Format) var row, format string if cmd.Flags().Changed("format") { row = report.NormalizeFormat(networkListOptions.Format) - } else { // 'podman network ls' equivalent to 'podman network ls --format="table {{ .Name }} {{ .Version }} {{ .Plugins }}" ' + } else { // 'podman network ls' equivalent to 'podman network ls --format="table {{.ID}} {{.Name}} {{.Version}} {{.Plugins}}" ' renderHeaders = true - row = defaultListRow + row = "{{.ID}}\t{{.Name}}\t{{.Version}}\t{{.Plugins}}\n" } - format = "{{range .}}" + row + "{{end}}" + format = report.EnforceRange(row) tmpl, err := template.New("listNetworks").Parse(format) if err != nil { diff --git a/cmd/podman/networks/network.go b/cmd/podman/networks/network.go index e729f35f3..4d6cd8abd 100644 --- a/cmd/podman/networks/network.go +++ b/cmd/podman/networks/network.go @@ -8,6 +8,9 @@ import ( ) var ( + // Pull in configured json library + json = registry.JSONLibrary() + // Command: podman _network_ networkCmd = &cobra.Command{ Use: "network", diff --git a/test/e2e/network_test.go b/test/e2e/network_test.go index a7eb6e629..53521cdc4 100644 --- a/test/e2e/network_test.go +++ b/test/e2e/network_test.go @@ -150,6 +150,13 @@ var _ = Describe("Podman network", func() { defer podmanTest.removeCNINetwork(net) Expect(session.ExitCode()).To(BeZero()) + // Tests Default Table Output + session = podmanTest.Podman([]string{"network", "ls", "--filter", "id=" + netID}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(BeZero()) + expectedTable := "NETWORK ID NAME VERSION PLUGINS" + Expect(session.OutputToString()).To(ContainSubstring(expectedTable)) + session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID}) session.WaitWithDefaultTimeout() Expect(session.ExitCode()).To(BeZero()) -- cgit v1.2.3-54-g00ecf