2016-06-06 08:28:52 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-06 08:28:52 +00:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 17:11:39 +00:00
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-09-13 18:53:11 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2016-06-06 08:28:52 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type byNetworkName []types.NetworkResource
|
|
|
|
|
|
|
|
func (r byNetworkName) Len() int { return len(r) }
|
|
|
|
func (r byNetworkName) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byNetworkName) Less(i, j int) bool { return r[i].Name < r[j].Name }
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
|
|
|
noTrunc bool
|
2016-08-04 12:59:51 +00:00
|
|
|
format string
|
2016-09-13 18:53:11 +00:00
|
|
|
filter opts.FilterOpt
|
2016-06-06 08:28:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 17:11:39 +00:00
|
|
|
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-09-13 18:53:11 +00:00
|
|
|
opts := listOptions{filter: opts.NewFilterOpt()}
|
2016-06-06 08:28:52 +00:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-16 14:44:10 +00:00
|
|
|
Use: "ls [OPTIONS]",
|
2016-06-06 08:28:52 +00:00
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List networks",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runList(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2016-09-05 10:11:14 +00:00
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display network IDs")
|
2016-06-06 08:28:52 +00:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate the output")
|
2016-08-04 12:59:51 +00:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print networks using a Go template")
|
2016-09-13 18:53:11 +00:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Provide filter values (i.e. 'dangling=true')")
|
2016-06-06 08:28:52 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 17:11:39 +00:00
|
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
2016-06-06 08:28:52 +00:00
|
|
|
client := dockerCli.Client()
|
2016-09-13 18:53:11 +00:00
|
|
|
options := types.NetworkListOptions{Filters: opts.filter.Value()}
|
2016-06-06 08:28:52 +00:00
|
|
|
networkResources, err := client.NetworkList(context.Background(), options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:59:18 +00:00
|
|
|
format := opts.format
|
|
|
|
if len(format) == 0 {
|
2016-08-04 13:00:00 +00:00
|
|
|
if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !opts.quiet {
|
2016-09-12 20:59:18 +00:00
|
|
|
format = dockerCli.ConfigFile().NetworksFormat
|
2016-08-04 12:59:51 +00:00
|
|
|
} else {
|
2016-09-13 18:21:07 +00:00
|
|
|
format = formatter.TableFormatKey
|
2016-08-04 12:59:51 +00:00
|
|
|
}
|
2016-06-06 08:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(byNetworkName(networkResources))
|
2016-08-04 12:59:51 +00:00
|
|
|
|
2016-09-12 20:59:18 +00:00
|
|
|
networksCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewNetworkFormat(format, opts.quiet),
|
|
|
|
Trunc: !opts.noTrunc,
|
2016-06-06 08:28:52 +00:00
|
|
|
}
|
2016-09-12 20:59:18 +00:00
|
|
|
return formatter.NetworkWrite(networksCtx, networkResources)
|
2016-06-06 08:28:52 +00:00
|
|
|
}
|