2016-06-06 04:28:52 -04:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-06 04:28:52 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/docker/docker/cli/command/formatter"
|
2016-09-13 14:53:11 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-06-06 04:28:52 -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
|
2016-08-04 08:59:51 -04:00
|
|
|
format string
|
2016-09-13 14:53:11 -04:00
|
|
|
filter opts.FilterOpt
|
2016-06-06 04:28:52 -04:00
|
|
|
}
|
|
|
|
|
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-06-06 04:28:52 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-16 10:44:10 -04:00
|
|
|
Use: "ls [OPTIONS]",
|
2016-06-06 04:28:52 -04: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 06:11:14 -04:00
|
|
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display network IDs")
|
2016-06-06 04:28:52 -04:00
|
|
|
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate the output")
|
2016-08-04 08:59:51 -04:00
|
|
|
flags.StringVar(&opts.format, "format", "", "Pretty-print networks using a Go template")
|
2016-09-13 14:53:11 -04:00
|
|
|
flags.VarP(&opts.filter, "filter", "f", "Provide filter values (i.e. 'dangling=true')")
|
2016-06-06 04:28:52 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runList(dockerCli *command.DockerCli, opts listOptions) error {
|
2016-06-06 04:28:52 -04:00
|
|
|
client := dockerCli.Client()
|
2016-09-13 14:53:11 -04:00
|
|
|
options := types.NetworkListOptions{Filters: opts.filter.Value()}
|
2016-06-06 04:28:52 -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-08-04 09:00:00 -04:00
|
|
|
if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !opts.quiet {
|
2016-09-12 16:59:18 -04:00
|
|
|
format = dockerCli.ConfigFile().NetworksFormat
|
2016-08-04 08:59:51 -04:00
|
|
|
} else {
|
2016-09-13 14:21:07 -04:00
|
|
|
format = formatter.TableFormatKey
|
2016-08-04 08:59:51 -04:00
|
|
|
}
|
2016-06-06 04:28:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(byNetworkName(networkResources))
|
2016-08-04 08:59:51 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
networksCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
|
|
|
Format: formatter.NewNetworkFormat(format, opts.quiet),
|
|
|
|
Trunc: !opts.noTrunc,
|
2016-06-06 04:28:52 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return formatter.NetworkWrite(networksCtx, networkResources)
|
2016-06-06 04:28:52 -04:00
|
|
|
}
|