@@ -8,43 +8,48 @@ import (
88)
99
1010const (
11- completionCommandName = "generate-completion"
12- completionFlagName = "generate-shell-completion"
13- completionFlag = "--" + completionFlagName
11+ completionCommandName = "completion"
12+
13+ // This flag is supposed to only be used by the completion script itself to generate completions on the fly.
14+ completionFlag = "--generate-shell-completion"
1415)
1516
17+ type renderCompletion func (cmd * Command , appName string ) (string , error )
18+
1619var (
1720 //go:embed autocomplete
1821 autoCompleteFS embed.FS
1922
2023 shellCompletions = map [string ]renderCompletion {
21- "bash" : getCompletion ("autocomplete/bash_autocomplete" ),
22- "ps" : getCompletion ("autocomplete/powershell_autocomplete.ps1" ),
23- "zsh" : getCompletion ("autocomplete/zsh_autocomplete" ),
24- "fish" : func (c * Command ) (string , error ) {
24+ "bash" : func (c * Command , appName string ) (string , error ) {
25+ b , err := autoCompleteFS .ReadFile ("autocomplete/bash_autocomplete" )
26+ return fmt .Sprintf (string (b ), appName ), err
27+ },
28+ "zsh" : func (c * Command , appName string ) (string , error ) {
29+ b , err := autoCompleteFS .ReadFile ("autocomplete/zsh_autocomplete" )
30+ return fmt .Sprintf (string (b ), appName ), err
31+ },
32+ "fish" : func (c * Command , appName string ) (string , error ) {
2533 return c .ToFishCompletion ()
2634 },
35+ "pwsh" : func (c * Command , appName string ) (string , error ) {
36+ b , err := autoCompleteFS .ReadFile ("autocomplete/powershell_autocomplete.ps1" )
37+ return string (b ), err
38+ },
2739 }
2840)
2941
30- type renderCompletion func (* Command ) (string , error )
31-
32- func getCompletion (s string ) renderCompletion {
33- return func (c * Command ) (string , error ) {
34- b , err := autoCompleteFS .ReadFile (s )
35- return string (b ), err
36- }
37- }
38-
39- func buildCompletionCommand () * Command {
42+ func buildCompletionCommand (appName string ) * Command {
4043 return & Command {
4144 Name : completionCommandName ,
4245 Hidden : true ,
43- Action : completionCommandAction ,
46+ Action : func (ctx context.Context , cmd * Command ) error {
47+ return printShellCompletion (ctx , cmd , appName )
48+ },
4449 }
4550}
4651
47- func completionCommandAction ( ctx context.Context , cmd * Command ) error {
52+ func printShellCompletion ( _ context.Context , cmd * Command , appName string ) error {
4853 var shells []string
4954 for k := range shellCompletions {
5055 shells = append (shells , k )
@@ -57,14 +62,20 @@ func completionCommandAction(ctx context.Context, cmd *Command) error {
5762 }
5863 s := cmd .Args ().First ()
5964
60- if rc , ok := shellCompletions [s ]; ! ok {
65+ renderCompletion , ok := shellCompletions [s ]
66+ if ! ok {
6167 return Exit (fmt .Sprintf ("unknown shell %s, available shells are %+v" , s , shells ), 1 )
62- } else if c , err := rc (cmd ); err != nil {
68+ }
69+
70+ completionScript , err := renderCompletion (cmd , appName )
71+ if err != nil {
6372 return Exit (err , 1 )
64- } else {
65- if _ , err = cmd .Writer .Write ([]byte (c )); err != nil {
66- return Exit (err , 1 )
67- }
6873 }
74+
75+ _ , err = cmd .Writer .Write ([]byte (completionScript ))
76+ if err != nil {
77+ return Exit (err , 1 )
78+ }
79+
6980 return nil
7081}
0 commit comments