diff --git a/lxc/completion.go b/lxc/completion.go index d6009c352e4c..add79129f738 100644 --- a/lxc/completion.go +++ b/lxc/completion.go @@ -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" ) @@ -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 { @@ -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) } } @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 }