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

feat(autocomplete): pass partial request to autocompleteFunc #3693

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions internal/core/autocomplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (

// AutoCompleteArgFunc is the function called to complete arguments values.
// It is retrieved from core.ArgSpec.AutoCompleteFunc.
type AutoCompleteArgFunc func(ctx context.Context, prefix string) AutocompleteSuggestions
type AutoCompleteArgFunc func(ctx context.Context, prefix string, request any) AutocompleteSuggestions

// AutoCompleteNode is a node in the AutoComplete Tree.
// An AutoCompleteNode can either represent a command, a subcommand, or a command argument.
Expand Down Expand Up @@ -361,7 +361,7 @@ func AutoCompleteArgValue(ctx context.Context, cmd *Command, argSpec *ArgSpec, a
return nil
}
if argSpec.AutoCompleteFunc != nil {
return argSpec.AutoCompleteFunc(ctx, argValuePrefix)
return argSpec.AutoCompleteFunc(ctx, argValuePrefix, requestFromCompletedArgs(cmd, completedArgs))
}

possibleValues := []string(nil)
Expand Down
2 changes: 1 addition & 1 deletion internal/core/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func testAutocompleteGetCommands() *Commands {
},
{
Name: "size",
AutoCompleteFunc: func(_ context.Context, prefix string) AutocompleteSuggestions {
AutoCompleteFunc: func(_ context.Context, prefix string, _ any) AutocompleteSuggestions {
return []string{regexp.MustCompile("[a-z]").ReplaceAllString(prefix, "")}
},
EnumValues: []string{"S", "M", "L", "XL", "XXL"},
Expand Down
18 changes: 17 additions & 1 deletion internal/core/autocomplete_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func getGlobalFlags(ctx context.Context) []FlagSpec {
}

func AutocompleteProfileName() AutoCompleteArgFunc {
return func(ctx context.Context, prefix string) AutocompleteSuggestions {
return func(ctx context.Context, prefix string, _ any) AutocompleteSuggestions {
res := AutocompleteSuggestions(nil)
configPath := ExtractConfigPath(ctx)
config, err := scw.LoadConfigFromPath(configPath)
Expand Down Expand Up @@ -204,3 +204,19 @@ func listRawArgsLocalities(completedArgs map[string]string, cmd *Command) []stri
}
return listRawArgs
}

func flattenCompleteArgs(completedArgs map[string]string) []string {
rawArgs := make([]string, 0, len(completedArgs))
for arg, value := range completedArgs {
rawArgs = append(rawArgs, arg+value) // "region=" + "nl-ams"
}

return rawArgs
}

func requestFromCompletedArgs(cmd *Command, completedArgs map[string]string) any {
request := reflect.New(cmd.ArgsType).Interface()
_ = args.UnmarshalStruct(flattenCompleteArgs(completedArgs), request)

return request
}
2 changes: 1 addition & 1 deletion internal/namespaces/iam/v1alpha1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func GetCommands() *core.Commands {

// Autocomplete permission set names using IAM API.
cmds.MustFind("iam", "policy", "create").Override(func(c *core.Command) *core.Command {
c.ArgSpecs.GetByName("rules.{index}.permission-set-names.{index}").AutoCompleteFunc = func(ctx context.Context, _ string) core.AutocompleteSuggestions {
c.ArgSpecs.GetByName("rules.{index}.permission-set-names.{index}").AutoCompleteFunc = func(ctx context.Context, _ string, _ any) core.AutocompleteSuggestions {
client := core.ExtractClient(ctx)
api := iam.NewAPI(client)
// TODO: store result in a CLI cache
Expand Down
7 changes: 5 additions & 2 deletions internal/namespaces/instance/v1/custom_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,12 @@ func serverUpdateBuilder(c *core.Command) *core.Command {

func serverGetBuilder(c *core.Command) *core.Command {
// This method is here as a proof of concept before we find the correct way to implement it at larger scale
c.ArgSpecs.GetPositionalArg().AutoCompleteFunc = func(ctx context.Context, prefix string) core.AutocompleteSuggestions {
c.ArgSpecs.GetPositionalArg().AutoCompleteFunc = func(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
req := request.(*instance.GetServerRequest)
api := instance.NewAPI(core.ExtractClient(ctx))
resp, err := api.ListServers(&instance.ListServersRequest{}, scw.WithAllPages())
resp, err := api.ListServers(&instance.ListServersRequest{
Zone: req.Zone,
}, scw.WithAllPages())
if err != nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/namespaces/instance/v1/custom_server_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ func sanitizeVolumeMap(serverName string, volumes map[string]*instance.VolumeSer
// Caching listImage response for shell completion
var completeListImagesCache *marketplace.ListImagesResponse

func instanceServerCreateImageAutoCompleteFunc(ctx context.Context, prefix string) core.AutocompleteSuggestions {
func instanceServerCreateImageAutoCompleteFunc(ctx context.Context, prefix string, _ any) core.AutocompleteSuggestions {
suggestions := core.AutocompleteSuggestions(nil)

client := core.ExtractClient(ctx)
Expand Down
2 changes: 1 addition & 1 deletion internal/namespaces/instance/v1/helpers_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var serverTypes = []string{
"H100-2-80G",
}

func completeServerType(_ context.Context, prefix string) core.AutocompleteSuggestions {
func completeServerType(_ context.Context, prefix string, _ any) core.AutocompleteSuggestions {
suggestions := []string(nil)

for _, serverType := range serverTypes {
Expand Down
14 changes: 10 additions & 4 deletions internal/namespaces/k8s/v1/custom_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,17 @@ func k8sClusterWaitCommand() *core.Command {
// Caching ListClusterTypes response for shell completion
var completeListClusterTypesCache *k8s.ListClusterTypesResponse

func autocompleteClusterType(ctx context.Context, prefix string) core.AutocompleteSuggestions {
func autocompleteClusterType(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
req := request.(*k8s.CreateClusterRequest)
suggestions := core.AutocompleteSuggestions(nil)

client := core.ExtractClient(ctx)
api := k8s.NewAPI(client)

if completeListClusterTypesCache == nil {
res, err := api.ListClusterTypes(&k8s.ListClusterTypesRequest{})
res, err := api.ListClusterTypes(&k8s.ListClusterTypesRequest{
Region: req.Region,
})
if err != nil {
return nil
}
Expand All @@ -467,14 +470,17 @@ func autocompleteClusterType(ctx context.Context, prefix string) core.Autocomple
// Caching ListK8SVersions response for shell completion
var completeListK8SVersionsCache *k8s.ListVersionsResponse

func autocompleteK8SVersion(ctx context.Context, prefix string) core.AutocompleteSuggestions {
func autocompleteK8SVersion(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
req := request.(*k8s.CreateClusterRequest)
suggestions := core.AutocompleteSuggestions(nil)

client := core.ExtractClient(ctx)
api := k8s.NewAPI(client)

if completeListK8SVersionsCache == nil {
res, err := api.ListVersions(&k8s.ListVersionsRequest{})
res, err := api.ListVersions(&k8s.ListVersionsRequest{
Region: req.Region,
})
if err != nil {
return nil
}
Expand Down
21 changes: 17 additions & 4 deletions internal/namespaces/rdb/v1/custom_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,24 @@ var completeListNodeTypeCache *rdb.ListNodeTypesResponse

var completeListEngineCache *rdb.ListDatabaseEnginesResponse

func autoCompleteNodeType(ctx context.Context, prefix string) core.AutocompleteSuggestions {
func autoCompleteNodeType(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
region := scw.Region("")
switch req := request.(type) {
case *rdb.CreateInstanceRequest:
region = req.Region
case *rdb.UpgradeInstanceRequest:
region = req.Region
}

suggestions := core.AutocompleteSuggestions(nil)

client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)

if completeListNodeTypeCache == nil {
res, err := api.ListNodeTypes(&rdb.ListNodeTypesRequest{}, scw.WithAllPages())
res, err := api.ListNodeTypes(&rdb.ListNodeTypesRequest{
Region: region,
}, scw.WithAllPages())
if err != nil {
return nil
}
Expand All @@ -193,13 +203,16 @@ func autoCompleteNodeType(ctx context.Context, prefix string) core.AutocompleteS
return suggestions
}

func autoCompleteDatabaseEngines(ctx context.Context, prefix string) core.AutocompleteSuggestions {
func autoCompleteDatabaseEngines(ctx context.Context, prefix string, request any) core.AutocompleteSuggestions {
req := request.(*rdb.CreateInstanceRequest)
suggestion := core.AutocompleteSuggestions(nil)
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)

if completeListEngineCache == nil {
res, err := api.ListDatabaseEngines(&rdb.ListDatabaseEnginesRequest{}, scw.WithAllPages())
res, err := api.ListDatabaseEngines(&rdb.ListDatabaseEnginesRequest{
Region: req.Region,
}, scw.WithAllPages())
if err != nil {
return nil
}
Expand Down
Loading