|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/containers/toolbox/pkg/utils" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var completionCmd = &cobra.Command{ |
| 12 | + Use: "completion [bash|zsh|fish|powershell]", |
| 13 | + Short: "Generate completion script", |
| 14 | + Long: `To load completions: |
| 15 | +
|
| 16 | +Bash: |
| 17 | +
|
| 18 | + $ source <(toolbox completion bash) |
| 19 | +
|
| 20 | + # To load completions for each session, execute once: |
| 21 | + # Linux: |
| 22 | + $ toolbox completion bash > /etc/bash_completion.d/toolbox |
| 23 | + # macOS: |
| 24 | + $ toolbox completion bash > /usr/local/etc/bash_completion.d/toolbox |
| 25 | +
|
| 26 | +Zsh: |
| 27 | +
|
| 28 | + # If shell completion is not already enabled in your environment, |
| 29 | + # you will need to enable it. You can execute the following once: |
| 30 | +
|
| 31 | + $ echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 32 | +
|
| 33 | + # To load completions for each session, execute once: |
| 34 | + $ toolbox completion zsh > "${fpath[1]}/_toolbox" |
| 35 | +
|
| 36 | + # You will need to start a new shell for this setup to take effect. |
| 37 | +
|
| 38 | +fish: |
| 39 | +
|
| 40 | + $ toolbox completion fish | source |
| 41 | +
|
| 42 | + # To load completions for each session, execute once: |
| 43 | + $ toolbox completion fish > ~/.config/fish/completions/toolbox.fish |
| 44 | +
|
| 45 | +`, |
| 46 | + Hidden: true, |
| 47 | + DisableFlagsInUseLine: true, |
| 48 | + ValidArgs: []string{"bash", "zsh", "fish"}, |
| 49 | + Args: cobra.ExactValidArgs(1), |
| 50 | + Run: func(cmd *cobra.Command, args []string) { |
| 51 | + switch args[0] { |
| 52 | + case "bash": |
| 53 | + cmd.Root().GenBashCompletion(os.Stdout) |
| 54 | + case "zsh": |
| 55 | + cmd.Root().GenZshCompletion(os.Stdout) |
| 56 | + case "fish": |
| 57 | + cmd.Root().GenFishCompletion(os.Stdout, true) |
| 58 | + } |
| 59 | + }, |
| 60 | +} |
| 61 | + |
| 62 | +func init() { |
| 63 | + rootCmd.AddCommand(completionCmd) |
| 64 | +} |
| 65 | + |
| 66 | +func completionEmpty(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 67 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 68 | +} |
| 69 | + |
| 70 | +func completionCommands(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 71 | + commandNames := []string{} |
| 72 | + commands := cmd.Root().Commands() |
| 73 | + for _, command := range commands { |
| 74 | + if strings.Contains(command.Name(), "complet") { |
| 75 | + continue |
| 76 | + } |
| 77 | + commandNames = append(commandNames, command.Name()) |
| 78 | + } |
| 79 | + |
| 80 | + return commandNames, cobra.ShellCompDirectiveNoFileComp |
| 81 | +} |
| 82 | + |
| 83 | +func completionContainerNames(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 84 | + containerNames := []string{} |
| 85 | + if containers, err := getContainers(); err == nil { |
| 86 | + for _, container := range containers { |
| 87 | + containerNames = append(containerNames, container.Names[0]) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if len(containerNames) == 0 { |
| 92 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 93 | + } |
| 94 | + |
| 95 | + return containerNames, cobra.ShellCompDirectiveNoFileComp |
| 96 | +} |
| 97 | + |
| 98 | +func completionContainerNamesFiltered(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 99 | + if cmd.Name() == "enter" && len(args) >= 1 { |
| 100 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 101 | + } |
| 102 | + |
| 103 | + containerNames := []string{} |
| 104 | + if containers, err := getContainers(); err == nil { |
| 105 | + for _, container := range containers { |
| 106 | + skip := false |
| 107 | + for _, arg := range args { |
| 108 | + if container.Names[0] == arg { |
| 109 | + skip = true |
| 110 | + break |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if skip { |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + containerNames = append(containerNames, container.Names[0]) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if len(containerNames) == 0 { |
| 123 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 124 | + } |
| 125 | + |
| 126 | + return containerNames, cobra.ShellCompDirectiveNoFileComp |
| 127 | + |
| 128 | +} |
| 129 | + |
| 130 | +func completionDistroNames(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 131 | + imageFlag := cmd.Flag("image") |
| 132 | + if imageFlag != nil && imageFlag.Changed { |
| 133 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 134 | + } |
| 135 | + |
| 136 | + distros := []string{} |
| 137 | + supportedDistros := utils.GetSupportedDistros() |
| 138 | + for key := range supportedDistros { |
| 139 | + distros = append(distros, key) |
| 140 | + } |
| 141 | + |
| 142 | + return distros, cobra.ShellCompDirectiveNoFileComp |
| 143 | +} |
| 144 | + |
| 145 | +func completionImageNames(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 146 | + distroFlag := cmd.Flag("distro") |
| 147 | + if distroFlag != nil && distroFlag.Changed { |
| 148 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 149 | + } |
| 150 | + |
| 151 | + imageNames := []string{} |
| 152 | + if images, err := getImages(); err == nil { |
| 153 | + for _, image := range images { |
| 154 | + if len(image.Names) > 0 { |
| 155 | + imageNames = append(imageNames, image.Names[0]) |
| 156 | + } else { |
| 157 | + imageNames = append(imageNames, image.ID) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if len(imageNames) == 0 { |
| 163 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 164 | + } |
| 165 | + |
| 166 | + return imageNames, cobra.ShellCompDirectiveNoFileComp |
| 167 | +} |
| 168 | + |
| 169 | +func completionImageNamesFiltered(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 170 | + imageNames := []string{} |
| 171 | + if images, err := getImages(); err == nil { |
| 172 | + for _, image := range images { |
| 173 | + skip := false |
| 174 | + var imageName string |
| 175 | + |
| 176 | + if len(image.Names) > 0 { |
| 177 | + imageName = image.Names[0] |
| 178 | + } else { |
| 179 | + imageName = image.ID |
| 180 | + } |
| 181 | + |
| 182 | + for _, arg := range args { |
| 183 | + if arg == imageName { |
| 184 | + skip = true |
| 185 | + break |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if skip { |
| 190 | + continue |
| 191 | + } |
| 192 | + |
| 193 | + imageNames = append(imageNames, imageName) |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if len(imageNames) == 0 { |
| 198 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 199 | + } |
| 200 | + |
| 201 | + return imageNames, cobra.ShellCompDirectiveNoFileComp |
| 202 | +} |
| 203 | + |
| 204 | +func completionLogLevels(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 205 | + return []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, cobra.ShellCompDirectiveNoFileComp |
| 206 | +} |
0 commit comments