|
4 | 4 |
|
5 | 5 | package cmd
|
6 | 6 |
|
7 |
| -//go:generate sh -c "cd .. && echo \\# generated using go generate in components/gpctl > ../../devops/images/workspaces/gitpod-dev/gpctl_completion; go run main.go completion bash >> ../../devops/images/workspaces/gitpod-dev/gpctl_completion" |
8 |
| - |
9 | 7 | import (
|
10 | 8 | "os"
|
11 | 9 |
|
12 | 10 | "github.com/spf13/cobra"
|
13 | 11 | )
|
14 | 12 |
|
15 |
| -const ( |
16 |
| - bashCompletionFunc = `__gpctl_parse_get_workspace_url() |
17 |
| -{ |
18 |
| - local gpctl_output out |
19 |
| - if gpctl_output=$(gpctl workspaces list --output-template '{{ range .Status }}{{ .Id }} {{ .Spec.Url | trimPrefix "http://" | trimPrefix "https://" }} {{ end }}' | tr " " "\n" 2>/dev/null); then |
20 |
| - out=($(echo "${gpctl_output}" | awk '{print $1}')) |
21 |
| - COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) |
22 |
| - fi |
23 |
| -} |
24 |
| -__gpctl_get_workspace_url() |
25 |
| -{ |
26 |
| - __gpctl_parse_get_workspace_url |
27 |
| - if [[ $? -eq 0 ]]; then |
28 |
| - return 0 |
29 |
| - fi |
30 |
| -} |
31 |
| -__gpctl_parse_get_imagebuild_ref() |
32 |
| -{ |
33 |
| - local gpctl_output out |
34 |
| - if gpctl_output=$(gpctl imagebuilds list -o jsonpath | tr " " "\n" 2>/dev/null); then |
35 |
| - out=($(echo "${gpctl_output}" | awk '{print $1}')) |
36 |
| - COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) ) |
37 |
| - fi |
38 |
| -} |
39 |
| -__gpctl_get_imagebuild_ref() |
40 |
| -{ |
41 |
| - __gpctl_parse_get_imagebuild_ref |
42 |
| - if [[ $? -eq 0 ]]; then |
43 |
| - return 0 |
44 |
| - fi |
45 |
| -} |
46 |
| -__gpctl_custom_func() { |
47 |
| - case ${last_command} in |
48 |
| - gpctl_workspaces_describe | gpctl_workspaces_stop | gpctl_workspaces_snapshot) |
49 |
| - __gpctl_get_workspace_url |
50 |
| - return |
51 |
| - ;; |
52 |
| - gpctl_imagebuilds_logs) |
53 |
| - __gpctl_get_imagebuild_ref |
54 |
| - return |
55 |
| - ;; |
56 |
| - *) |
57 |
| - ;; |
58 |
| - esac |
59 |
| -} |
60 |
| -` |
61 |
| -) |
| 13 | +var completionCmd = &cobra.Command{ |
| 14 | + Use: "completion [bash|zsh|fish|powershell]", |
| 15 | + Short: "Generate completion script", |
| 16 | + Long: `To load completions: |
| 17 | +
|
| 18 | +Bash: |
| 19 | +
|
| 20 | + $ source <(gpctl completion bash) |
| 21 | +
|
| 22 | + # To load completions for each session, execute once: |
| 23 | + # Linux: |
| 24 | + $ gpctl completion bash > /etc/bash_completion.d/gpctl |
| 25 | + # macOS: |
| 26 | + $ gpctl completion bash > /usr/local/etc/bash_completion.d/gpctl |
| 27 | +
|
| 28 | +Zsh: |
| 29 | +
|
| 30 | + # If shell completion is not already enabled in your environment, |
| 31 | + # you will need to enable it. You can execute the following once: |
| 32 | +
|
| 33 | + $ echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 34 | +
|
| 35 | + # To load completions for each session, execute once: |
| 36 | + $ gpctl completion zsh > "${fpath[1]}/_gpctl" |
| 37 | +
|
| 38 | + # You will need to start a new shell for this setup to take effect. |
| 39 | +
|
| 40 | +fish: |
| 41 | +
|
| 42 | + $ gpctl completion fish | source |
| 43 | +
|
| 44 | + # To load completions for each session, execute once: |
| 45 | + $ gpctl completion fish > ~/.config/fish/completions/gpctl.fish |
| 46 | +
|
| 47 | +PowerShell: |
| 48 | +
|
| 49 | + PS> gpctl completion powershell | Out-String | Invoke-Expression |
62 | 50 |
|
63 |
| -// bashCompletionCmd represents the bashCompletion command |
64 |
| -var bashCompletionCmd = &cobra.Command{ |
65 |
| - Use: "completion bash|zsh", |
66 |
| - Short: "Provides shell completion for gpctl. Use with `. <(gpctl completion)`", |
67 |
| - Hidden: true, |
68 |
| - Args: cobra.ExactValidArgs(1), |
69 |
| - ValidArgs: []string{"bash", "zsh"}, |
| 51 | + # To load completions for every new session, run: |
| 52 | + PS> gpctl completion powershell > gpctl.ps1 |
| 53 | + # and source this file from your PowerShell profile. |
| 54 | +`, |
| 55 | + DisableFlagsInUseLine: true, |
| 56 | + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, |
| 57 | + Args: cobra.ExactValidArgs(1), |
70 | 58 | Run: func(cmd *cobra.Command, args []string) {
|
71 | 59 | switch args[0] {
|
72 | 60 | case "bash":
|
73 |
| - rootCmd.GenBashCompletion(os.Stdout) |
| 61 | + cmd.Root().GenBashCompletion(os.Stdout) |
74 | 62 | case "zsh":
|
75 |
| - rootCmd.GenZshCompletion(os.Stdout) |
| 63 | + cmd.Root().GenZshCompletion(os.Stdout) |
| 64 | + case "fish": |
| 65 | + cmd.Root().GenFishCompletion(os.Stdout, true) |
| 66 | + case "powershell": |
| 67 | + cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) |
76 | 68 | }
|
77 | 69 | },
|
78 | 70 | }
|
79 | 71 |
|
80 | 72 | func init() {
|
81 |
| - rootCmd.AddCommand(bashCompletionCmd) |
| 73 | + rootCmd.AddCommand(completionCmd) |
82 | 74 | }
|
0 commit comments