mirror of
https://github.com/containers/podman.git
synced 2025-10-19 04:03:23 +08:00
Refactored file
moved --format to templateOut() Rm parse package except JSON Signed-off-by: Parker Van Roy <pvanroy@redhat.com>
This commit is contained in:
@ -38,6 +38,7 @@ var (
|
|||||||
networkListOptions entities.NetworkListOptions
|
networkListOptions entities.NetworkListOptions
|
||||||
filters []string
|
filters []string
|
||||||
noTrunc bool
|
noTrunc bool
|
||||||
|
defaultListRow = "{{.Name}}\t{{.Version}}\t{{.Plugins}}\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
func networkListFlags(flags *pflag.FlagSet) {
|
func networkListFlags(flags *pflag.FlagSet) {
|
||||||
@ -78,13 +79,37 @@ func networkList(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case report.IsJSON(networkListOptions.Format):
|
// quiet means we only print the network names
|
||||||
return jsonOut(responses)
|
|
||||||
case networkListOptions.Quiet:
|
case networkListOptions.Quiet:
|
||||||
// quiet means we only print the network names
|
|
||||||
return quietOut(responses)
|
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))
|
nlprs := make([]ListPrintReports, 0, len(responses))
|
||||||
for _, r := range responses {
|
for _, r := range responses {
|
||||||
nlprs = append(nlprs, ListPrintReports{r})
|
nlprs = append(nlprs, ListPrintReports{r})
|
||||||
@ -99,13 +124,16 @@ func networkList(cmd *cobra.Command, args []string) error {
|
|||||||
"Labels": "labels",
|
"Labels": "labels",
|
||||||
"ID": "network id",
|
"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") {
|
if cmd.Flags().Changed("format") {
|
||||||
renderHeaders = parse.HasTable(networkListOptions.Format)
|
|
||||||
row = report.NormalizeFormat(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)
|
tmpl, err := template.New("listNetworks").Parse(format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -122,34 +150,22 @@ func networkList(cmd *cobra.Command, args []string) error {
|
|||||||
return tmpl.Execute(w, nlprs)
|
return tmpl.Execute(w, nlprs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func quietOut(responses []*entities.NetworkListReport) error {
|
// ListPrintReports returns the network list report
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
type ListPrintReports struct {
|
type ListPrintReports struct {
|
||||||
*entities.NetworkListReport
|
*entities.NetworkListReport
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Version returns the CNI version
|
||||||
func (n ListPrintReports) Version() string {
|
func (n ListPrintReports) Version() string {
|
||||||
return n.CNIVersion
|
return n.CNIVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Plugins returns the CNI Plugins
|
||||||
func (n ListPrintReports) Plugins() string {
|
func (n ListPrintReports) Plugins() string {
|
||||||
return network.GetCNIPlugins(n.NetworkConfigList)
|
return network.GetCNIPlugins(n.NetworkConfigList)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Labels returns any labels added to a Network
|
||||||
func (n ListPrintReports) Labels() string {
|
func (n ListPrintReports) Labels() string {
|
||||||
list := make([]string, 0, len(n.NetworkListReport.Labels))
|
list := make([]string, 0, len(n.NetworkListReport.Labels))
|
||||||
for k, v := range n.NetworkListReport.Labels {
|
for k, v := range n.NetworkListReport.Labels {
|
||||||
@ -158,6 +174,7 @@ func (n ListPrintReports) Labels() string {
|
|||||||
return strings.Join(list, ",")
|
return strings.Join(list, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ID returns the Podman Network ID
|
||||||
func (n ListPrintReports) ID() string {
|
func (n ListPrintReports) ID() string {
|
||||||
length := 12
|
length := 12
|
||||||
if noTrunc {
|
if noTrunc {
|
||||||
|
Reference in New Issue
Block a user