From b67bf1af4fed0620cf8a21ea2aacf14368b979ec Mon Sep 17 00:00:00 2001 From: Kadin Sayani Date: Wed, 18 Dec 2024 09:27:57 -0700 Subject: [PATCH] lxc/completion: Pre-allocate slices where possible Signed-off-by: Kadin Sayani --- lxc/completion.go | 75 ++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/lxc/completion.go b/lxc/completion.go index 3f7988128f72..d70348c0cea6 100644 --- a/lxc/completion.go +++ b/lxc/completion.go @@ -14,7 +14,6 @@ import ( // cmpClusterGroupNames provides shell completion for cluster group names. // It takes a partial input string and returns a list of matching names along with a shell completion directive. func (g *cmdGlobal) cmpClusterGroupNames(toComplete string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(toComplete) @@ -30,7 +29,7 @@ func (g *cmdGlobal) cmpClusterGroupNames(toComplete string) ([]string, cobra.She return nil, cobra.ShellCompDirectiveError } - results, err = resource.server.GetClusterGroupNames() + results, err := resource.server.GetClusterGroupNames() if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -41,7 +40,6 @@ func (g *cmdGlobal) cmpClusterGroupNames(toComplete string) ([]string, cobra.She // cmpClusterGroups provides shell completion for cluster groups and their remotes. // It takes a partial input string and returns a list of matching cluster groups along with a shell completion directive. func (g *cmdGlobal) cmpClusterGroups(toComplete string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(toComplete) @@ -62,6 +60,8 @@ func (g *cmdGlobal) cmpClusterGroups(toComplete string) ([]string, cobra.ShellCo return nil, cobra.ShellCompDirectiveError } + results := make([]string, 0, len(groups)) + for _, group := range groups { var name string @@ -105,7 +105,7 @@ func (g *cmdGlobal) cmpClusterMemberConfigs(memberName string) ([]string, cobra. return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(member.Config)) for k := range member.Config { results = append(results, k) } @@ -160,6 +160,7 @@ func (g *cmdGlobal) cmpClusterMembers(toComplete string) ([]string, cobra.ShellC return nil, cobra.ShellCompDirectiveError } + results = make([]string, 0, len(members)) for _, member := range members { var name string @@ -185,7 +186,6 @@ func (g *cmdGlobal) cmpClusterMembers(toComplete string) ([]string, cobra.ShellC // cmpImages provides shell completion for image aliases. // It takes a partial input string and returns a list of matching image aliases along with a shell completion directive. func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirective) { - var results []string var remote string cmpDirectives := cobra.ShellCompDirectiveNoFileComp @@ -199,6 +199,7 @@ func (g *cmdGlobal) cmpImages(toComplete string) ([]string, cobra.ShellCompDirec images, _ := remoteServer.GetImages() + results := make([]string, 0, len(images)) for _, image := range images { for _, alias := range image.Aliases { var name string @@ -510,7 +511,7 @@ func (g *cmdGlobal) cmpInstanceDeviceNames(instanceName string) ([]string, cobra return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(instanceNameOnly.Devices)) for k := range instanceNameOnly.Devices { results = append(results, k) } @@ -593,6 +594,7 @@ func (g *cmdGlobal) cmpInstances(toComplete string) ([]string, cobra.ShellCompDi instances, _ := resource.server.GetInstanceNames("") + results = make([]string, 0, len(instances)) for _, instance := range instances { var name string @@ -652,6 +654,7 @@ func (g *cmdGlobal) cmpInstancesAction(toComplete string, action string, flagFor instances, _ := resource.server.GetInstances("") + results = make([]string, 0, len(instances)) for _, instance := range instances { var name string @@ -721,20 +724,19 @@ func (g *cmdGlobal) cmpInstancesAndSnapshots(toComplete string) ([]string, cobra // cmpInstanceNamesFromRemote provides shell completion for instances for a specific remote. // It takes a partial input string and returns a list of matching instances along with a shell completion directive. func (g *cmdGlobal) cmpInstanceNamesFromRemote(toComplete string) ([]string, cobra.ShellCompDirective) { - var results []string - resources, _ := g.ParseServers(toComplete) if len(resources) > 0 { resource := resources[0] containers, _ := resource.server.GetInstanceNames("container") - results = append(results, containers...) vms, _ := resource.server.GetInstanceNames("virtual-machine") - results = append(results, vms...) + results := append(containers, vms...) + + return results, cobra.ShellCompDirectiveNoFileComp } - return results, cobra.ShellCompDirectiveNoFileComp + return nil, cobra.ShellCompDirectiveNoFileComp } // cmpNetworkACLConfigs provides shell completion for network ACL configs. @@ -754,7 +756,7 @@ func (g *cmdGlobal) cmpNetworkACLConfigs(aclName string) ([]string, cobra.ShellC return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(acl.Config)) for k := range acl.Config { results = append(results, k) } @@ -765,7 +767,6 @@ func (g *cmdGlobal) cmpNetworkACLConfigs(aclName string) ([]string, cobra.ShellC // cmpNetworkACLs provides shell completion for network ACL's. // It takes a partial input string and returns a list of matching network ACL's along with a shell completion directive. func (g *cmdGlobal) cmpNetworkACLs(toComplete string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(toComplete) @@ -781,6 +782,7 @@ func (g *cmdGlobal) cmpNetworkACLs(toComplete string) ([]string, cobra.ShellComp return nil, cobra.ShellCompDirectiveError } + results := make([]string, 0, len(acls)) for _, acl := range acls { var name string @@ -805,9 +807,8 @@ func (g *cmdGlobal) cmpNetworkACLs(toComplete string) ([]string, cobra.ShellComp // cmpNetworkACLRuleProperties provides shell completion for network ACL rule properties. // It returns a list of network ACL rules provided by `networkACLRuleJSONStructFieldMap()“ along with a shell completion directive. func (g *cmdGlobal) cmpNetworkACLRuleProperties() ([]string, cobra.ShellCompDirective) { - var results []string - allowedKeys := networkACLRuleJSONStructFieldMap() + results := make([]string, 0, len(allowedKeys)) for key := range allowedKeys { results = append(results, key+"=") } @@ -832,7 +833,7 @@ func (g *cmdGlobal) cmpNetworkForwardConfigs(networkName string, listenAddress s return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(forward.Config)) for k := range forward.Config { results = append(results, k) } @@ -843,7 +844,6 @@ func (g *cmdGlobal) cmpNetworkForwardConfigs(networkName string, listenAddress s // cmpNetworkForwards provides shell completion for network forwards. // It takes a network name and returns a list of network forwards along with a shell completion directive. func (g *cmdGlobal) cmpNetworkForwards(networkName string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(networkName) @@ -865,7 +865,6 @@ func (g *cmdGlobal) cmpNetworkForwards(networkName string) ([]string, cobra.Shel // cmpNetworkLoadBalancers provides shell completion for network load balancers. // It takes a network name and returns a list of network load balancers along with a shell completion directive. func (g *cmdGlobal) cmpNetworkLoadBalancers(networkName string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(networkName) @@ -887,7 +886,6 @@ func (g *cmdGlobal) cmpNetworkLoadBalancers(networkName string) ([]string, cobra // cmpNetworkPeerConfigs provides shell completion for network peer configs. // It takes a network name and peer name, and returns a list of network peer configs along with a shell completion directive. func (g *cmdGlobal) cmpNetworkPeerConfigs(networkName string, peerName string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(networkName) @@ -903,6 +901,7 @@ func (g *cmdGlobal) cmpNetworkPeerConfigs(networkName string, peerName string) ( return nil, cobra.ShellCompDirectiveError } + results := make([]string, 0, len(peer.Config)) for k := range peer.Config { results = append(results, k) } @@ -913,7 +912,6 @@ func (g *cmdGlobal) cmpNetworkPeerConfigs(networkName string, peerName string) ( // cmpNetworkPeers provides shell completion for network peers. // It takes a network name and returns a list of network peers along with a shell completion directive. func (g *cmdGlobal) cmpNetworkPeers(networkName string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(networkName) @@ -948,6 +946,7 @@ func (g *cmdGlobal) cmpNetworks(toComplete string) ([]string, cobra.ShellCompDir return nil, cobra.ShellCompDirectiveError } + results = make([]string, 0, len(networks)) for _, network := range networks { var name string @@ -987,7 +986,7 @@ func (g *cmdGlobal) cmpNetworkConfigs(networkName string) ([]string, cobra.Shell return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(network.Config)) for k := range network.Config { results = append(results, k) } @@ -1012,7 +1011,7 @@ func (g *cmdGlobal) cmpNetworkInstances(networkName string) ([]string, cobra.She return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(network.UsedBy)) for _, i := range network.UsedBy { r := regexp.MustCompile(`/1.0/instances/(.*)`) match := r.FindStringSubmatch(i) @@ -1042,7 +1041,7 @@ func (g *cmdGlobal) cmpNetworkProfiles(networkName string) ([]string, cobra.Shel return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(network.UsedBy)) for _, i := range network.UsedBy { r := regexp.MustCompile(`/1.0/profiles/(.*)`) match := r.FindStringSubmatch(i) @@ -1072,7 +1071,7 @@ func (g *cmdGlobal) cmpNetworkZoneConfigs(zoneName string) ([]string, cobra.Shel return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(zone.Config)) for k := range zone.Config { results = append(results, k) } @@ -1083,7 +1082,6 @@ func (g *cmdGlobal) cmpNetworkZoneConfigs(zoneName string) ([]string, cobra.Shel // cmpNetworkZoneRecordConfigs provides shell completion for network zone record configs. // It takes a zone name and record name, and returns a list of network zone record configs along with a shell completion directive. func (g *cmdGlobal) cmpNetworkZoneRecordConfigs(zoneName string, recordName string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(zoneName) @@ -1099,6 +1097,7 @@ func (g *cmdGlobal) cmpNetworkZoneRecordConfigs(zoneName string, recordName stri return nil, cobra.ShellCompDirectiveError } + results := make([]string, 0, len(peer.Config)) for k := range peer.Config { results = append(results, k) } @@ -1109,7 +1108,6 @@ func (g *cmdGlobal) cmpNetworkZoneRecordConfigs(zoneName string, recordName stri // cmpNetworkZoneRecords provides shell completion for network zone records. // It takes a zone name and returns a list of network zone records along with a shell completion directive. func (g *cmdGlobal) cmpNetworkZoneRecords(zoneName string) ([]string, cobra.ShellCompDirective) { - var results []string cmpDirectives := cobra.ShellCompDirectiveNoFileComp resources, _ := g.ParseServers(zoneName) @@ -1144,6 +1142,7 @@ func (g *cmdGlobal) cmpNetworkZones(toComplete string) ([]string, cobra.ShellCom return nil, cobra.ShellCompDirectiveError } + results = make([]string, 0, len(zones)) for _, project := range zones { var name string @@ -1182,7 +1181,7 @@ func (g *cmdGlobal) cmpProfileConfigs(profileName string) ([]string, cobra.Shell return nil, cobra.ShellCompDirectiveError } - var configs []string + configs := make([]string, 0, len(profile.Config)) for c := range profile.Config { configs = append(configs, c) } @@ -1207,7 +1206,7 @@ func (g *cmdGlobal) cmpProfileDeviceNames(instanceName string) ([]string, cobra. return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(profile.Devices)) for k := range profile.Devices { results = append(results, k) } @@ -1225,8 +1224,7 @@ func (g *cmdGlobal) cmpProfileNamesFromRemote(toComplete string) ([]string, cobr if len(resources) > 0 { resource := resources[0] - profiles, _ := resource.server.GetProfileNames() - results = append(results, profiles...) + results, _ = resource.server.GetProfileNames() } return results, cobra.ShellCompDirectiveNoFileComp @@ -1245,6 +1243,7 @@ func (g *cmdGlobal) cmpProfiles(toComplete string, includeRemotes bool) ([]strin profiles, _ := resource.server.GetProfileNames() + results = make([]string, 0, len(profiles)) for _, profile := range profiles { var name string @@ -1283,7 +1282,7 @@ func (g *cmdGlobal) cmpProjectConfigs(projectName string) ([]string, cobra.Shell return nil, cobra.ShellCompDirectiveError } - var configs []string + configs := make([]string, 0, len(project.Config)) for c := range project.Config { configs = append(configs, c) } @@ -1307,6 +1306,7 @@ func (g *cmdGlobal) cmpProjects(toComplete string) ([]string, cobra.ShellCompDir return nil, cobra.ShellCompDirectiveError } + results = make([]string, 0, len(projects)) for _, project := range projects { var name string @@ -1381,7 +1381,7 @@ func (g *cmdGlobal) cmpStoragePoolConfigs(poolName string) ([]string, cobra.Shel return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(pool.Config)) for k := range pool.Config { results = append(results, k) } @@ -1398,7 +1398,7 @@ func (g *cmdGlobal) cmpStoragePoolWithVolume(toComplete string) ([]string, cobra return nil, compdir } - var results []string + results := make([]string, 0, len(pools)) for _, pool := range pools { if strings.HasSuffix(pool, ":") { results = append(results, pool) @@ -1416,7 +1416,7 @@ func (g *cmdGlobal) cmpStoragePoolWithVolume(toComplete string) ([]string, cobra return nil, compdir } - var results []string + results := make([]string, 0, len(volumes)) for _, volume := range volumes { volName, _ := parseVolume("custom", volume) results = append(results, pool+"/"+volName) @@ -1437,6 +1437,7 @@ func (g *cmdGlobal) cmpStoragePools(toComplete string, noSpace bool) ([]string, storagePools, _ := resource.server.GetStoragePoolNames() + results = make([]string, 0, len(storagePools)) for _, storage := range storagePools { var name string @@ -1486,7 +1487,7 @@ func (g *cmdGlobal) cmpStoragePoolVolumeConfigs(poolName string, volumeName stri return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(volume.Config)) for k := range volume.Config { results = append(results, k) } @@ -1518,7 +1519,7 @@ func (g *cmdGlobal) cmpStoragePoolVolumeInstances(poolName string, volumeName st return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(volume.UsedBy)) for _, i := range volume.UsedBy { r := regexp.MustCompile(`/1.0/instances/(.*)`) match := r.FindStringSubmatch(i) @@ -1555,7 +1556,7 @@ func (g *cmdGlobal) cmpStoragePoolVolumeProfiles(poolName string, volumeName str return nil, cobra.ShellCompDirectiveError } - var results []string + results := make([]string, 0, len(volume.UsedBy)) for _, i := range volume.UsedBy { r := regexp.MustCompile(`/1.0/profiles/(.*)`) match := r.FindStringSubmatch(i)