Skip to content

Commit 8d8af37

Browse files
authored
fix(autocomplete): add basename arg for custom path (#3829)
1 parent cb8a9ac commit 8d8af37

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

cmd/scw/testdata/test-all-usage-autocomplete-install-usage.golden

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ USAGE:
66
scw autocomplete install [arg=value ...]
77

88
ARGS:
9-
[shell]
9+
[shell]
10+
[basename=scw]
1011

1112
FLAGS:
1213
-h, --help help for install

docs/commands/autocomplete.md

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ scw autocomplete install [arg=value ...]
2424
| Name | | Description |
2525
|------|---|-------------|
2626
| shell | | |
27+
| basename | Default: `` | |
2728

2829

2930

@@ -45,6 +46,7 @@ scw autocomplete script [arg=value ...]
4546
| Name | | Description |
4647
|------|---|-------------|
4748
| shell | Default: `/bin/bash` | |
49+
| basename | Default: `` | |
4850

4951

5052

internal/namespaces/autocomplete/autocomplete.go

+29-13
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ type autocompleteScript struct {
5151

5252
// autocompleteScripts regroups the autocomplete scripts for the different shells
5353
// The key is the path of the shell.
54-
func autocompleteScripts(ctx context.Context) map[string]autocompleteScript {
55-
binaryName := core.ExtractBinaryName(ctx)
54+
func autocompleteScripts(ctx context.Context, basename string) map[string]autocompleteScript {
5655
homePath := core.ExtractUserHomeDir(ctx)
5756
return map[string]autocompleteScript{
5857
"bash": {
@@ -78,8 +77,8 @@ func autocompleteScripts(ctx context.Context) map[string]autocompleteScript {
7877
return
7978
}
8079
complete -F _%[1]s %[1]s
81-
`, binaryName),
82-
CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=bash)"`, binaryName),
80+
`, basename),
81+
CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=bash)"`, basename),
8382
ShellConfigurationFile: map[string]string{
8483
"darwin": path.Join(homePath, ".bash_profile"),
8584
"linux": path.Join(homePath, ".bashrc"),
@@ -102,8 +101,8 @@ func autocompleteScripts(ctx context.Context) map[string]autocompleteScript {
102101
complete --erase --command %[1]s;
103102
complete --command %[1]s --no-files;
104103
complete --command %[1]s --arguments '(%[1]s autocomplete complete fish -- (commandline) (commandline --cursor) (commandline --current-token) (commandline --current-process --tokenize --cut-at-cursor))';
105-
`, binaryName),
106-
CompleteScript: fmt.Sprintf(`eval (%s autocomplete script shell=fish)`, binaryName),
104+
`, basename),
105+
CompleteScript: fmt.Sprintf(`eval (%s autocomplete script shell=fish)`, basename),
107106
ShellConfigurationFile: map[string]string{
108107
"darwin": path.Join(homePath, ".config/fish/config.fish"),
109108
"linux": path.Join(homePath, ".config/fish/config.fish"),
@@ -124,8 +123,8 @@ func autocompleteScripts(ctx context.Context) map[string]autocompleteScript {
124123
compadd "${opts[@]}" -- "${output[@]}"
125124
}
126125
compdef _%[1]s %[1]s
127-
`, binaryName),
128-
CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=zsh)"`, binaryName),
126+
`, basename),
127+
CompleteScript: fmt.Sprintf(`eval "$(%s autocomplete script shell=zsh)"`, basename),
129128
ShellConfigurationFile: map[string]string{
130129
"darwin": path.Join(homePath, ".zshrc"),
131130
"linux": path.Join(homePath, ".zshrc"),
@@ -135,7 +134,8 @@ func autocompleteScripts(ctx context.Context) map[string]autocompleteScript {
135134
}
136135

137136
type InstallArgs struct {
138-
Shell string
137+
Shell string
138+
Basename string
139139
}
140140

141141
func autocompleteInstallCommand() *core.Command {
@@ -149,6 +149,13 @@ func autocompleteInstallCommand() *core.Command {
149149
{
150150
Name: "shell",
151151
},
152+
{
153+
Name: "basename",
154+
Default: func(ctx context.Context) (value string, doc string) {
155+
resp := core.ExtractBinaryName(ctx)
156+
return resp, resp
157+
},
158+
},
152159
},
153160
ArgsType: reflect.TypeOf(InstallArgs{}),
154161
Run: InstallCommandRun,
@@ -181,8 +188,8 @@ func InstallCommandRun(ctx context.Context, argsI interface{}) (i interface{}, e
181188
}
182189

183190
shellName := filepath.Base(shellArg)
184-
185-
script, exists := autocompleteScripts(ctx)[shellName]
191+
basename := argsI.(*InstallArgs).Basename
192+
script, exists := autocompleteScripts(ctx, basename)[shellName]
186193
if !exists {
187194
return nil, unsupportedShellError(shellName)
188195
}
@@ -380,7 +387,8 @@ func autocompleteCompleteZshCommand() *core.Command {
380387
}
381388

382389
type autocompleteShowArgs struct {
383-
Shell string
390+
Shell string
391+
Basename string
384392
}
385393

386394
func autocompleteScriptCommand() *core.Command {
@@ -396,11 +404,19 @@ func autocompleteScriptCommand() *core.Command {
396404
Name: "shell",
397405
Default: core.DefaultValueSetter(os.Getenv("SHELL")),
398406
},
407+
{
408+
Name: "basename",
409+
Default: func(ctx context.Context) (value string, doc string) {
410+
resp := core.ExtractBinaryName(ctx)
411+
return resp, resp
412+
},
413+
},
399414
},
400415
ArgsType: reflect.TypeOf(autocompleteShowArgs{}),
401416
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
402417
shell := filepath.Base(argsI.(*autocompleteShowArgs).Shell)
403-
script, exists := autocompleteScripts(ctx)[shell]
418+
basename := argsI.(*autocompleteShowArgs).Basename
419+
script, exists := autocompleteScripts(ctx, basename)[shell]
404420
if !exists {
405421
return nil, unsupportedShellError(shell)
406422
}

internal/namespaces/init/init.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,9 @@ Default path for configuration file is based on the following priority order:
245245
// Install autocomplete
246246
if *args.InstallAutocomplete {
247247
_, _ = interactive.Println()
248-
_, err := autocomplete.InstallCommandRun(ctx, &autocomplete.InstallArgs{})
248+
_, err := autocomplete.InstallCommandRun(ctx, &autocomplete.InstallArgs{
249+
Basename: "scw",
250+
})
249251
if err != nil {
250252
successDetails = append(successDetails, "Except for autocomplete: "+err.Error())
251253
}

0 commit comments

Comments
 (0)