diff --git a/etcdctl/ctlv3/command/check.go b/etcdctl/ctlv3/command/check.go index a2a5ca315..50d6d3bbd 100644 --- a/etcdctl/ctlv3/command/check.go +++ b/etcdctl/ctlv3/command/check.go @@ -130,6 +130,9 @@ func NewCheckPerfCommand() *cobra.Command { cmd.Flags().StringVar(&checkPerfPrefix, "prefix", "/etcdctl-check-perf/", "The prefix for writing the performance check's keys.") cmd.Flags().BoolVar(&autoCompact, "auto-compact", false, "Compact storage with last revision after test is finished.") cmd.Flags().BoolVar(&autoDefrag, "auto-defrag", false, "Defragment storage after test is finished.") + cmd.RegisterFlagCompletionFunc("load", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"small", "medium", "large", "xLarge"}, cobra.ShellCompDirectiveDefault + }) return cmd } diff --git a/etcdctl/ctlv3/command/completion_command.go b/etcdctl/ctlv3/command/completion_command.go new file mode 100644 index 000000000..66a213cd3 --- /dev/null +++ b/etcdctl/ctlv3/command/completion_command.go @@ -0,0 +1,84 @@ +// Copyright 2021 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package command + +import ( + "os" + + "github.com/spf13/cobra" +) + +func NewCompletionCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate completion script", + Long: `To load completions: + +Bash: + + $ source <(etcdctl completion bash) + + # To load completions for each session, execute once: + # Linux: + $ etcdctl completion bash > /etc/bash_completion.d/etcdctl + # macOS: + $ etcdctl completion bash > /usr/local/etc/bash_completion.d/etcdctl + +Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + $ etcdctl completion zsh > "${fpath[1]}/_etcdctl" + + # You will need to start a new shell for this setup to take effect. + +fish: + + $ etcdctl completion fish | source + + # To load completions for each session, execute once: + $ etcdctl completion fish > ~/.config/fish/completions/etcdctl.fish + +PowerShell: + + PS> etcdctl completion powershell | Out-String | Invoke-Expression + + # To load completions for every new session, run: + PS> etcdctl completion powershell > etcdctl.ps1 + # and source this file from your PowerShell profile. +`, + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, + Args: cobra.ExactValidArgs(1), + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) + case "powershell": + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) + } + }, + } + + return cmd +} diff --git a/etcdctl/ctlv3/command/defrag_command.go b/etcdctl/ctlv3/command/defrag_command.go index 42e47cbb9..cd0d8a94a 100644 --- a/etcdctl/ctlv3/command/defrag_command.go +++ b/etcdctl/ctlv3/command/defrag_command.go @@ -36,6 +36,7 @@ func NewDefragCommand() *cobra.Command { } cmd.PersistentFlags().BoolVar(&epClusterEndpoints, "cluster", false, "use all endpoints from the cluster member list") cmd.Flags().StringVar(&defragDataDir, "data-dir", "", "Optional. If present, defragments a data directory not in use by etcd.") + cmd.MarkFlagDirname("data-dir") return cmd } diff --git a/etcdctl/ctlv3/command/get_command.go b/etcdctl/ctlv3/command/get_command.go index c94ac08b9..34edb6fe2 100644 --- a/etcdctl/ctlv3/command/get_command.go +++ b/etcdctl/ctlv3/command/get_command.go @@ -54,6 +54,17 @@ func NewGetCommand() *cobra.Command { cmd.Flags().BoolVar(&getKeysOnly, "keys-only", false, "Get only the keys") cmd.Flags().BoolVar(&getCountOnly, "count-only", false, "Get only the count") cmd.Flags().BoolVar(&printValueOnly, "print-value-only", false, `Only write values when using the "simple" output format`) + + cmd.RegisterFlagCompletionFunc("consistency", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"l", "s"}, cobra.ShellCompDirectiveDefault + }) + cmd.RegisterFlagCompletionFunc("order", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"ASCEND", "DESCEND"}, cobra.ShellCompDirectiveDefault + }) + cmd.RegisterFlagCompletionFunc("sort-by", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"CREATE", "KEY", "MODIFY", "VALUE", "VERSION"}, cobra.ShellCompDirectiveDefault + }) + return cmd } diff --git a/etcdctl/ctlv3/command/put_command.go b/etcdctl/ctlv3/command/put_command.go index 35eb32148..b8a2d38b5 100644 --- a/etcdctl/ctlv3/command/put_command.go +++ b/etcdctl/ctlv3/command/put_command.go @@ -20,7 +20,7 @@ import ( "strconv" "github.com/spf13/cobra" - "go.etcd.io/etcd/client/v3" + clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/pkg/v3/cobrautl" ) diff --git a/etcdctl/ctlv3/command/snapshot_command.go b/etcdctl/ctlv3/command/snapshot_command.go index ee01d50c5..ea52f3a26 100644 --- a/etcdctl/ctlv3/command/snapshot_command.go +++ b/etcdctl/ctlv3/command/snapshot_command.go @@ -88,6 +88,8 @@ func NewSnapshotRestoreCommand() *cobra.Command { cmd.Flags().StringVar(&restorePeerURLs, "initial-advertise-peer-urls", defaultInitialAdvertisePeerURLs, "List of this member's peer URLs to advertise to the rest of the cluster") cmd.Flags().StringVar(&restoreName, "name", defaultName, "Human-readable name for this member") cmd.Flags().BoolVar(&skipHashCheck, "skip-hash-check", false, "Ignore snapshot integrity hash value (required if copied from data directory)") + cmd.MarkFlagDirname("data-dir") + cmd.MarkFlagDirname("wal-dir") return cmd } diff --git a/etcdctl/ctlv3/ctl.go b/etcdctl/ctlv3/ctl.go index d25263c73..8de7a7768 100644 --- a/etcdctl/ctlv3/ctl.go +++ b/etcdctl/ctlv3/ctl.go @@ -53,6 +53,9 @@ func init() { rootCmd.PersistentFlags().StringVarP(&globalFlags.OutputFormat, "write-out", "w", "simple", "set the output format (fields, json, protobuf, simple, table)") rootCmd.PersistentFlags().BoolVar(&globalFlags.IsHex, "hex", false, "print byte strings as hex encoded strings") + rootCmd.RegisterFlagCompletionFunc("write-out", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"fields", "json", "protobuf", "simple", "table"}, cobra.ShellCompDirectiveDefault + }) rootCmd.PersistentFlags().DurationVar(&globalFlags.DialTimeout, "dial-timeout", defaultDialTimeout, "dial timeout for client connections") rootCmd.PersistentFlags().DurationVar(&globalFlags.CommandTimeOut, "command-timeout", defaultCommandTimeOut, "timeout for short running command (excluding dial timeout)") @@ -93,6 +96,7 @@ func init() { command.NewUserCommand(), command.NewRoleCommand(), command.NewCheckCommand(), + command.NewCompletionCommand(), ) } diff --git a/tests/e2e/ctl_v3_completion_test.go b/tests/e2e/ctl_v3_completion_test.go new file mode 100644 index 000000000..eb5423a2b --- /dev/null +++ b/tests/e2e/ctl_v3_completion_test.go @@ -0,0 +1,43 @@ +// Copyright 2021 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package e2e + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCtlV3CompletionBash(t *testing.T) { testShellCompletion(t, "bash") } + +func testShellCompletion(t *testing.T, shellName string) { + BeforeTest(t) + + stdout := new(bytes.Buffer) + completionCmd := exec.Command(ctlBinPath, "completion", shellName) + completionCmd.Stdout = stdout + completionCmd.Stderr = os.Stderr + require.NoError(t, completionCmd.Run()) + + filename := fmt.Sprintf("etcdctl-%s.completion", shellName) + require.NoError(t, os.WriteFile(filename, stdout.Bytes(), 0644)) + + shellCmd := exec.Command(shellName, "-c", "source "+filename) + require.NoError(t, shellCmd.Run()) +}