Skip to content

Commit

Permalink
CLI: Rework cmpInstanceKeys for contextual completions based on ins…
Browse files Browse the repository at this point in the history
…tance type (#14684)

Fixes #14689.

Summary of changes:
- Correctly provides instance config key completions based on instance
type.
  • Loading branch information
tomponline authored Jan 18, 2025
2 parents f45d4fd + f4dc670 commit 01bcd6d
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions lxc/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/spf13/cobra"

"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
)
Expand Down Expand Up @@ -284,6 +285,21 @@ func (g *cmdGlobal) cmpInstanceKeys(instanceName string) ([]string, cobra.ShellC
return nil, cobra.ShellCompDirectiveError
}

instanceTypeAnyKeys := make([]string, 0, len(instancetype.InstanceConfigKeysAny))
for key := range instancetype.InstanceConfigKeysAny {
instanceTypeAnyKeys = append(instanceTypeAnyKeys, key)
}

instanceTypeContainerKeys := make([]string, 0, len(instancetype.InstanceConfigKeysContainer))
for key := range instancetype.InstanceConfigKeysContainer {
instanceTypeContainerKeys = append(instanceTypeContainerKeys, key)
}

instanceTypeVMKeys := make([]string, 0, len(instancetype.InstanceConfigKeysVM))
for key := range instancetype.InstanceConfigKeysVM {
instanceTypeVMKeys = append(instanceTypeVMKeys, key)
}

// Pre-allocate configKeys slice capacity.
keyCount := 0
for _, field := range instanceConfig {
Expand All @@ -294,18 +310,16 @@ func (g *cmdGlobal) cmpInstanceKeys(instanceName string) ([]string, cobra.ShellC

for _, field := range instanceConfig {
for _, key := range field.Keys {
for configKey, configKeyField := range key {
for configKey := range key {
configKey = strings.TrimSuffix(configKey, "*")

// InstanceTypeAny config keys.
if configKeyField.Condition == "" {
if shared.ValueInSlice(configKey, instanceTypeAnyKeys) || shared.StringHasPrefix(configKey, instancetype.ConfigKeyPrefixesAny...) {
configKeys = append(configKeys, configKey)
continue
}

if instanceType == string(api.InstanceTypeContainer) && configKeyField.Condition == "container" {
if instanceType == string(api.InstanceTypeContainer) && (shared.ValueInSlice(configKey, instanceTypeContainerKeys) || shared.StringHasPrefix(configKey, instancetype.ConfigKeyPrefixesContainer...)) {
configKeys = append(configKeys, configKey)
} else if instanceType == string(api.InstanceTypeVM) && configKeyField.Condition == "virtual machine" {
} else if instanceType == string(api.InstanceTypeVM) && shared.ValueInSlice(configKey, instanceTypeVMKeys) {
configKeys = append(configKeys, configKey)
}
}
Expand Down Expand Up @@ -1536,9 +1550,9 @@ func (g *cmdGlobal) cmpStoragePoolVolumeConfigs(poolName string, volumeName stri
resource := resources[0]
client := resource.server

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
_, pool, found := strings.Cut(poolName, ":")
if !found {
pool = poolName
}

volName, volType := parseVolume("custom", volumeName)
Expand Down Expand Up @@ -1568,9 +1582,9 @@ func (g *cmdGlobal) cmpStoragePoolVolumeInstances(poolName string, volumeName st
resource := resources[0]
client := resource.server

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
_, pool, found := strings.Cut(poolName, ":")
if !found {
pool = poolName
}

volName, volType := parseVolume("custom", volumeName)
Expand Down Expand Up @@ -1605,9 +1619,9 @@ func (g *cmdGlobal) cmpStoragePoolVolumeProfiles(poolName string, volumeName str
resource := resources[0]
client := resource.server

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
_, pool, found := strings.Cut(poolName, ":")
if !found {
pool = poolName
}

volName, volType := parseVolume("custom", volumeName)
Expand Down Expand Up @@ -1642,9 +1656,9 @@ func (g *cmdGlobal) cmpStoragePoolVolumeSnapshots(poolName string, volumeName st
resource := resources[0]
client := resource.server

var pool = poolName
if strings.Contains(poolName, ":") {
pool = strings.Split(poolName, ":")[1]
_, pool, found := strings.Cut(poolName, ":")
if !found {
pool = poolName
}

volName, volType := parseVolume("custom", volumeName)
Expand All @@ -1670,8 +1684,8 @@ func (g *cmdGlobal) cmpStoragePoolVolumes(poolName string, volumeTypes ...string
resource := resources[0]
client := resource.server

_, pool, _ := strings.Cut(poolName, ":")
if pool == "" {
_, pool, found := strings.Cut(poolName, ":")
if !found {
pool = poolName
}

Expand Down

0 comments on commit 01bcd6d

Please sign in to comment.