From afbb09540dc8b7886f1c448d4e014be8b960de06 Mon Sep 17 00:00:00 2001 From: lmasson Date: Thu, 23 May 2024 15:09:23 +0200 Subject: [PATCH] fix(autocomplete): add basename for custom path --- docs/commands/autocomplete.md | 2 ++ .../namespaces/autocomplete/autocomplete.go | 31 ++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/docs/commands/autocomplete.md b/docs/commands/autocomplete.md index feb97e90d6..95f1777fb1 100644 --- a/docs/commands/autocomplete.md +++ b/docs/commands/autocomplete.md @@ -24,6 +24,7 @@ scw autocomplete install [arg=value ...] | Name | | Description | |------|---|-------------| | shell | | | +| basename | Default: `` | | @@ -45,6 +46,7 @@ scw autocomplete script [arg=value ...] | Name | | Description | |------|---|-------------| | shell | Default: `/bin/bash` | | +| basename | Default: `` | | diff --git a/internal/namespaces/autocomplete/autocomplete.go b/internal/namespaces/autocomplete/autocomplete.go index 9fc2bfd18e..573fc37425 100644 --- a/internal/namespaces/autocomplete/autocomplete.go +++ b/internal/namespaces/autocomplete/autocomplete.go @@ -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": { @@ -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 { @@ -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, @@ -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) } @@ -380,7 +388,8 @@ func autocompleteCompleteZshCommand() *core.Command { } type autocompleteShowArgs struct { - Shell string + Shell string + Basename string } func autocompleteScriptCommand() *core.Command { @@ -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) }