|
| 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 | +} |
0 commit comments