Skip to content

Commit 5ae2c90

Browse files
committed
refactor
1 parent 7b640ff commit 5ae2c90

File tree

6 files changed

+29
-118
lines changed

6 files changed

+29
-118
lines changed

completion.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func DefaultCompletionHandler(inv *Invocation) []string {
1818
allResps = append(allResps, cmd.Name())
1919
}
2020
for _, opt := range inv.Command.Options {
21-
if opt.ValueSource == ValueSourceNone || opt.Value.Type() == "string-array" {
21+
if opt.ValueSource == ValueSourceNone || opt.ValueSource == ValueSourceDefault || opt.Value.Type() == "string-array" {
2222
allResps = append(allResps, "--"+opt.Flag)
2323
}
2424
}

completion/all.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/user"
88
"path/filepath"
99
"strings"
10+
"text/template"
1011

1112
"github.com/coder/serpent"
1213
)
@@ -19,10 +20,10 @@ const (
1920
)
2021

2122
var shellCompletionByName = map[string]func(io.Writer, string) error{
22-
BashShell: GenerateBashCompletion,
23-
FishShell: GenerateFishCompletion,
24-
ZShell: GenerateZshCompletion,
25-
Powershell: GeneratePowershellCompletion,
23+
BashShell: GenerateCompletion(bashCompletionTemplate),
24+
FishShell: GenerateCompletion(fishCompletionTemplate),
25+
ZShell: GenerateCompletion(zshCompletionTemplate),
26+
Powershell: GenerateCompletion(pshCompletionTemplate),
2627
}
2728

2829
func ShellOptions(choice *string) *serpent.Enum {
@@ -72,3 +73,26 @@ func GetUserShell() (string, error) {
7273

7374
return "", fmt.Errorf("default shell not found")
7475
}
76+
77+
func GenerateCompletion(
78+
scriptTemplate string,
79+
) func(io.Writer, string) error {
80+
return func(w io.Writer, rootCmdName string) error {
81+
tmpl, err := template.New("script").Parse(scriptTemplate)
82+
if err != nil {
83+
return fmt.Errorf("parse template: %w", err)
84+
}
85+
86+
err = tmpl.Execute(
87+
w,
88+
map[string]string{
89+
"Name": rootCmdName,
90+
},
91+
)
92+
if err != nil {
93+
return fmt.Errorf("execute template: %w", err)
94+
}
95+
96+
return nil
97+
}
98+
}

completion/bash.go

-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package completion
22

3-
import (
4-
"fmt"
5-
"io"
6-
"text/template"
7-
)
8-
93
const bashCompletionTemplate = `
104
_generate_{{.Name}}_completions() {
115
# Capture the line excluding the command, and everything after the current word
@@ -26,25 +20,3 @@ _generate_{{.Name}}_completions() {
2620
# Setup Bash to use the function for completions for '{{.Name}}'
2721
complete -F _generate_{{.Name}}_completions {{.Name}}
2822
`
29-
30-
func GenerateBashCompletion(
31-
w io.Writer,
32-
rootCmdName string,
33-
) error {
34-
tmpl, err := template.New("bash").Parse(bashCompletionTemplate)
35-
if err != nil {
36-
return fmt.Errorf("parse template: %w", err)
37-
}
38-
39-
err = tmpl.Execute(
40-
w,
41-
map[string]string{
42-
"Name": rootCmdName,
43-
},
44-
)
45-
if err != nil {
46-
return fmt.Errorf("execute template: %w", err)
47-
}
48-
49-
return nil
50-
}

completion/fish.go

-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package completion
22

3-
import (
4-
"fmt"
5-
"io"
6-
"text/template"
7-
)
8-
93
const fishCompletionTemplate = `
104
function _{{.Name}}_completions
115
# Capture the full command line as an array
@@ -17,25 +11,3 @@ end
1711
# Setup Fish to use the function for completions for '{{.Name}}'
1812
complete -c {{.Name}} -f -a '(_{{.Name}}_completions)'
1913
`
20-
21-
func GenerateFishCompletion(
22-
w io.Writer,
23-
rootCmdName string,
24-
) error {
25-
tmpl, err := template.New("fish").Parse(fishCompletionTemplate)
26-
if err != nil {
27-
return fmt.Errorf("parse template: %w", err)
28-
}
29-
30-
err = tmpl.Execute(
31-
w,
32-
map[string]string{
33-
"Name": rootCmdName,
34-
},
35-
)
36-
if err != nil {
37-
return fmt.Errorf("execute template: %w", err)
38-
}
39-
40-
return nil
41-
}

completion/powershell.go

-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package completion
22

3-
import (
4-
"fmt"
5-
"io"
6-
"text/template"
7-
)
8-
93
const pshCompletionTemplate = `
104
115
# Escaping output sourced from:
@@ -37,7 +31,6 @@ $_{{.Name}}_completions = {
3731
$Command = $Command + $Space
3832
}
3933
# Get completions by calling the command with the COMPLETION_MODE environment variable set to 1
40-
"$Command" | Out-File -Append -FilePath "out.log"
4134
$env:COMPLETION_MODE = 1
4235
Invoke-Expression $Command | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
4336
"$_" | _{{.Name}}_escapeStringWithSpecialChars
@@ -47,25 +40,3 @@ $_{{.Name}}_completions = {
4740
4841
Register-ArgumentCompleter -CommandName {{.Name}} -ScriptBlock $_{{.Name}}_completions
4942
`
50-
51-
func GeneratePowershellCompletion(
52-
w io.Writer,
53-
rootCmdName string,
54-
) error {
55-
tmpl, err := template.New("powershell").Parse(pshCompletionTemplate)
56-
if err != nil {
57-
return fmt.Errorf("parse template: %w", err)
58-
}
59-
60-
err = tmpl.Execute(
61-
w,
62-
map[string]string{
63-
"Name": rootCmdName,
64-
},
65-
)
66-
if err != nil {
67-
return fmt.Errorf("execute template: %w", err)
68-
}
69-
70-
return nil
71-
}

completion/zsh.go

-28
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package completion
22

3-
import (
4-
"fmt"
5-
"io"
6-
"text/template"
7-
)
8-
93
const zshCompletionTemplate = `
104
_{{.Name}}_completions() {
115
local -a args completions
@@ -16,25 +10,3 @@ _{{.Name}}_completions() {
1610
1711
compdef _{{.Name}}_completions {{.Name}}
1812
`
19-
20-
func GenerateZshCompletion(
21-
w io.Writer,
22-
rootCmdName string,
23-
) error {
24-
tmpl, err := template.New("zsh").Parse(zshCompletionTemplate)
25-
if err != nil {
26-
return fmt.Errorf("parse template: %w", err)
27-
}
28-
29-
err = tmpl.Execute(
30-
w,
31-
map[string]string{
32-
"Name": rootCmdName,
33-
},
34-
)
35-
if err != nil {
36-
return fmt.Errorf("execute template: %w", err)
37-
}
38-
39-
return nil
40-
}

0 commit comments

Comments
 (0)