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