-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters