-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcompletion.go
103 lines (82 loc) · 3.02 KB
/
completion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package cmd
import (
"fmt"
"os"
"slices"
"strings"
"github.com/spf13/cobra"
)
// generalComDesc describes the long description for the completion command
const generalComDesc = `
Generate the autocompletion script for medusa for the specific shell.
Bash:
To load completions in the current shell session:
source <(medusa completion bash)
To load completions for every new session, execute once:
- Linux:
medusa completion bash > /etc/bash_completion.d/medusa
- macOS:
medusa completion bash > /usr/local/etc/bash_completion.d/medusa
Zsh:
To load completions in the current shell session:
source <(medusa completion zsh)
To load completions for every new session, execute once:
medusa completion zsh > "${fpath[1]}/_medusa"
PowerShell:
To load completions in the current shell session:
PS> medusa completion powershell | Out-String | Invoke-Expression
To load completions for every new session, run:
PS> medusa completion powershell > medusa.ps1
and source this file from your PowerShell profile.
`
var supportedShells = []string{"bash", "zsh", "powershell"}
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion <shell>",
Short: "Generate the autocompletion script for medusa for the specific shell",
Long: generalComDesc,
Args: cmdValidateCompletionArgs,
RunE: cmdRunCompletion,
SilenceUsage: true,
SilenceErrors: true,
}
func init() {
rootCmd.AddCommand(completionCmd)
}
// cmdValidateCompletionArgs validates CLI arguments
func cmdValidateCompletionArgs(cmd *cobra.Command, args []string) error {
// Make sure we have exactly 1 argument
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
err = fmt.Errorf("completion requires only 1 shell argument (options: %s)", strings.Join(supportedShells, ", "))
cmdLogger.Error("Failed to validate args for completion command", err)
return err
}
// Make sure that the shell is a supported type
if contains := slices.Contains(supportedShells, args[0]); !contains {
err := fmt.Errorf("%s is not a supported shell", args[0])
cmdLogger.Error("Failed to validate args for completion command", err)
return err
}
return nil
}
// cmdRunCompletion executes the completion CLI command
func cmdRunCompletion(cmd *cobra.Command, args []string) error {
// NOTE: Please be aware that if the supported shells changes, then this switch statement must also change
var err error
switch args[0] {
case "bash":
err = cmd.Root().GenBashCompletionV2(os.Stdout, true)
case "zsh":
err = cmd.Root().GenZshCompletion(os.Stdout)
case "powershell":
err = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
// We are throwing a panic here because our validation function should have handled this and something is wrong.
cmdLogger.Panic("Failed to run the completion command", fmt.Errorf("%s is not a supported shell type", args[0]))
}
// Log an error if we encountered one
if err != nil {
cmdLogger.Error("Failed to run the completion command", err)
}
return err
}