Skip to content

Commit 2a1666b

Browse files
committed
cmd: Added shell completion command
1 parent 3c87722 commit 2a1666b

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

src/cmd/completion.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/containers/toolbox/pkg/utils"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var completionCmd = &cobra.Command{
11+
Use: "completion [bash|zsh|fish|powershell]",
12+
Short: "Generate completion script",
13+
Long: `To load completions:
14+
15+
Bash:
16+
17+
$ source <(toolbox completion bash)
18+
19+
# To load completions for each session, execute once:
20+
# Linux:
21+
$ toolbox completion bash > /etc/bash_completion.d/toolbox
22+
# macOS:
23+
$ toolbox completion bash > /usr/local/etc/bash_completion.d/toolbox
24+
25+
Zsh:
26+
27+
# If shell completion is not already enabled in your environment,
28+
# you will need to enable it. You can execute the following once:
29+
30+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
31+
32+
# To load completions for each session, execute once:
33+
$ toolbox completion zsh > "${fpath[1]}/_toolbox"
34+
35+
# You will need to start a new shell for this setup to take effect.
36+
37+
fish:
38+
39+
$ toolbox completion fish | source
40+
41+
# To load completions for each session, execute once:
42+
$ toolbox completion fish > ~/.config/fish/completions/toolbox.fish
43+
44+
`,
45+
DisableFlagsInUseLine: true,
46+
ValidArgs: []string{"bash", "zsh", "fish"},
47+
Args: cobra.ExactValidArgs(1),
48+
Run: func(cmd *cobra.Command, args []string) {
49+
switch args[0] {
50+
case "bash":
51+
cmd.Root().GenBashCompletion(os.Stdout)
52+
case "zsh":
53+
cmd.Root().GenZshCompletion(os.Stdout)
54+
case "fish":
55+
cmd.Root().GenFishCompletion(os.Stdout, true)
56+
}
57+
},
58+
}
59+
60+
func init() {
61+
rootCmd.AddCommand(completionCmd)
62+
}
63+
64+
func getDistroCompletionNames() []string {
65+
distros := []string{}
66+
supported_distros := utils.GetSupportedDistros()
67+
for key, _ := range supported_distros {
68+
distros = append(distros, key)
69+
}
70+
return distros
71+
}
72+
73+
func getImageCompletionNames() []string {
74+
var image_names []string
75+
if images, err := getImages(); err == nil {
76+
for _, image := range images {
77+
image_names = append(image_names, image.Names[0])
78+
}
79+
}
80+
return image_names
81+
}
82+
83+
func getContainerCompletionNames() []string {
84+
container_names := []string{}
85+
if containers, err := getContainers(); err == nil {
86+
for _, container := range containers {
87+
container_names = append(container_names, container.Names[0])
88+
}
89+
}
90+
return container_names
91+
}

src/cmd/create.go

+18
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ func init() {
9191
"Create a toolbox container for a different operating system release than the host")
9292

9393
createCmd.SetHelpFunc(createHelp)
94+
95+
createCmd.RegisterFlagCompletionFunc(
96+
"distro",
97+
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
98+
return getDistroCompletionNames(), cobra.ShellCompDirectiveNoFileComp
99+
})
100+
101+
createCmd.RegisterFlagCompletionFunc(
102+
"image",
103+
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
104+
// TODO: Create completion depending on args. If the distro arg is present return empty as
105+
// distro and image arguments can't be used together
106+
107+
// TODO: Else, get the list of images available for toolbox locally
108+
109+
return getImageCompletionNames(), cobra.ShellCompDirectiveNoFileComp
110+
})
111+
94112
rootCmd.AddCommand(createCmd)
95113
}
96114

src/cmd/run.go

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func init() {
7070
"Run command inside a toolbox container for a different operating system release than the host")
7171

7272
runCmd.SetHelpFunc(runHelp)
73+
74+
runCmd.RegisterFlagCompletionFunc(
75+
"container",
76+
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
77+
return getContainerCompletionNames(), cobra.ShellCompDirectiveNoFileComp
78+
})
79+
7380
rootCmd.AddCommand(runCmd)
7481
}
7582

src/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_build_wrapper_program = find_program('go-build-wrapper')
55

66
sources = files(
77
'toolbox.go',
8+
'cmd/completion.go',
89
'cmd/create.go',
910
'cmd/enter.go',
1011
'cmd/help.go',

src/pkg/utils/utils.go

+4
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,7 @@ func ShowManual(manual string) error {
762762

763763
return nil
764764
}
765+
766+
func GetSupportedDistros() map[string]Distro {
767+
return supportedDistros
768+
}

0 commit comments

Comments
 (0)