summaryrefslogtreecommitdiff
path: root/cmd/podman/shell_completion_test.go
diff options
context:
space:
mode:
authorPaul Holzinger <paul.holzinger@web.de>2020-05-31 17:35:59 +0200
committerPaul Holzinger <paul.holzinger@web.de>2020-11-12 11:38:31 +0100
commitb5d1d89a377e38762a75c2042dcc50a370ba6707 (patch)
treeeebedb62f7aeeb17cd6fdbe9317a59effd02e96d /cmd/podman/shell_completion_test.go
parentdf4bf5c584f264bdefef5cb30d85c036260eff8f (diff)
downloadpodman-b5d1d89a377e38762a75c2042dcc50a370ba6707.tar.gz
podman-b5d1d89a377e38762a75c2042dcc50a370ba6707.tar.bz2
podman-b5d1d89a377e38762a75c2042dcc50a370ba6707.zip
Add shell completion with cobra
Allow automatic generation for shell completion scripts with the internal cobra functions (requires v1.0.0+). This should replace the handwritten completion scripts and even adds support for fish. With this approach it is less likley that completions and code are out of sync. We can now create the scripts with - podman completion bash - podman completion zsh - podman completion fish To test the completion run: source <(podman completion bash) The same works for podman-remote and podman --remote and it will complete your remote containers/images with the correct endpoints values from --url/--connection. The completion logic is written in go and provided by the cobra library. The completion functions lives in `cmd/podman/completion/completion.go`. The unit test at cmd/podman/shell_completion_test.go checks if each command and flag has an autocompletion function set. This prevents that commands and flags have no shell completion set. This commit does not replace the current autocompletion scripts. Closes #6440 Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Diffstat (limited to 'cmd/podman/shell_completion_test.go')
-rw-r--r--cmd/podman/shell_completion_test.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/cmd/podman/shell_completion_test.go b/cmd/podman/shell_completion_test.go
new file mode 100644
index 000000000..d2b500b09
--- /dev/null
+++ b/cmd/podman/shell_completion_test.go
@@ -0,0 +1,71 @@
+/*
+ The purpose of this test is to keep a consistent
+ and great shell autocompletion experience.
+
+ This test ensures that each command and flag has a shell completion
+ function set. (except boolean, hidden and deprecated flags)
+
+ Shell completion functions are defined in:
+ - "github.com/containers/podman/v2/cmd/podman/common/completion.go"
+ - "github.com/containers/common/pkg/completion"
+ and are called Autocomplete...
+
+ To apply such function to a command use the ValidArgsFunction field.
+ To apply such function to a flag use cmd.RegisterFlagCompletionFunc(name,func)
+
+ If there are any questions/problems please tag Luap99.
+*/
+
+package main
+
+import (
+ "testing"
+
+ "github.com/spf13/cobra"
+ "github.com/spf13/pflag"
+)
+
+func TestShellCompletionFunctions(t *testing.T) {
+
+ rootCmd := parseCommands()
+ checkCommand(t, rootCmd)
+
+}
+
+func checkCommand(t *testing.T, cmd *cobra.Command) {
+
+ if cmd.HasSubCommands() {
+ for _, childCmd := range cmd.Commands() {
+ checkCommand(t, childCmd)
+ }
+
+ // if not check if completion for that command is provided
+ } else if cmd.ValidArgsFunction == nil && cmd.ValidArgs == nil {
+ t.Errorf("%s command has no shell completion function set", cmd.CommandPath())
+ }
+
+ // loop over all local flags
+ cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
+
+ // an error means that there is a completion function for this flag
+ err := cmd.RegisterFlagCompletionFunc(flag.Name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
+ return nil, cobra.ShellCompDirectiveDefault
+ })
+
+ switch {
+ case flag.Value.Type() == "bool" && err != nil:
+ // make sure bool flags don't have a completion function
+ t.Errorf(`%s --%s is a bool flag but has a shell completion function set.
+You have to remove this shell completion function.`, cmd.CommandPath(), flag.Name)
+ return
+
+ case flag.Value.Type() == "bool" || flag.Hidden || len(flag.Deprecated) > 0:
+ // skip bool, hidden and deprecated flags
+ return
+
+ case err == nil:
+ // there is no shell completion function
+ t.Errorf("%s --%s flag has no shell completion function set", cmd.CommandPath(), flag.Name)
+ }
+ })
+}