From 73eaa96c8545da797be614a5dbeee46cd68754aa Mon Sep 17 00:00:00 2001 From: Black-Hole1 Date: Wed, 27 Dec 2023 15:12:33 +0800 Subject: [PATCH] feat(config): add AddDevices function In most cases, there might be more than one device. Adding the AddDevices method helps reduce the amount of code. Signed-off-by: Black-Hole1 --- pkg/config/config.go | 5 +++++ pkg/config/json_test.go | 10 ++++------ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 7afc3b0e..f818a167 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -146,6 +146,11 @@ func (vm *VirtualMachine) AddDevice(dev VirtioDevice) { vm.Devices = append(vm.Devices, dev) } +// AddDevices adds a list of devices to vm. +func (vm *VirtualMachine) AddDevices(dev ...VirtioDevice) { + vm.Devices = append(vm.Devices, dev...) +} + func (vm *VirtualMachine) AddTimeSyncFromCmdLine(cmdlineOpts string) error { if cmdlineOpts == "" { return nil diff --git a/pkg/config/json_test.go b/pkg/config/json_test.go index 49fd9d54..9a23551c 100644 --- a/pkg/config/json_test.go +++ b/pkg/config/json_test.go @@ -79,14 +79,12 @@ var jsonTests = map[string]jsonTest{ dev = VirtioVsockNew(1234, "/virtiovsock", false) vm.AddDevice(dev) // virtio-fs - dev = VirtioFsNew("/virtiofs", "tag") - vm.AddDevice(dev) + fs := VirtioFsNew("/virtiofs", "tag") // USB mass storage - dev = USBMassStorageNew("/usbmassstorage") - vm.AddDevice(dev) + usb := USBMassStorageNew("/usbmassstorage") // rosetta - dev = RosettaShareNew("vz-rosetta") - vm.AddDevice(dev) + rosetta := RosettaShareNew("vz-rosetta") + vm.AddDevices(fs, usb, rosetta) return vm },