Skip to content

Commit

Permalink
add of doc and update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Laure-di committed May 23, 2024
1 parent 71eadfe commit ee47af4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 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
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

0 comments on commit ee47af4

Please sign in to comment.