Skip to content

Commit 8a15052

Browse files
authored
feat(vpc-gw): autocomplete vpc-gw gateway-network list private-networ… (#3657)
1 parent 71dc470 commit 8a15052

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

internal/core/autocomplete_utils.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func AutocompleteProfileName() AutoCompleteArgFunc {
8080
}
8181
}
8282

83+
var autocompleteResourceToNamespace = map[string]string{
84+
"private-network": "vpc",
85+
}
86+
8387
// AutocompleteGetArg tries to complete an argument by using the list verb if it exists for the same resource
8488
// It will search for the same field in the response of the list
8589
// Field name will be stripped of the resource name (ex: cluster-id -> id)
@@ -113,21 +117,23 @@ func AutocompleteGetArg(ctx context.Context, cmd *Command, argSpec *ArgSpec, com
113117
argName = strings.TrimLeft(argName, "-")
114118

115119
listCmd, hasList := commands.find(cmd.Namespace, argResource, "list")
120+
116121
if !hasList {
117-
return nil
122+
namespace, ok := autocompleteResourceToNamespace[argResource]
123+
if ok {
124+
listCmd, hasList = commands.find(namespace, argResource, "list")
125+
}
126+
if !hasList {
127+
return nil
128+
}
118129
}
119130

120131
// Build empty arguments and run command
121132
// Has to use interceptor if it exists as ArgsType could be handled by interceptor
122133
listCmdArgs := reflect.New(listCmd.ArgsType).Interface()
123134

124135
// Keep zone and region arguments
125-
listRawArgs := []string(nil)
126-
for arg, value := range completedArgs {
127-
if strings.HasPrefix(arg, "zone") || strings.HasPrefix(arg, "region") {
128-
listRawArgs = append(listRawArgs, arg+value)
129-
}
130-
}
136+
listRawArgs := listRawArgsLocalities(completedArgs, listCmd)
131137

132138
// Apply default arguments
133139
listRawArgs = ApplyDefaultValues(ctx, listCmd.ArgSpecs, listRawArgs)
@@ -177,3 +183,24 @@ func AutocompleteGetArg(ctx context.Context, cmd *Command, argSpec *ArgSpec, com
177183

178184
return values
179185
}
186+
187+
func listRawArgsLocalities(completedArgs map[string]string, cmd *Command) []string {
188+
listRawArgs := []string(nil)
189+
specs := cmd.ArgSpecs
190+
for arg, value := range completedArgs {
191+
if strings.HasPrefix(arg, "zone") {
192+
if specs.GetByName("region") != nil {
193+
zone, _ := scw.ParseZone(value)
194+
region, _ := zone.Region()
195+
listRawArgs = append(listRawArgs, "region="+region.String())
196+
}
197+
if specs.GetByName("zone") != nil {
198+
listRawArgs = append(listRawArgs, arg+value)
199+
}
200+
}
201+
if strings.HasPrefix(arg, "region") && specs.GetByName("region") != nil {
202+
listRawArgs = append(listRawArgs, arg+value)
203+
}
204+
}
205+
return listRawArgs
206+
}

0 commit comments

Comments
 (0)