refactor: remove unnecessary type conversions

Remove redundant type conversions identified by unconvert linter:
- Remove int() conversions for already-int VMID fields
- Remove int64() conversions for already-int64 arithmetic results
- Remove uint64() conversions for already-uint64 Disk/MaxDisk fields
- Remove int() on syscall.Stdin (already int constant)
This commit is contained in:
rcourtman 2025-11-27 10:32:38 +00:00
parent 67b60adb65
commit e9ac1429c0
4 changed files with 25 additions and 25 deletions

View file

@ -177,7 +177,7 @@ func getPassphrase(prompt string, confirm bool) string {
// Interactive prompt // Interactive prompt
fmt.Print(prompt) fmt.Print(prompt)
bytePassword, err := term.ReadPassword(int(syscall.Stdin)) bytePassword, err := term.ReadPassword(syscall.Stdin)
fmt.Println() fmt.Println()
if err != nil { if err != nil {
return "" return ""
@ -188,7 +188,7 @@ func getPassphrase(prompt string, confirm bool) string {
// Confirm if requested // Confirm if requested
if confirm { if confirm {
fmt.Print("Confirm passphrase: ") fmt.Print("Confirm passphrase: ")
bytePassword2, err := term.ReadPassword(int(syscall.Stdin)) bytePassword2, err := term.ReadPassword(syscall.Stdin)
fmt.Println() fmt.Println()
if err != nil { if err != nil {
return "" return ""

View file

@ -1008,8 +1008,8 @@ func generateGuestNetworkInfo() ([]string, []models.GuestNetworkInterface) {
ipAddresses = append(ipAddresses, ip) ipAddresses = append(ipAddresses, ip)
} }
rxBytes := int64(rand.Int63n(8*1024*1024*1024) + rand.Int63n(256*1024*1024)) rxBytes := rand.Int63n(8*1024*1024*1024) + rand.Int63n(256*1024*1024)
txBytes := int64(rand.Int63n(6*1024*1024*1024) + rand.Int63n(256*1024*1024)) txBytes := rand.Int63n(6*1024*1024*1024) + rand.Int63n(256*1024*1024)
interfaces = append(interfaces, models.GuestNetworkInterface{ interfaces = append(interfaces, models.GuestNetworkInterface{
Name: name, Name: name,
@ -2537,7 +2537,7 @@ func generateBackups(vms []models.VM, containers []models.Container) []models.St
numBackups := 1 + rand.Intn(3) numBackups := 1 + rand.Intn(3)
for i := 0; i < numBackups; i++ { for i := 0; i < numBackups; i++ {
backupTime := time.Now().Add(-time.Duration(rand.Intn(30*24)) * time.Hour) backupTime := time.Now().Add(-time.Duration(rand.Intn(30*24)) * time.Hour)
backupSize := int64(vm.Disk.Total/10 + rand.Int63n(vm.Disk.Total/5)) // 10-30% of disk size backupSize := vm.Disk.Total/10 + rand.Int63n(vm.Disk.Total/5) // 10-30% of disk size
backup := models.StorageBackup{ backup := models.StorageBackup{
ID: fmt.Sprintf("backup-%s-vm-%d-%d", vm.Node, vm.VMID, i), ID: fmt.Sprintf("backup-%s-vm-%d-%d", vm.Node, vm.VMID, i),
@ -2575,22 +2575,22 @@ func generateBackups(vms []models.VM, containers []models.Container) []models.St
numBackups := 1 + rand.Intn(2) numBackups := 1 + rand.Intn(2)
for i := 0; i < numBackups; i++ { for i := 0; i < numBackups; i++ {
backupTime := time.Now().Add(-time.Duration(rand.Intn(30*24)) * time.Hour) backupTime := time.Now().Add(-time.Duration(rand.Intn(30*24)) * time.Hour)
backupSize := int64(ct.Disk.Total/20 + rand.Int63n(ct.Disk.Total/10)) // 5-15% of disk size backupSize := ct.Disk.Total/20 + rand.Int63n(ct.Disk.Total/10) // 5-15% of disk size
backup := models.StorageBackup{ backup := models.StorageBackup{
ID: fmt.Sprintf("backup-%s-ct-%d-%d", ct.Node, int(ct.VMID), i), ID: fmt.Sprintf("backup-%s-ct-%d-%d", ct.Node, ct.VMID, i),
Storage: "local", Storage: "local",
Node: ct.Node, Node: ct.Node,
Instance: ct.Instance, Instance: ct.Instance,
Type: "lxc", Type: "lxc",
VMID: int(ct.VMID), VMID: ct.VMID,
Time: backupTime, Time: backupTime,
CTime: backupTime.Unix(), CTime: backupTime.Unix(),
Size: backupSize, Size: backupSize,
Format: "tar.zst", Format: "tar.zst",
Notes: fmt.Sprintf("Backup of %s", ct.Name), Notes: fmt.Sprintf("Backup of %s", ct.Name),
Protected: rand.Float64() > 0.9, // 10% protected Protected: rand.Float64() > 0.9, // 10% protected
Volid: fmt.Sprintf("local:backup/vzdump-lxc-%d-%s.tar.zst", int(ct.VMID), backupTime.Format("2006_01_02-15_04_05")), Volid: fmt.Sprintf("local:backup/vzdump-lxc-%d-%s.tar.zst", ct.VMID, backupTime.Format("2006_01_02-15_04_05")),
IsPBS: false, IsPBS: false,
Verified: rand.Float64() > 0.2, // 80% verified Verified: rand.Float64() > 0.2, // 80% verified
} }
@ -2784,7 +2784,7 @@ func generatePBSBackups(vms []models.VM, containers []models.Container) []models
BackupType: "vm", BackupType: "vm",
VMID: fmt.Sprintf("%d", vm.VMID), VMID: fmt.Sprintf("%d", vm.VMID),
BackupTime: backupTime, BackupTime: backupTime,
Size: int64(vm.Disk.Total/8 + rand.Int63n(vm.Disk.Total/4)), Size: vm.Disk.Total/8 + rand.Int63n(vm.Disk.Total/4),
Protected: rand.Float64() > 0.85, // 15% protected Protected: rand.Float64() > 0.85, // 15% protected
Verified: rand.Float64() > 0.2, // 80% verified Verified: rand.Float64() > 0.2, // 80% verified
Comment: fmt.Sprintf("Automated backup of %s", vm.Name), Comment: fmt.Sprintf("Automated backup of %s", vm.Name),
@ -2807,14 +2807,14 @@ func generatePBSBackups(vms []models.VM, containers []models.Container) []models
backupTime := time.Now().Add(-time.Duration(rand.Intn(45*24)) * time.Hour) backupTime := time.Now().Add(-time.Duration(rand.Intn(45*24)) * time.Hour)
backup := models.PBSBackup{ backup := models.PBSBackup{
ID: fmt.Sprintf("pbs-backup-ct-%d-%d", int(ct.VMID), i), ID: fmt.Sprintf("pbs-backup-ct-%d-%d", ct.VMID, i),
Instance: pbsInstances[rand.Intn(len(pbsInstances))], Instance: pbsInstances[rand.Intn(len(pbsInstances))],
Datastore: datastores[rand.Intn(len(datastores))], Datastore: datastores[rand.Intn(len(datastores))],
Namespace: "root", Namespace: "root",
BackupType: "ct", BackupType: "ct",
VMID: fmt.Sprintf("%d", int(ct.VMID)), VMID: fmt.Sprintf("%d", ct.VMID),
BackupTime: backupTime, BackupTime: backupTime,
Size: int64(ct.Disk.Total/15 + rand.Int63n(ct.Disk.Total/8)), Size: ct.Disk.Total/15 + rand.Int63n(ct.Disk.Total/8),
Protected: rand.Float64() > 0.9, // 10% protected Protected: rand.Float64() > 0.9, // 10% protected
Verified: rand.Float64() > 0.15, // 85% verified Verified: rand.Float64() > 0.15, // 85% verified
Comment: fmt.Sprintf("Daily backup of %s", ct.Name), Comment: fmt.Sprintf("Daily backup of %s", ct.Name),
@ -2839,9 +2839,9 @@ func generatePBSBackups(vms []models.VM, containers []models.Container) []models
BackupType: "ct", // Host configs are stored as 'ct' type in PBS BackupType: "ct", // Host configs are stored as 'ct' type in PBS
VMID: "0", // VMID 0 indicates host config VMID: "0", // VMID 0 indicates host config
BackupTime: backupTime, BackupTime: backupTime,
Size: int64(50*1024*1024 + rand.Int63n(100*1024*1024)), // 50-150MB for host configs Size: 50*1024*1024 + rand.Int63n(100*1024*1024), // 50-150MB for host configs
Protected: rand.Float64() > 0.7, // 30% protected Protected: rand.Float64() > 0.7, // 30% protected
Verified: rand.Float64() > 0.1, // 90% verified Verified: rand.Float64() > 0.1, // 90% verified
Comment: "PMG host configuration backup", Comment: "PMG host configuration backup",
Owner: "root@pam", Owner: "root@pam",
} }
@ -3076,12 +3076,12 @@ func generateSnapshots(vms []models.VM, containers []models.Container) []models.
snapshotTime := time.Now().Add(-time.Duration(rand.Intn(60*24)) * time.Hour) snapshotTime := time.Now().Add(-time.Duration(rand.Intn(60*24)) * time.Hour)
snapshot := models.GuestSnapshot{ snapshot := models.GuestSnapshot{
ID: fmt.Sprintf("snapshot-%s-ct-%d-%d", ct.Node, int(ct.VMID), i), ID: fmt.Sprintf("snapshot-%s-ct-%d-%d", ct.Node, ct.VMID, i),
Name: snapshotNames[rand.Intn(len(snapshotNames))], Name: snapshotNames[rand.Intn(len(snapshotNames))],
Node: ct.Node, Node: ct.Node,
Instance: ct.Instance, Instance: ct.Instance,
Type: "lxc", Type: "lxc",
VMID: int(ct.VMID), VMID: ct.VMID,
Time: snapshotTime, Time: snapshotTime,
Description: fmt.Sprintf("Container snapshot for %s", ct.Name), Description: fmt.Sprintf("Container snapshot for %s", ct.Name),
VMState: false, // Containers don't have VM state VMState: false, // Containers don't have VM state

View file

@ -6913,7 +6913,7 @@ func (m *Monitor) pollVMsAndContainersEfficient(ctx context.Context, instanceNam
Status: res.Status, Status: res.Status,
Type: "lxc", Type: "lxc",
CPU: safeFloat(res.CPU), CPU: safeFloat(res.CPU),
CPUs: int(res.MaxCPU), CPUs: res.MaxCPU,
Memory: models.Memory{ Memory: models.Memory{
Total: int64(memTotal), Total: int64(memTotal),
Used: int64(memUsed), Used: int64(memUsed),
@ -8132,7 +8132,7 @@ func (m *Monitor) pollStorageBackupsWithNodes(ctx context.Context, instanceName
} }
for _, ct := range snapshot.Containers { for _, ct := range snapshot.Containers {
if ct.Instance == instanceName { if ct.Instance == instanceName {
guestNodeMap[int(ct.VMID)] = ct.Node guestNodeMap[ct.VMID] = ct.Node
} }
} }
@ -8465,9 +8465,9 @@ func buildGuestLookups(snapshot models.StateSnapshot, metadataStore *config.Gues
Instance: ct.Instance, Instance: ct.Instance,
Node: ct.Node, Node: ct.Node,
Type: ct.Type, Type: ct.Type,
VMID: int(ct.VMID), VMID: ct.VMID,
} }
key := alerts.BuildGuestKey(ct.Instance, ct.Node, int(ct.VMID)) key := alerts.BuildGuestKey(ct.Instance, ct.Node, ct.VMID)
if _, exists := byKey[key]; !exists { if _, exists := byKey[key]; !exists {
byKey[key] = info byKey[key] = info
} }

View file

@ -443,8 +443,8 @@ func (m *Monitor) pollVMsWithNodes(ctx context.Context, instanceName string, cli
// Calculate disk usage - start with allocated disk size // Calculate disk usage - start with allocated disk size
// NOTE: The Proxmox cluster/resources API always returns 0 for VM disk usage // NOTE: The Proxmox cluster/resources API always returns 0 for VM disk usage
// We must query the guest agent to get actual disk usage // We must query the guest agent to get actual disk usage
diskUsed := uint64(vm.Disk) diskUsed := vm.Disk
diskTotal := uint64(vm.MaxDisk) diskTotal := vm.MaxDisk
diskFree := diskTotal - diskUsed diskFree := diskTotal - diskUsed
diskUsage := safePercentage(float64(diskUsed), float64(diskTotal)) diskUsage := safePercentage(float64(diskUsed), float64(diskTotal))
diskStatusReason := "" diskStatusReason := ""
@ -733,7 +733,7 @@ func (m *Monitor) pollVMsWithNodes(ctx context.Context, instanceName string, cli
Status: vm.Status, Status: vm.Status,
Type: "qemu", Type: "qemu",
CPU: cpuUsage, CPU: cpuUsage,
CPUs: int(vm.CPUs), CPUs: vm.CPUs,
Memory: memory, Memory: memory,
Disk: models.Disk{ Disk: models.Disk{
Total: int64(diskTotal), Total: int64(diskTotal),