1
1
package cli
2
2
3
3
import (
4
- "golang.org/x/xerrors "
4
+ "fmt "
5
5
6
6
"github.com/coder/coder/v2/cli/cliui"
7
7
"github.com/coder/serpent"
@@ -31,26 +31,30 @@ func (*RootCmd) completion() *serpent.Command {
31
31
},
32
32
Handler :func (inv * serpent.Invocation )error {
33
33
if shellName != "" {
34
- shell := completion .ShellByName (shellName ,inv .Command .Parent .Name ())
35
- if shell = =nil {
36
- return xerrors . Errorf ( "unsupported shell %q" , shellName )
34
+ shell , err := completion .ShellByName (shellName ,inv .Command .Parent .Name ())
35
+ if err ! =nil {
36
+ return err
37
37
}
38
+ if printOutput {
39
+ return shell .WriteCompletion (inv .Stdout )
40
+ }
41
+ return installCompletion (inv ,shell )
42
+ }
43
+ shell ,err := completion .DetectUserShell (inv .Command .Parent .Name ())
44
+ if err == nil {
38
45
return installCompletion (inv ,shell )
39
46
}
40
- // shell, err := completion.DetectUserShell(inv.Command.Parent.Name())
41
- // if err == nil {
42
- // return installCompletion(inv, shell)
43
- // }
47
+ // Silently continue to the shell selection if detecting failed.
44
48
choice ,err := cliui .Select (inv , cliui.SelectOptions {
45
- Message :"Select a shell to install completion for" ,
49
+ Message :"Select a shell to install completion for: " ,
46
50
Options :shellOptions .Choices ,
47
51
})
48
52
if err != nil {
49
53
return err
50
54
}
51
- shellChoice := completion .ShellByName (choice ,inv .Command .Parent .Name ())
52
- if shellChoice = =nil {
53
- return xerrors . Errorf ( "unsupported shell %q" , shellName )
55
+ shellChoice , err := completion .ShellByName (choice ,inv .Command .Parent .Name ())
56
+ if err ! =nil {
57
+ return err
54
58
}
55
59
if printOutput {
56
60
return shellChoice .WriteCompletion (inv .Stdout )
@@ -66,13 +70,17 @@ func installCompletion(inv *serpent.Invocation, shell completion.Shell) error {
66
70
// If we can't determine the install path, just print the completion script.
67
71
return shell .WriteCompletion (inv .Stdout )
68
72
}
73
+ action := "appending to"
74
+ if shell .UsesOwnFile () {
75
+ action = "creating"
76
+ }
69
77
choice ,err := cliui .Select (inv , cliui.SelectOptions {
70
78
Options : []string {
71
79
"Confirm" ,
72
80
"Print to terminal" ,
73
81
"Cancel" ,
74
82
},
75
- Message :"Install completion for" + shell .Name ()+ " by appending to " + path + "?" ,
83
+ Message :fmt . Sprintf ( "Install completion for%s by %s %s?" , shell .Name (), action , path ) ,
76
84
HideSearch :true ,
77
85
})
78
86
if err != nil {
@@ -81,7 +89,7 @@ func installCompletion(inv *serpent.Invocation, shell completion.Shell) error {
81
89
if choice == "Cancel" {
82
90
return nil
83
91
}
84
- if choice ! ="Print to terminal" {
92
+ if choice = ="Print to terminal" {
85
93
return shell .WriteCompletion (inv .Stdout )
86
94
}
87
95
return completion .InstallShellCompletion (shell )