summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-02-25 06:03:54 +0100
committerGitHub <noreply@github.com>2021-02-25 06:03:54 +0100
commit79e803203239d6f5840b18924e706325272e773d (patch)
tree74d3fbca2c05a1809a0ab14b426e734ff57dd043 /cmd
parent25d81955ec959d0cfa2958edba0935d1257dd787 (diff)
parentef549235eb63117d10537e6d54adf448e05b40ad (diff)
downloadpodman-79e803203239d6f5840b18924e706325272e773d.tar.gz
podman-79e803203239d6f5840b18924e706325272e773d.tar.bz2
podman-79e803203239d6f5840b18924e706325272e773d.zip
Merge pull request #8010 from ParkerVR/format-networks/list
networks/list.go updates for --format
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podman/networks/list.go67
-rw-r--r--cmd/podman/networks/network.go3
2 files changed, 44 insertions, 26 deletions
diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go
index 6d8d35589..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"
@@ -78,13 +76,38 @@ 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)
+ quietOut(responses)
+
+ // JSON output formatting
+ case report.IsJSON(networkListOptions.Format):
+ err = jsonOut(responses)
+
+ // table or other format output
+ default:
+ err = templateOut(responses, cmd)
+ }
+
+ return err
+}
+
+func quietOut(responses []*entities.NetworkListReport) {
+ for _, r := range responses {
+ fmt.Println(r.Name)
+ }
+}
+
+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 +122,16 @@ func networkList(cmd *cobra.Command, args []string) error {
"Labels": "labels",
"ID": "network id",
})
- renderHeaders := true
- row := "{{.Name}}\t{{.Version}}\t{{.Plugins}}\n"
+
+ renderHeaders := report.HasTable(networkListOptions.Format)
+ 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 {{.ID}} {{.Name}} {{.Version}} {{.Plugins}}" '
+ renderHeaders = true
+ row = "{{.ID}}\t{{.Name}}\t{{.Version}}\t{{.Plugins}}\n"
}
- format := parse.EnforceRange(row)
+ format = report.EnforceRange(row)
tmpl, err := template.New("listNetworks").Parse(format)
if err != nil {
@@ -122,34 +148,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 +172,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 {
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",