Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use upstream spf13 cobra #642

Merged
merged 3 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func SetupRootCommand(rootCmd *cobra.Command) {
cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
cobra.AddTemplateFunc("managementSubCommands", managementSubCommands)
cobra.AddTemplateFunc("wrappedFlagUsages", wrappedFlagUsages)
cobra.AddTemplateFunc("useLine", UseLine)

rootCmd.SetUsageTemplate(usageTemplate)
rootCmd.SetHelpTemplate(helpTemplate)
Expand Down Expand Up @@ -97,9 +98,19 @@ func managementSubCommands(cmd *cobra.Command) []*cobra.Command {
return cmds
}

// UseLine returns the usage line for a command. This implementation is different
// from the default Command.UseLine in that it does not add a `[flags]` to the
// of the line.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed a word here "to the ..... of the line"

func UseLine(cmd *cobra.Command) string {
if cmd.HasParent() {
return cmd.Parent().CommandPath() + " " + cmd.Use
}
return cmd.Use
}

var usageTemplate = `Usage:

{{- if not .HasSubCommands}} {{.UseLine}}{{end}}
{{- if not .HasSubCommands}} {{ useLine . }}{{end}}
{{- if .HasSubCommands}} {{ .CommandPath}} COMMAND{{end}}

{{ .Short | trim }}
Expand Down
10 changes: 5 additions & 5 deletions cli/command/checkpoint/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
// NewCheckpointCommand returns the `checkpoint` subcommand (only in experimental)
func NewCheckpointCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "checkpoint",
Short: "Manage checkpoints",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"experimental": "", "version": "1.25"},
Use: "checkpoint",
Short: "Manage checkpoints",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"experimental": "", "version": "1.25"},
}
cmd.AddCommand(
newCreateCommand(dockerCli),
Expand Down
10 changes: 5 additions & 5 deletions cli/command/config/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
// nolint: interfacer
func NewConfigCommand(dockerCli *command.DockerCli) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage Docker configs",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"version": "1.30"},
Use: "config",
Short: "Manage Docker configs",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"version": "1.30"},
}
cmd.AddCommand(
newConfigListCommand(dockerCli),
Expand Down
45 changes: 29 additions & 16 deletions cli/command/container/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ func TestValidateAttach(t *testing.T) {

// nolint: unparam
func parseRun(args []string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
flags := pflag.NewFlagSet("run", pflag.ContinueOnError)
flags.SetOutput(ioutil.Discard)
flags.Usage = nil
copts := addFlags(flags)
flags, copts := setupRunFlags()
if err := flags.Parse(args); err != nil {
return nil, nil, nil, err
}
Expand All @@ -60,6 +57,14 @@ func parseRun(args []string) (*container.Config, *container.HostConfig, *network
return containerConfig.Config, containerConfig.HostConfig, containerConfig.NetworkingConfig, err
}

func setupRunFlags() (*pflag.FlagSet, *containerOptions) {
flags := pflag.NewFlagSet("run", pflag.ContinueOnError)
flags.SetOutput(ioutil.Discard)
flags.Usage = nil
copts := addFlags(flags)
return flags, copts
}

func parseMustError(t *testing.T, args string) {
_, _, _, err := parseRun(strings.Split(args+" ubuntu bash", " "))
assert.Error(t, err, args)
Expand Down Expand Up @@ -227,20 +232,21 @@ func TestParseWithMacAddress(t *testing.T) {
}
}

func TestParseWithMemory(t *testing.T) {
invalidMemory := "--memory=invalid"
_, _, _, err := parseRun([]string{invalidMemory, "img", "cmd"})
testutil.ErrorContains(t, err, invalidMemory)
func TestRunFlagsParseWithMemory(t *testing.T) {
flags, _ := setupRunFlags()
args := []string{"--memory=invalid", "img", "cmd"}
err := flags.Parse(args)
testutil.ErrorContains(t, err, `invalid argument "invalid" for "-m, --memory" flag`)

_, hostconfig := mustParse(t, "--memory=1G")
assert.Equal(t, int64(1073741824), hostconfig.Memory)
}

func TestParseWithMemorySwap(t *testing.T) {
invalidMemory := "--memory-swap=invalid"

_, _, _, err := parseRun([]string{invalidMemory, "img", "cmd"})
testutil.ErrorContains(t, err, invalidMemory)
flags, _ := setupRunFlags()
args := []string{"--memory-swap=invalid", "img", "cmd"}
err := flags.Parse(args)
testutil.ErrorContains(t, err, `invalid argument "invalid" for "--memory-swap" flag`)

_, hostconfig := mustParse(t, "--memory-swap=1G")
assert.Equal(t, int64(1073741824), hostconfig.MemorySwap)
Expand Down Expand Up @@ -365,7 +371,10 @@ func TestParseDevice(t *testing.T) {

func TestParseModes(t *testing.T) {
// pid ko
_, _, _, err := parseRun([]string{"--pid=container:", "img", "cmd"})
flags, copts := setupRunFlags()
args := []string{"--pid=container:", "img", "cmd"}
require.NoError(t, flags.Parse(args))
_, err := parse(flags, copts)
testutil.ErrorContains(t, err, "--pid: invalid PID mode")

// pid ok
Expand All @@ -385,14 +394,18 @@ func TestParseModes(t *testing.T) {
if !hostconfig.UTSMode.Valid() {
t.Fatalf("Expected a valid UTSMode, got %v", hostconfig.UTSMode)
}
}

func TestRunFlagsParseShmSize(t *testing.T) {
// shm-size ko
expectedErr := `invalid argument "a128m" for --shm-size=a128m: invalid size: 'a128m'`
_, _, _, err = parseRun([]string{"--shm-size=a128m", "img", "cmd"})
flags, _ := setupRunFlags()
args := []string{"--shm-size=a128m", "img", "cmd"}
expectedErr := `invalid argument "a128m" for "--shm-size" flag: invalid size: 'a128m'`
err := flags.Parse(args)
testutil.ErrorContains(t, err, expectedErr)

// shm-size ok
_, hostconfig, _, err = parseRun([]string{"--shm-size=128m", "img", "cmd"})
_, hostconfig, _, err := parseRun([]string{"--shm-size=128m", "img", "cmd"})
require.NoError(t, err)
if hostconfig.ShmSize != 134217728 {
t.Fatalf("Expected a valid ShmSize, got %d", hostconfig.ShmSize)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
return nil
},
Tags: map[string]string{"version": "1.25"},
Annotations: map[string]string{"version": "1.25"},
}

flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
return nil
},
Tags: map[string]string{"version": "1.25"},
Annotations: map[string]string{"version": "1.25"},
}

flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command {
}
return nil
},
Tags: map[string]string{"version": "1.25"},
Annotations: map[string]string{"version": "1.25"},
}

flags := cmd.Flags()
Expand Down
10 changes: 5 additions & 5 deletions cli/command/node/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
// NewNodeCommand returns a cobra command for `node` subcommands
func NewNodeCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "node",
Short: "Manage Swarm nodes",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"version": "1.24"},
Use: "node",
Short: "Manage Swarm nodes",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"version": "1.24"},
}
cmd.AddCommand(
newDemoteCommand(dockerCli),
Expand Down
10 changes: 5 additions & 5 deletions cli/command/plugin/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
// nolint: interfacer
func NewPluginCommand(dockerCli *command.DockerCli) *cobra.Command {
cmd := &cobra.Command{
Use: "plugin",
Short: "Manage plugins",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"version": "1.25"},
Use: "plugin",
Short: "Manage plugins",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"version": "1.25"},
}

cmd.AddCommand(
Expand Down
2 changes: 1 addition & 1 deletion cli/command/plugin/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newUpgradeCommand(dockerCli *command.DockerCli) *cobra.Command {
}
return runUpgrade(dockerCli, options)
},
Tags: map[string]string{"version": "1.26"},
Annotations: map[string]string{"version": "1.26"},
}

flags := cmd.Flags()
Expand Down
10 changes: 5 additions & 5 deletions cli/command/secret/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
// nolint: interfacer
func NewSecretCommand(dockerCli *command.DockerCli) *cobra.Command {
cmd := &cobra.Command{
Use: "secret",
Short: "Manage Docker secrets",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"version": "1.25"},
Use: "secret",
Short: "Manage Docker secrets",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"version": "1.25"},
}
cmd.AddCommand(
newSecretListCommand(dockerCli),
Expand Down
10 changes: 5 additions & 5 deletions cli/command/service/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
// nolint: interfacer
func NewServiceCommand(dockerCli *command.DockerCli) *cobra.Command {
cmd := &cobra.Command{
Use: "service",
Short: "Manage services",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"version": "1.24"},
Use: "service",
Short: "Manage services",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"version": "1.24"},
}
cmd.AddCommand(
newCreateCommand(dockerCli),
Expand Down
2 changes: 1 addition & 1 deletion cli/command/service/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newLogsCommand(dockerCli *command.DockerCli) *cobra.Command {
opts.target = args[0]
return runLogs(dockerCli, &opts)
},
Tags: map[string]string{"version": "1.29"},
Annotations: map[string]string{"version": "1.29"},
}

flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cli/command/service/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newRollbackCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runRollback(dockerCli, options, args[0])
},
Tags: map[string]string{"version": "1.31"},
Annotations: map[string]string{"version": "1.31"},
}

flags := cmd.Flags()
Expand Down
7 changes: 4 additions & 3 deletions cli/command/service/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/docker/cli/internal/test/testutil"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
mounttypes "github.com/docker/docker/api/types/mount"
Expand Down Expand Up @@ -165,15 +166,15 @@ func TestUpdateDNSConfig(t *testing.T) {
// IPv6
flags.Set("dns-add", "2001:db8:abc8::1")
// Invalid dns record
assert.EqualError(t, flags.Set("dns-add", "x.y.z.w"), "x.y.z.w is not an ip address")
testutil.ErrorContains(t, flags.Set("dns-add", "x.y.z.w"), "x.y.z.w is not an ip address")

// domains with duplicates
flags.Set("dns-search-add", "example.com")
flags.Set("dns-search-add", "example.com")
flags.Set("dns-search-add", "example.org")
flags.Set("dns-search-rm", "example.org")
// Invalid dns search domain
assert.EqualError(t, flags.Set("dns-search-add", "example$com"), "example$com is not a valid domain")
testutil.ErrorContains(t, flags.Set("dns-search-add", "example$com"), "example$com is not a valid domain")

flags.Set("dns-option-add", "ndots:9")
flags.Set("dns-option-rm", "timeout:3")
Expand Down Expand Up @@ -362,7 +363,7 @@ func TestUpdateHosts(t *testing.T) {
// just hostname should work as well
flags.Set("host-rm", "example.net")
// bad format error
assert.EqualError(t, flags.Set("host-add", "$example.com$"), `bad format for add-host: "$example.com$"`)
testutil.ErrorContains(t, flags.Set("host-add", "$example.com$"), `bad format for add-host: "$example.com$"`)

hosts := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 example.net"}

Expand Down
12 changes: 6 additions & 6 deletions cli/command/stack/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
// nolint: interfacer
func NewStackCommand(dockerCli *command.DockerCli) *cobra.Command {
cmd := &cobra.Command{
Use: "stack",
Short: "Manage Docker stacks",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"version": "1.25"},
Use: "stack",
Short: "Manage Docker stacks",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"version": "1.25"},
}
cmd.AddCommand(
newDeployCommand(dockerCli),
Expand All @@ -31,6 +31,6 @@ func NewTopLevelDeployCommand(dockerCli command.Cli) *cobra.Command {
cmd := newDeployCommand(dockerCli)
// Remove the aliases at the top level
cmd.Aliases = []string{}
cmd.Tags = map[string]string{"experimental": "", "version": "1.25"}
cmd.Annotations = map[string]string{"experimental": "", "version": "1.25"}
return cmd
}
2 changes: 1 addition & 1 deletion cli/command/swarm/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newCACommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runCA(dockerCli, cmd.Flags(), opts)
},
Tags: map[string]string{"version": "1.30"},
Annotations: map[string]string{"version": "1.30"},
}

flags := cmd.Flags()
Expand Down
10 changes: 5 additions & 5 deletions cli/command/swarm/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
// nolint: interfacer
func NewSwarmCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "swarm",
Short: "Manage Swarm",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Tags: map[string]string{"version": "1.24"},
Use: "swarm",
Short: "Manage Swarm",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
Annotations: map[string]string{"version": "1.24"},
}
cmd.AddCommand(
newInitCommand(dockerCli),
Expand Down
1 change: 1 addition & 0 deletions cli/command/swarm/testdata/update-noargs.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Flags:
--cert-expiry duration Validity period for node certificates (ns|us|ms|s|m|h) (default 2160h0m0s)
--dispatcher-heartbeat duration Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
--external-ca external-ca Specifications of one or more certificate signing endpoints
-h, --help help for update
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, yes, think we previously special-cased -h / --help` and hid it, because using that flag will print the same information as is being printed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, I think we might have been relying on a side-effect of an issue that was fixed in cobra to hide this flag, after we migrated to cobra. I think we can restore the old behaviour by hiding this flag. I'll try it out.

--max-snapshots uint Number of additional Raft snapshots to retain
--snapshot-interval uint Number of log entries between Raft snapshots (default 10000)
--task-history-limit int Task history retention limit (default 5)
2 changes: 1 addition & 1 deletion cli/command/system/df.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newDiskUsageCommand(dockerCli *command.DockerCli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runDiskUsage(dockerCli, opts)
},
Tags: map[string]string{"version": "1.25"},
Annotations: map[string]string{"version": "1.25"},
}

flags := cmd.Flags()
Expand Down
2 changes: 1 addition & 1 deletion cli/command/system/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newPruneCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runPrune(dockerCli, options)
},
Tags: map[string]string{"version": "1.25"},
Annotations: map[string]string{"version": "1.25"},
}

flags := cmd.Flags()
Expand Down
Loading