Skip to content

Commit

Permalink
lxc: Add completions for server keys
Browse files Browse the repository at this point in the history
Signed-off-by: Kadin Sayani <[email protected]>
  • Loading branch information
kadinsayani committed Sep 17, 2024
1 parent 33f0800 commit 22b90b6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
56 changes: 53 additions & 3 deletions lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec
}

func (g *cmdGlobal) cmpInstanceAllKeys(instanceName string) ([]string, cobra.ShellCompDirective) {
var keys []string
cmpDirectives := cobra.ShellCompDirectiveNoFileComp

_, instanceNameOnly, _ := strings.Cut(instanceName, ":")
if instanceNameOnly == "" {
serverKeys, directives := g.cmpServerAllKeys(instanceName)
keys = append(keys, serverKeys...)
cmpDirectives = directives

return keys, cmpDirectives
}

resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
Expand All @@ -219,13 +231,12 @@ func (g *cmdGlobal) cmpInstanceAllKeys(instanceName string) ([]string, cobra.She
resource := resources[0]
client := resource.server

instanceNameOnly, _, err := client.GetInstance(instanceName)
instance, _, err := client.GetInstance(instanceNameOnly)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

var keys []string
instanceType := instanceNameOnly.Type
instanceType := instance.Type

if instanceType == "container" {
for k := range instancetype.InstanceConfigKeysContainer {
Expand All @@ -241,6 +252,41 @@ func (g *cmdGlobal) cmpInstanceAllKeys(instanceName string) ([]string, cobra.She
keys = append(keys, k)
}

return keys, cmpDirectives
}

func (g *cmdGlobal) cmpServerAllKeys(instanceName string) ([]string, cobra.ShellCompDirective) {
var keys []string

resources, err := g.ParseServers(instanceName)
if err != nil || len(resources) == 0 {
return nil, cobra.ShellCompDirectiveError
}

resource := resources[0]
client := resource.server

config, err := client.GetMetadataConfiguration()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}

metadata := (*config)["metadata"].(map[string]any)

Check failure on line 274 in lxc/completion.go

View workflow job for this annotation

GitHub Actions / Code

unchecked-type-assertion: type cast result is unchecked in (*config)["metadata"].(map[string]any) - type assertion will panic if not matched (revive)
configs := metadata["configs"].(map[string]any)

Check failure on line 275 in lxc/completion.go

View workflow job for this annotation

GitHub Actions / Code

unchecked-type-assertion: type cast result is unchecked in metadata["configs"].(map[string]any) - type assertion will panic if not matched (revive)
server := configs["server"].(map[string]any)

Check failure on line 276 in lxc/completion.go

View workflow job for this annotation

GitHub Actions / Code

unchecked-type-assertion: type cast result is unchecked in configs["server"].(map[string]any) - type assertion will panic if not matched (revive)
for _, field := range server {
fieldMap := field.(map[string]any)

Check failure on line 278 in lxc/completion.go

View workflow job for this annotation

GitHub Actions / Code

unchecked-type-assertion: type cast result is unchecked in field.(map[string]any) - type assertion will panic if not matched (revive)
keyArray := fieldMap["keys"].([]any)

Check failure on line 279 in lxc/completion.go

View workflow job for this annotation

GitHub Actions / Code

unchecked-type-assertion: type cast result is unchecked in fieldMap["keys"].([]any) - type assertion will panic if not matched (revive)

for _, keyObj := range keyArray {
keyMap := keyObj.(map[string]any)

Check failure on line 282 in lxc/completion.go

View workflow job for this annotation

GitHub Actions / Code

unchecked-type-assertion: type cast result is unchecked in keyObj.(map[string]any) - type assertion will panic if not matched (revive)

for k := range keyMap {
keys = append(keys, k)
}
}
}

return keys, cobra.ShellCompDirectiveNoFileComp
}

Expand Down Expand Up @@ -937,6 +983,10 @@ func (g *cmdGlobal) cmpRemotes(includeAll bool) ([]string, cobra.ShellCompDirect
continue
}

if remoteName == "local" || rc.Protocol == "simplestreams" {
continue
}

results = append(results, fmt.Sprintf("%s:", remoteName))
}

Expand Down
18 changes: 15 additions & 3 deletions lxc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,11 @@ func (c *cmdConfigGet) command() *cobra.Command {

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpInstances(toComplete)
if strings.Contains(toComplete, ".") {
return c.global.cmpServerAllKeys(toComplete)
} else {

Check failure on line 405 in lxc/config.go

View workflow job for this annotation

GitHub Actions / Code

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return c.global.cmpInstances(toComplete)
}
}

if len(args) == 1 {
Expand Down Expand Up @@ -553,7 +557,11 @@ lxc config set core.https_address=[::]:8443

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpInstances(toComplete)
if strings.Contains(toComplete, ".") {
return c.global.cmpServerAllKeys(toComplete)
} else {

Check failure on line 562 in lxc/config.go

View workflow job for this annotation

GitHub Actions / Code

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return c.global.cmpInstances(toComplete)
}
}

if len(args) == 1 {
Expand Down Expand Up @@ -902,7 +910,11 @@ func (c *cmdConfigUnset) command() *cobra.Command {

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return c.global.cmpInstances(toComplete)
if strings.Contains(toComplete, ".") {
return c.global.cmpServerAllKeys(toComplete)
} else {

Check failure on line 915 in lxc/config.go

View workflow job for this annotation

GitHub Actions / Code

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
return c.global.cmpInstances(toComplete)
}
}

if len(args) == 1 {
Expand Down

0 comments on commit 22b90b6

Please sign in to comment.