Skip to content

Commit

Permalink
cmd: Added shell completion command
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergs committed Jul 21, 2021
1 parent 3c87722 commit 2a1666b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cmd

import (
"os"

"github.com/containers/toolbox/pkg/utils"
"github.com/spf13/cobra"
)

var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: `To load completions:
Bash:
$ source <(toolbox completion bash)
# To load completions for each session, execute once:
# Linux:
$ toolbox completion bash > /etc/bash_completion.d/toolbox
# macOS:
$ toolbox completion bash > /usr/local/etc/bash_completion.d/toolbox
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:
$ toolbox completion zsh > "${fpath[1]}/_toolbox"
# You will need to start a new shell for this setup to take effect.
fish:
$ toolbox completion fish | source
# To load completions for each session, execute once:
$ toolbox completion fish > ~/.config/fish/completions/toolbox.fish
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish"},
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)
}
},
}

func init() {
rootCmd.AddCommand(completionCmd)
}

func getDistroCompletionNames() []string {
distros := []string{}
supported_distros := utils.GetSupportedDistros()
for key, _ := range supported_distros {
distros = append(distros, key)
}
return distros
}

func getImageCompletionNames() []string {
var image_names []string
if images, err := getImages(); err == nil {
for _, image := range images {
image_names = append(image_names, image.Names[0])
}
}
return image_names
}

func getContainerCompletionNames() []string {
container_names := []string{}
if containers, err := getContainers(); err == nil {
for _, container := range containers {
container_names = append(container_names, container.Names[0])
}
}
return container_names
}
18 changes: 18 additions & 0 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ func init() {
"Create a toolbox container for a different operating system release than the host")

createCmd.SetHelpFunc(createHelp)

createCmd.RegisterFlagCompletionFunc(
"distro",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return getDistroCompletionNames(), cobra.ShellCompDirectiveNoFileComp
})

createCmd.RegisterFlagCompletionFunc(
"image",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// TODO: Create completion depending on args. If the distro arg is present return empty as
// distro and image arguments can't be used together

// TODO: Else, get the list of images available for toolbox locally

return getImageCompletionNames(), cobra.ShellCompDirectiveNoFileComp
})

rootCmd.AddCommand(createCmd)
}

Expand Down
7 changes: 7 additions & 0 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func init() {
"Run command inside a toolbox container for a different operating system release than the host")

runCmd.SetHelpFunc(runHelp)

runCmd.RegisterFlagCompletionFunc(
"container",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return getContainerCompletionNames(), cobra.ShellCompDirectiveNoFileComp
})

rootCmd.AddCommand(runCmd)
}

Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_build_wrapper_program = find_program('go-build-wrapper')

sources = files(
'toolbox.go',
'cmd/completion.go',
'cmd/create.go',
'cmd/enter.go',
'cmd/help.go',
Expand Down
4 changes: 4 additions & 0 deletions src/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,7 @@ func ShowManual(manual string) error {

return nil
}

func GetSupportedDistros() map[string]Distro {
return supportedDistros
}

0 comments on commit 2a1666b

Please sign in to comment.