Skip to content

Commit 8fba1fe

Browse files
committed
Fix goconst linter errors by extracting repeated string literals
Extract repeated string literals into constants to satisfy the goconst linter: "yes" in libvirt.go and memory unit strings in utils.go.
1 parent 7fab946 commit 8fba1fe

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

internal/libvirt/libvirt.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import (
3838
"github.com/cobaltcore-dev/kvm-node-agent/internal/libvirt/dominfo"
3939
)
4040

41+
const supportedYes = "yes"
42+
4143
type LibVirt struct {
4244
virt *libvirt.Libvirt
4345
client client.Client
@@ -372,7 +374,7 @@ func (l *LibVirt) addDomainCapabilities(old v1.Hypervisor) (v1.Hypervisor, error
372374
// - <mode name="example3" supported="yes"></mode> becomes "mode/example3"
373375
newHv.Status.DomainCapabilities.SupportedCpuModes = []string{}
374376
for _, cpuMode := range domCapabilities.CPU.Modes {
375-
if cpuMode.Supported != "yes" {
377+
if cpuMode.Supported != supportedYes {
376378
continue
377379
}
378380
newHv.Status.DomainCapabilities.SupportedCpuModes = append(
@@ -396,7 +398,7 @@ func (l *LibVirt) addDomainCapabilities(old v1.Hypervisor) (v1.Hypervisor, error
396398
// - <video supported="yes"></video> becomes "video".
397399
newHv.Status.DomainCapabilities.SupportedDevices = []string{}
398400
for _, device := range domCapabilities.Devices.Devices {
399-
if device.Supported != "yes" {
401+
if device.Supported != supportedYes {
400402
continue
401403
}
402404
newHv.Status.DomainCapabilities.SupportedDevices = append(
@@ -416,7 +418,7 @@ func (l *LibVirt) addDomainCapabilities(old v1.Hypervisor) (v1.Hypervisor, error
416418
// Convert the supported features into a flat list.
417419
newHv.Status.DomainCapabilities.SupportedFeatures = []string{}
418420
for _, feature := range domCapabilities.Features.Features {
419-
if feature.Supported == "yes" {
421+
if feature.Supported == supportedYes {
420422
newHv.Status.DomainCapabilities.SupportedFeatures = append(
421423
newHv.Status.DomainCapabilities.SupportedFeatures,
422424
feature.XMLName.Local,

internal/libvirt/utils.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ import (
2424
"k8s.io/apimachinery/pkg/api/resource"
2525
)
2626

27+
const (
28+
UnitKiB = "KiB"
29+
UnitMiB = "MiB"
30+
UnitGiB = "GiB"
31+
UnitTiB = "TiB"
32+
)
33+
2734
type UUID [16]byte
2835

2936
func (uuid UUID) String() string {
@@ -59,13 +66,13 @@ func MemoryToResource(value int64, unit string) (resource.Quantity, error) {
5966
var quantity *resource.Quantity
6067
// Check the unit
6168
switch unit {
62-
case "KiB":
69+
case UnitKiB:
6370
quantity = resource.NewQuantity(value*1024, resource.BinarySI)
64-
case "MiB":
71+
case UnitMiB:
6572
quantity = resource.NewQuantity(value*1024*1024, resource.BinarySI)
66-
case "GiB":
73+
case UnitGiB:
6774
quantity = resource.NewQuantity(value*1024*1024*1024, resource.BinarySI)
68-
case "TiB":
75+
case UnitTiB:
6976
quantity = resource.NewQuantity(value*1024*1024*1024*1024, resource.BinarySI)
7077
}
7178
if quantity == nil {

0 commit comments

Comments
 (0)