summaryrefslogtreecommitdiff
path: root/cmd/podman/networks/list.go
blob: e270622559248d8b37c537169306b507bc992a8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package network

import (
	"encoding/json"
	"fmt"
	"html/template"
	"io"
	"os"
	"strings"

	"github.com/containers/libpod/cmd/podman/validate"

	"github.com/containers/libpod/cmd/podman/registry"
	"github.com/containers/libpod/pkg/domain/entities"
	"github.com/containers/libpod/pkg/network"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

var (
	networklistDescription = `List networks`
	networklistCommand     = &cobra.Command{
		Use:     "ls",
		Args:    validate.NoArgs,
		Short:   "network list",
		Long:    networklistDescription,
		RunE:    networkList,
		Example: `podman network list`,
		Annotations: map[string]string{
			registry.ParentNSRequired: "",
		},
	}
)

var (
	networkListOptions entities.NetworkListOptions
	headers            string = "NAME\tVERSION\tPLUGINS\n"
	defaultListRow     string = "{{.Name}}\t{{.Version}}\t{{.Plugins}}\n"
)

func networkListFlags(flags *pflag.FlagSet) {
	// TODO enable filters based on something
	//flags.StringSliceVarP(&networklistCommand.Filter, "filter", "f",  []string{}, "Pause all running containers")
	flags.StringVarP(&networkListOptions.Format, "format", "f", "", "Pretty-print containers to JSON or using a Go template")
	flags.BoolVarP(&networkListOptions.Quiet, "quiet", "q", false, "display only names")
}

func init() {
	registry.Commands = append(registry.Commands, registry.CliCommand{
		Mode:    []entities.EngineMode{entities.ABIMode},
		Command: networklistCommand,
		Parent:  networkCmd,
	})
	flags := networklistCommand.Flags()
	networkListFlags(flags)
}

func networkList(cmd *cobra.Command, args []string) error {
	var (
		w     io.Writer = os.Stdout
		nlprs []NetworkListPrintReports
	)

	responses, err := registry.ContainerEngine().NetworkList(registry.Context(), networkListOptions)
	if err != nil {
		return err
	}

	// quiet means we only print the network names
	if networkListOptions.Quiet {
		return quietOut(responses)
	}

	if networkListOptions.Format == "json" {
		return jsonOut(responses)
	}

	for _, r := range responses {
		nlprs = append(nlprs, NetworkListPrintReports{r})
	}

	row := networkListOptions.Format
	if len(row) < 1 {
		row = defaultListRow
	}
	if !strings.HasSuffix(row, "\n") {
		row += "\n"
	}

	format := "{{range . }}" + row + "{{end}}"
	if !cmd.Flag("format").Changed {
		format = headers + format
	}
	tmpl, err := template.New("listNetworks").Parse(format)
	if err != nil {
		return err
	}
	if err := tmpl.Execute(w, nlprs); err != nil {
		return err
	}
	if flusher, ok := w.(interface{ Flush() error }); ok {
		return flusher.Flush()
	}
	return nil
}

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
}

type NetworkListPrintReports struct {
	*entities.NetworkListReport
}

func (n NetworkListPrintReports) Version() string {
	return n.CNIVersion
}

func (n NetworkListPrintReports) Plugins() string {
	return network.GetCNIPlugins(n.NetworkConfigList)
}