mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Merge pull request #13133 from avorima/shell-completion
etcdctl: add command to generate shell completion
This commit is contained in:
commit
69fadd41b0
@ -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
|
||||
}
|
||||
|
84
etcdctl/ctlv3/command/completion_command.go
Normal file
84
etcdctl/ctlv3/command/completion_command.go
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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(),
|
||||
)
|
||||
}
|
||||
|
||||
|
43
tests/e2e/ctl_v3_completion_test.go
Normal file
43
tests/e2e/ctl_v3_completion_test.go
Normal file
@ -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())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user