Skip to content

Commit

Permalink
feat: add autocomplete for cluster set command (#1695)
Browse files Browse the repository at this point in the history
## Description:
`kurtosis cluster set` has no autocomplete; had a go at adding this. 

I wasn't 100% sure how to test, but what I did was use the `__complete`
command then compare its output to the similar `context set` command:

```
√ kurtosis % ./cli/cli/scripts/launch-cli.sh __complete context set ""
cloud-<hash>
default
:36
Completion ended with directive: ShellCompDirectiveNoFileComp, ShellCompDirectiveKeepOrder
√ kurtosis % ./cli/cli/scripts/launch-cli.sh __complete cluster set ""
docker
k3d-k3s-default
minikube
:36
Completion ended with directive: ShellCompDirectiveNoFileComp, ShellCompDirectiveKeepOrder
```

I'm not sure how to test this for real, i.e. hitting tab in my shell to
see autocomplete results. Maybe some config needed on my side?

## Is this change user facing?
YES

## References (if applicable):
<!-- Add relevant Github Issues, Discord threads, or other helpful
information. -->

---------

Co-authored-by: Omar <omar@omar>
  • Loading branch information
omar711 and Omar committed Nov 6, 2023
1 parent 6ab0008 commit d36164d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
26 changes: 17 additions & 9 deletions cli/cli/commands/cluster/ls/ls.go
Expand Up @@ -32,17 +32,10 @@ var LsCmd = &lowlevel.LowlevelKurtosisCommand{
}

func run(ctx context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) error {
kurtosisConfigStore := kurtosis_config.GetKurtosisConfigStore()
configProvider := kurtosis_config.NewKurtosisConfigProvider(kurtosisConfigStore)
kurtosisConfig, err := configProvider.GetOrInitializeConfig()
clusterList, err := GetClusterList()
if err != nil {
return stacktrace.Propagate(err, "Failed to get or initialize Kurtosis configuration")
}
var clusterList []string
for clusterName := range kurtosisConfig.GetKurtosisClusters() {
clusterList = append(clusterList, clusterName)
return stacktrace.Propagate(err, "Failed to get Kurtosis cluster list")
}
sort.Strings(clusterList)

tablePrinter := output_printers.NewTablePrinter(clusterCurrentColumnHeader, clusterNameColumnHeader)

Expand All @@ -60,6 +53,21 @@ func run(ctx context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) e
return nil
}

func GetClusterList() ([]string, error) {
kurtosisConfigStore := kurtosis_config.GetKurtosisConfigStore()
configProvider := kurtosis_config.NewKurtosisConfigProvider(kurtosisConfigStore)
kurtosisConfig, err := configProvider.GetOrInitializeConfig()
if err != nil {
return []string{}, stacktrace.Propagate(err, "Failed to get or initialize Kurtosis configuration")
}
var clusterList []string
for clusterName := range kurtosisConfig.GetKurtosisClusters() {
clusterList = append(clusterList, clusterName)
}
sort.Strings(clusterList)
return clusterList, nil
}

func isCurrentCluster(clusterName string) bool {
clusterSettingStore := kurtosis_cluster_setting.GetKurtosisClusterSettingStore()
currentClusterName, _ := clusterSettingStore.GetClusterSetting()
Expand Down
11 changes: 10 additions & 1 deletion cli/cli/commands/cluster/set/set.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/commands/cluster/ls"
"github.com/kurtosis-tech/kurtosis/cli/cli/defaults"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/engine_manager"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/kurtosis_config_getter"
Expand Down Expand Up @@ -33,7 +34,7 @@ var SetCmd = &lowlevel.LowlevelKurtosisCommand{
IsOptional: false,
DefaultValue: nil,
IsGreedy: false,
ArgCompletionProvider: nil,
ArgCompletionProvider: args.NewManualCompletionsProvider(getCompletions),
ValidationFunc: nil,
},
},
Expand All @@ -42,6 +43,14 @@ var SetCmd = &lowlevel.LowlevelKurtosisCommand{
PostValidationAndRunFunc: nil,
}

func getCompletions(ctx context.Context, flags *flags.ParsedFlags, previousArgs *args.ParsedArgs) ([]string, error) {
clusterList, err := ls.GetClusterList()
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to get Kurtosis cluster list")
}
return clusterList, nil
}

func run(ctx context.Context, flags *flags.ParsedFlags, args *args.ParsedArgs) error {
clusterName, err := args.GetNonGreedyArg(clusterNameArgKey)
if err != nil {
Expand Down

0 comments on commit d36164d

Please sign in to comment.