reduce usage of string split
This commit is contained in:
24
app/model.go
24
app/model.go
@@ -253,31 +253,31 @@ func (instance *Instance) RebuildBoot() {
|
|||||||
eligibleBoot[string(k)] = true
|
eligibleBoot[string(k)] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
x := strings.Split(instance.configBoot, "order=") // should be a;b;c;d ...
|
bootOrder := PVEObjectStringToMap(instance.configBoot)["order"]
|
||||||
if len(x) == 2 {
|
|
||||||
y := strings.Split(x[1], ";")
|
if len(bootOrder) != 0 {
|
||||||
for _, bootTarget := range y {
|
for bootTarget := range strings.SplitSeq(bootOrder, ";") { // iterate over elements selected for boot, add them to Enabled, and remove them from eligible boot target
|
||||||
_, isEligible := eligibleBoot[bootTarget]
|
_, isEligible := eligibleBoot[bootTarget]
|
||||||
if val, ok := instance.Volumes[VolumeID(bootTarget)]; ok && isEligible { // if the item is eligible and is in volumes
|
if val, ok := instance.Volumes[VolumeID(bootTarget)]; ok && isEligible { // if the item is eligible and is in volumes
|
||||||
instance.Boot.Enabled = append(instance.Boot.Enabled, val)
|
instance.Boot.Enabled = append(instance.Boot.Enabled, val)
|
||||||
eligibleBoot[bootTarget] = false
|
delete(eligibleBoot, bootTarget)
|
||||||
} else if val, ok := instance.Nets[NetID(bootTarget)]; ok && isEligible { // if the item is eligible and is in nets
|
} else if val, ok := instance.Nets[NetID(bootTarget)]; ok && isEligible { // if the item is eligible and is in nets
|
||||||
instance.Boot.Enabled = append(instance.Boot.Enabled, val)
|
instance.Boot.Enabled = append(instance.Boot.Enabled, val)
|
||||||
eligibleBoot[bootTarget] = false
|
delete(eligibleBoot, bootTarget)
|
||||||
} else {
|
} else { // item is not eligible for boot but is included in the boot order
|
||||||
log.Printf("Encountered non-eligible boot target %s in instance %s\n", bootTarget, instance.Name)
|
log.Printf("Encountered enabled but non-eligible boot target %s in instance %s\n", bootTarget, instance.Name)
|
||||||
eligibleBoot[bootTarget] = false
|
delete(eligibleBoot, bootTarget)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for bootTarget, isEligible := range eligibleBoot {
|
for bootTarget, isEligible := range eligibleBoot { // iterate over remaining items, add them to Disabled
|
||||||
if val, ok := instance.Volumes[VolumeID(bootTarget)]; ok && isEligible { // if the item is eligible and is in volumes
|
if val, ok := instance.Volumes[VolumeID(bootTarget)]; ok && isEligible { // if the item is eligible and is in volumes
|
||||||
instance.Boot.Disabled = append(instance.Boot.Disabled, val)
|
instance.Boot.Disabled = append(instance.Boot.Disabled, val)
|
||||||
} else if val, ok := instance.Nets[NetID(bootTarget)]; ok && isEligible { // if the item is eligible and is in nets
|
} else if val, ok := instance.Nets[NetID(bootTarget)]; ok && isEligible { // if the item is eligible and is in nets
|
||||||
instance.Boot.Disabled = append(instance.Boot.Disabled, val)
|
instance.Boot.Disabled = append(instance.Boot.Disabled, val)
|
||||||
} else {
|
} else { // item is not eligible and is not already in the boot order, skip adding to model
|
||||||
log.Printf("Encountered non-eligible boot target %s in instance %s\n", bootTarget, instance.Name)
|
log.Printf("Encountered disabled and non-eligible boot target %s in instance %s\n", bootTarget, instance.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -232,14 +232,11 @@ func MergeCTDisksAndUnused(cc *proxmox.ContainerConfig) map[string]string {
|
|||||||
func GetVolumeInfo(host *Node, volume string) (*Volume, error) {
|
func GetVolumeInfo(host *Node, volume string) (*Volume, error) {
|
||||||
volumeData := Volume{}
|
volumeData := Volume{}
|
||||||
|
|
||||||
storageID := strings.Split(volume, ":")[0]
|
volumeObj := PVEObjectStringToMap(volume)
|
||||||
volumeID := strings.Split(volume, ",")[0]
|
volumeFile := volumeObj[""]
|
||||||
mp := ""
|
volumeStorage := strings.Split(volumeFile, ":")[0]
|
||||||
if strings.Contains(volume, "mp=") {
|
|
||||||
x := strings.Split(volume, "mp=")[1]
|
storage, err := host.pvenode.Storage(context.Background(), volumeStorage)
|
||||||
mp = strings.Split(x, ",")[0]
|
|
||||||
}
|
|
||||||
storage, err := host.pvenode.Storage(context.Background(), storageID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &volumeData, nil
|
return &volumeData, nil
|
||||||
}
|
}
|
||||||
@@ -250,38 +247,58 @@ func GetVolumeInfo(host *Node, volume string) (*Volume, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range content {
|
for _, c := range content {
|
||||||
if c.Volid == volumeID {
|
if c.Volid == volumeFile {
|
||||||
volumeData.Storage = storageID
|
volumeData.Storage = volumeStorage
|
||||||
volumeData.Format = c.Format
|
volumeData.Format = c.Format
|
||||||
volumeData.Size = uint64(c.Size)
|
volumeData.Size = uint64(c.Size)
|
||||||
volumeData.File = volumeID
|
volumeData.File = volumeFile
|
||||||
volumeData.MP = mp
|
volumeData.MP = volumeObj["mp"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &volumeData, nil
|
return &volumeData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetNetInfo(net string) (*Net, error) {
|
func GetNetInfo(netstring string) (*Net, error) {
|
||||||
n := Net{}
|
n := Net{}
|
||||||
|
|
||||||
for _, val := range strings.Split(net, ",") {
|
netobj := PVEObjectStringToMap(netstring)
|
||||||
if strings.HasPrefix(val, "rate=") {
|
|
||||||
rate, err := strconv.ParseUint(strings.TrimPrefix(val, "rate="), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return &n, err
|
|
||||||
}
|
|
||||||
n.Rate = rate
|
|
||||||
} else if strings.HasPrefix(val, "tag=") {
|
|
||||||
vlan, err := strconv.ParseUint(strings.TrimPrefix(val, "tag="), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return &n, err
|
|
||||||
}
|
|
||||||
n.VLAN = vlan
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n.Value = net
|
rate, err := strconv.ParseUint(netobj["rate"], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return &n, err
|
||||||
|
}
|
||||||
|
n.Rate = rate
|
||||||
|
|
||||||
|
vlan, err := strconv.ParseUint(netobj["tag"], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return &n, err
|
||||||
|
}
|
||||||
|
n.VLAN = vlan
|
||||||
|
|
||||||
|
n.Value = netstring
|
||||||
|
|
||||||
return &n, nil
|
return &n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// most pve objects (nets, disks, pcie, etc) have the following similar format:
|
||||||
|
// objname: v1,k2=v2,k3=v3,k4=v4 ...
|
||||||
|
// this function maps such strings to a map so that each individual key or value can be found more quickly
|
||||||
|
// in pcie or disks, the first value often does not have a key name, in such cases the key will be empty string ""
|
||||||
|
func PVEObjectStringToMap(objectstring string) map[string]string {
|
||||||
|
objectmap := map[string]string{}
|
||||||
|
for v := range strings.SplitSeq(objectstring, ",") {
|
||||||
|
key := ""
|
||||||
|
val := ""
|
||||||
|
if strings.Contains(v, "=") {
|
||||||
|
x := strings.Split(v, "=")
|
||||||
|
key = x[0]
|
||||||
|
val = x[1]
|
||||||
|
} else {
|
||||||
|
key = ""
|
||||||
|
val = v
|
||||||
|
}
|
||||||
|
objectmap[key] = val
|
||||||
|
}
|
||||||
|
return objectmap
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user