Update cli imports to using local package

Also, rename a bunch of variable to not *shadow* the `opts` package
name.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
Upstream-commit: d7f6563efc
Component: cli
This commit is contained in:
Vincent Demeester
2017-05-15 14:45:19 +02:00
parent fd70e3c064
commit 242da31a23
48 changed files with 471 additions and 481 deletions

View File

@ -1,13 +1,12 @@
package network
import (
"golang.org/x/net/context"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/opts"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type connectOptions struct {
@ -21,7 +20,7 @@ type connectOptions struct {
}
func newConnectCommand(dockerCli *command.DockerCli) *cobra.Command {
opts := connectOptions{
options := connectOptions{
links: opts.NewListOpts(opts.ValidateLink),
}
@ -30,34 +29,34 @@ func newConnectCommand(dockerCli *command.DockerCli) *cobra.Command {
Short: "Connect a container to a network",
Args: cli.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
opts.network = args[0]
opts.container = args[1]
return runConnect(dockerCli, opts)
options.network = args[0]
options.container = args[1]
return runConnect(dockerCli, options)
},
}
flags := cmd.Flags()
flags.StringVar(&opts.ipaddress, "ip", "", "IPv4 address (e.g., 172.30.100.104)")
flags.StringVar(&opts.ipv6address, "ip6", "", "IPv6 address (e.g., 2001:db8::33)")
flags.Var(&opts.links, "link", "Add link to another container")
flags.StringSliceVar(&opts.aliases, "alias", []string{}, "Add network-scoped alias for the container")
flags.StringSliceVar(&opts.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container")
flags.StringVar(&options.ipaddress, "ip", "", "IPv4 address (e.g., 172.30.100.104)")
flags.StringVar(&options.ipv6address, "ip6", "", "IPv6 address (e.g., 2001:db8::33)")
flags.Var(&options.links, "link", "Add link to another container")
flags.StringSliceVar(&options.aliases, "alias", []string{}, "Add network-scoped alias for the container")
flags.StringSliceVar(&options.linklocalips, "link-local-ip", []string{}, "Add a link-local address for the container")
return cmd
}
func runConnect(dockerCli *command.DockerCli, opts connectOptions) error {
func runConnect(dockerCli *command.DockerCli, options connectOptions) error {
client := dockerCli.Client()
epConfig := &network.EndpointSettings{
IPAMConfig: &network.EndpointIPAMConfig{
IPv4Address: opts.ipaddress,
IPv6Address: opts.ipv6address,
LinkLocalIPs: opts.linklocalips,
IPv4Address: options.ipaddress,
IPv6Address: options.ipv6address,
LinkLocalIPs: options.linklocalips,
},
Links: opts.links.GetAll(),
Aliases: opts.aliases,
Links: options.links.GetAll(),
Aliases: options.aliases,
}
return client.NetworkConnect(context.Background(), opts.network, opts.container, epConfig)
return client.NetworkConnect(context.Background(), options.network, options.container, epConfig)
}

View File

@ -5,16 +5,15 @@ import (
"net"
"strings"
"golang.org/x/net/context"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/opts"
runconfigopts "github.com/docker/docker/runconfig/opts"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type createOptions struct {
@ -36,7 +35,7 @@ type createOptions struct {
}
func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
opts := createOptions{
options := createOptions{
driverOpts: *opts.NewMapOpts(nil, nil),
labels: opts.NewListOpts(opts.ValidateEnv),
ipamAux: *opts.NewMapOpts(nil, nil),
@ -48,59 +47,59 @@ func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
Short: "Create a network",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.name = args[0]
return runCreate(dockerCli, opts)
options.name = args[0]
return runCreate(dockerCli, options)
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.driver, "driver", "d", "bridge", "Driver to manage the Network")
flags.VarP(&opts.driverOpts, "opt", "o", "Set driver specific options")
flags.Var(&opts.labels, "label", "Set metadata on a network")
flags.BoolVar(&opts.internal, "internal", false, "Restrict external access to the network")
flags.BoolVar(&opts.ipv6, "ipv6", false, "Enable IPv6 networking")
flags.BoolVar(&opts.attachable, "attachable", false, "Enable manual container attachment")
flags.StringVarP(&options.driver, "driver", "d", "bridge", "Driver to manage the Network")
flags.VarP(&options.driverOpts, "opt", "o", "Set driver specific options")
flags.Var(&options.labels, "label", "Set metadata on a network")
flags.BoolVar(&options.internal, "internal", false, "Restrict external access to the network")
flags.BoolVar(&options.ipv6, "ipv6", false, "Enable IPv6 networking")
flags.BoolVar(&options.attachable, "attachable", false, "Enable manual container attachment")
flags.SetAnnotation("attachable", "version", []string{"1.25"})
flags.BoolVar(&opts.ingress, "ingress", false, "Create swarm routing-mesh network")
flags.BoolVar(&options.ingress, "ingress", false, "Create swarm routing-mesh network")
flags.SetAnnotation("ingress", "version", []string{"1.29"})
flags.StringVar(&opts.ipamDriver, "ipam-driver", "default", "IP Address Management Driver")
flags.StringSliceVar(&opts.ipamSubnet, "subnet", []string{}, "Subnet in CIDR format that represents a network segment")
flags.StringSliceVar(&opts.ipamIPRange, "ip-range", []string{}, "Allocate container ip from a sub-range")
flags.StringSliceVar(&opts.ipamGateway, "gateway", []string{}, "IPv4 or IPv6 Gateway for the master subnet")
flags.StringVar(&options.ipamDriver, "ipam-driver", "default", "IP Address Management Driver")
flags.StringSliceVar(&options.ipamSubnet, "subnet", []string{}, "Subnet in CIDR format that represents a network segment")
flags.StringSliceVar(&options.ipamIPRange, "ip-range", []string{}, "Allocate container ip from a sub-range")
flags.StringSliceVar(&options.ipamGateway, "gateway", []string{}, "IPv4 or IPv6 Gateway for the master subnet")
flags.Var(&opts.ipamAux, "aux-address", "Auxiliary IPv4 or IPv6 addresses used by Network driver")
flags.Var(&opts.ipamOpt, "ipam-opt", "Set IPAM driver specific options")
flags.Var(&options.ipamAux, "aux-address", "Auxiliary IPv4 or IPv6 addresses used by Network driver")
flags.Var(&options.ipamOpt, "ipam-opt", "Set IPAM driver specific options")
return cmd
}
func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
func runCreate(dockerCli *command.DockerCli, options createOptions) error {
client := dockerCli.Client()
ipamCfg, err := consolidateIpam(opts.ipamSubnet, opts.ipamIPRange, opts.ipamGateway, opts.ipamAux.GetAll())
ipamCfg, err := consolidateIpam(options.ipamSubnet, options.ipamIPRange, options.ipamGateway, options.ipamAux.GetAll())
if err != nil {
return err
}
// Construct network create request body
nc := types.NetworkCreate{
Driver: opts.driver,
Options: opts.driverOpts.GetAll(),
Driver: options.driver,
Options: options.driverOpts.GetAll(),
IPAM: &network.IPAM{
Driver: opts.ipamDriver,
Driver: options.ipamDriver,
Config: ipamCfg,
Options: opts.ipamOpt.GetAll(),
Options: options.ipamOpt.GetAll(),
},
CheckDuplicate: true,
Internal: opts.internal,
EnableIPv6: opts.ipv6,
Attachable: opts.attachable,
Ingress: opts.ingress,
Labels: runconfigopts.ConvertKVStringsToMap(opts.labels.GetAll()),
Internal: options.internal,
EnableIPv6: options.ipv6,
Attachable: options.attachable,
Ingress: options.ingress,
Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
}
resp, err := client.NetworkCreate(context.Background(), opts.name, nc)
resp, err := client.NetworkCreate(context.Background(), options.name, nc)
if err != nil {
return err
}

View File

@ -3,14 +3,13 @@ package network
import (
"sort"
"golang.org/x/net/context"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types"
"github.com/docker/docker/opts"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type byNetworkName []types.NetworkResource
@ -27,7 +26,7 @@ type listOptions struct {
}
func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
opts := listOptions{filter: opts.NewFilterOpt()}
options := listOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{
Use: "ls [OPTIONS]",
@ -35,30 +34,30 @@ func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
Short: "List networks",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runList(dockerCli, opts)
return runList(dockerCli, options)
},
}
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")
flags.VarP(&opts.filter, "filter", "f", "Provide filter values (e.g. 'driver=bridge')")
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display network IDs")
flags.BoolVar(&options.noTrunc, "no-trunc", false, "Do not truncate the output")
flags.StringVar(&options.format, "format", "", "Pretty-print networks using a Go template")
flags.VarP(&options.filter, "filter", "f", "Provide filter values (e.g. 'driver=bridge')")
return cmd
}
func runList(dockerCli *command.DockerCli, opts listOptions) error {
func runList(dockerCli *command.DockerCli, options listOptions) error {
client := dockerCli.Client()
options := types.NetworkListOptions{Filters: opts.filter.Value()}
networkResources, err := client.NetworkList(context.Background(), options)
listOptions := types.NetworkListOptions{Filters: options.filter.Value()}
networkResources, err := client.NetworkList(context.Background(), listOptions)
if err != nil {
return err
}
format := opts.format
format := options.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !opts.quiet {
if len(dockerCli.ConfigFile().NetworksFormat) > 0 && !options.quiet {
format = dockerCli.ConfigFile().NetworksFormat
} else {
format = formatter.TableFormatKey
@ -69,8 +68,8 @@ func runList(dockerCli *command.DockerCli, opts listOptions) error {
networksCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewNetworkFormat(format, opts.quiet),
Trunc: !opts.noTrunc,
Format: formatter.NewNetworkFormat(format, options.quiet),
Trunc: !options.noTrunc,
}
return formatter.NetworkWrite(networksCtx, networkResources)
}

View File

@ -3,12 +3,11 @@ package network
import (
"fmt"
"golang.org/x/net/context"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/docker/opts"
"github.com/docker/cli/opts"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type pruneOptions struct {
@ -18,14 +17,14 @@ type pruneOptions struct {
// NewPruneCommand returns a new cobra prune command for networks
func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
opts := pruneOptions{filter: opts.NewFilterOpt()}
options := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{
Use: "prune [OPTIONS]",
Short: "Remove all unused networks",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
output, err := runPrune(dockerCli, opts)
output, err := runPrune(dockerCli, options)
if err != nil {
return err
}
@ -38,8 +37,8 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
}
flags := cmd.Flags()
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
flags.BoolVarP(&options.force, "force", "f", false, "Do not prompt for confirmation")
flags.Var(&options.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
return cmd
}
@ -47,10 +46,10 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
const warning = `WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue?`
func runPrune(dockerCli command.Cli, opts pruneOptions) (output string, err error) {
pruneFilters := command.PruneFilters(dockerCli, opts.filter.Value())
func runPrune(dockerCli command.Cli, options pruneOptions) (output string, err error) {
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
return
}