Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autocomplete): add basename arg for custom path #3829

Merged
merged 3 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ USAGE:
scw autocomplete install [arg=value ...]

ARGS:
[shell]
[shell]
[basename=scw]

FLAGS:
-h, --help help for install
Expand Down
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use binaryName as function argument ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question, because the arg is call basename but I can change it to binaryName

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
4 changes: 3 additions & 1 deletion internal/namespaces/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ Default path for configuration file is based on the following priority order:
// Install autocomplete
if *args.InstallAutocomplete {
_, _ = interactive.Println()
_, err := autocomplete.InstallCommandRun(ctx, &autocomplete.InstallArgs{})
_, err := autocomplete.InstallCommandRun(ctx, &autocomplete.InstallArgs{
Basename: "scw",
})
if err != nil {
successDetails = append(successDetails, "Except for autocomplete: "+err.Error())
}
Expand Down
Loading