Skip to content

Commit

Permalink
fix(autocomplete): add basename for custom path
Browse files Browse the repository at this point in the history
  • Loading branch information
Laure-di committed May 23, 2024
1 parent e00d962 commit afbb095
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/commands/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ scw autocomplete install [arg=value ...]
| Name | | Description |
|------|---|-------------|
| shell | | |
| basename | Default: `` | |



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



31 changes: 24 additions & 7 deletions internal/namespaces/autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type autocompleteScript struct {

// autocompleteScripts regroups the autocomplete scripts for the different shells
// The key is the path of the shell.
func autocompleteScripts(ctx context.Context) map[string]autocompleteScript {
binaryName := core.ExtractBinaryName(ctx)
func autocompleteScripts(ctx context.Context, basename string) map[string]autocompleteScript {
binaryName := basename
homePath := core.ExtractUserHomeDir(ctx)
return map[string]autocompleteScript{
"bash": {
Expand Down Expand Up @@ -135,7 +135,8 @@ func autocompleteScripts(ctx context.Context) map[string]autocompleteScript {
}

type InstallArgs struct {
Shell string
Shell string
Basename string
}

func autocompleteInstallCommand() *core.Command {
Expand All @@ -149,6 +150,13 @@ func autocompleteInstallCommand() *core.Command {
{
Name: "shell",
},
{
Name: "basename",
Default: func(ctx context.Context) (value string, doc string) {
resp := core.ExtractBinaryName(ctx)
return resp, resp
},
},
},
ArgsType: reflect.TypeOf(InstallArgs{}),
Run: InstallCommandRun,
Expand Down Expand Up @@ -181,8 +189,8 @@ func InstallCommandRun(ctx context.Context, argsI interface{}) (i interface{}, e
}

shellName := filepath.Base(shellArg)

script, exists := autocompleteScripts(ctx)[shellName]
basename := argsI.(*InstallArgs).Basename
script, exists := autocompleteScripts(ctx, basename)[shellName]
if !exists {
return nil, unsupportedShellError(shellName)
}
Expand Down Expand Up @@ -380,7 +388,8 @@ func autocompleteCompleteZshCommand() *core.Command {
}

type autocompleteShowArgs struct {
Shell string
Shell string
Basename string
}

func autocompleteScriptCommand() *core.Command {
Expand All @@ -396,11 +405,19 @@ func autocompleteScriptCommand() *core.Command {
Name: "shell",
Default: core.DefaultValueSetter(os.Getenv("SHELL")),
},
{
Name: "basename",
Default: func(ctx context.Context) (value string, doc string) {
resp := core.ExtractBinaryName(ctx)
return resp, resp
},
},
},
ArgsType: reflect.TypeOf(autocompleteShowArgs{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
shell := filepath.Base(argsI.(*autocompleteShowArgs).Shell)
script, exists := autocompleteScripts(ctx)[shell]
basename := argsI.(*autocompleteShowArgs).Basename
script, exists := autocompleteScripts(ctx, basename)[shell]
if !exists {
return nil, unsupportedShellError(shell)
}
Expand Down

0 comments on commit afbb095

Please sign in to comment.