-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion.go
More file actions
95 lines (79 loc) · 2.92 KB
/
completion.go
File metadata and controls
95 lines (79 loc) · 2.92 KB
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
package cmd
import (
"log/slog"
"os"
"github.com/spf13/cobra"
)
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate shell completion script",
Long: `Generate shell completion script for spec-forge.
To load completions:
Bash:
source <(spec-forge completion bash)
# To load completions for each session, execute once:
# Linux:
spec-forge completion bash > /etc/bash_completion.d/spec-forge
# macOS:
spec-forge completion bash > $(brew --prefix)/etc/bash_completion.d/spec-forge
Zsh:
# If shell completion is not already enabled in your environment,
# you will need to enable it. Add the following to your ~/.zshrc:
autoload -Uz compinit
compinit
# Then load completions:
spec-forge completion zsh > "${fpath[1]}/_spec-forge"
# You will need to start a new shell for this setup to take effect.
Fish:
spec-forge completion fish | source
# To load completions for each session, execute once:
spec-forge completion fish > ~/.config/fish/completions/spec-forge.fish
PowerShell:
spec-forge completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
spec-forge completion powershell > spec-forge.ps1
# and source this file from your PowerShell profile.
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: func(cmd *cobra.Command, args []string) error {
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
return cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
return cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
default:
return nil
}
},
}
func init() {
rootCmd.AddCommand(completionCmd)
}
// newCompletionCmd creates a new completion command instance for testing.
func newCompletionCmd() *cobra.Command {
return &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate shell completion script",
Long: completionCmd.Long,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
RunE: completionCmd.RunE,
}
}
// registerCompletion registers shell completion for a flag. Errors are logged
// as warnings since completion is best-effort and should not block startup.
func registerCompletion(cmd *cobra.Command, flag string, completions []string) {
if err := cmd.RegisterFlagCompletionFunc(flag,
cobra.FixedCompletions(completions, cobra.ShellCompDirectiveNoFileComp),
); err != nil {
slog.Warn("failed to register flag completion", "flag", flag, "error", err)
}
}