diff options
author | Parker Van Roy <pvanroy@redhat.com> | 2021-02-22 16:11:06 -0500 |
---|---|---|
committer | Parker Van Roy <pvanroy@redhat.com> | 2021-02-24 14:52:19 -0500 |
commit | ef549235eb63117d10537e6d54adf448e05b40ad (patch) | |
tree | 74d3fbca2c05a1809a0ab14b426e734ff57dd043 | |
parent | ccbe4ff73f72cf584c63a980f111bec148f57ac4 (diff) | |
download | podman-ef549235eb63117d10537e6d54adf448e05b40ad.tar.gz podman-ef549235eb63117d10537e6d54adf448e05b40ad.tar.bz2 podman-ef549235eb63117d10537e6d54adf448e05b40ad.zip |
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 <pvanroy@redhat.com>
-rw-r--r-- | cmd/podman/networks/list.go | 26 | ||||
-rw-r--r-- | cmd/podman/networks/network.go | 3 | ||||
-rw-r--r-- | test/e2e/network_test.go | 7 |
3 files changed, 22 insertions, 14 deletions
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()) |