From e336529768b2f2507bbc364470a2fdc876529612 Mon Sep 17 00:00:00 2001 From: Kevin Cui Date: Thu, 25 Jan 2024 09:36:19 +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: Kevin Cui --- pkg/config/config.go | 6 +++++- pkg/config/json_test.go | 12 ++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index efd83f20..2f84efbe 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -143,8 +143,12 @@ func (vm *VirtualMachine) VirtioVsockDevices() []*VirtioVsock { // AddDevice adds a dev to vm. This device can be created with one of the // VirtioXXXNew methods. func (vm *VirtualMachine) AddDevice(dev VirtioDevice) error { - vm.Devices = append(vm.Devices, dev) + return vm.AddDevices(dev) +} +// AddDevices adds a list of devices to vm. +func (vm *VirtualMachine) AddDevices(dev ...VirtioDevice) error { + vm.Devices = append(vm.Devices, dev...) return nil } diff --git a/pkg/config/json_test.go b/pkg/config/json_test.go index 64ac0864..9345ebb1 100644 --- a/pkg/config/json_test.go +++ b/pkg/config/json_test.go @@ -97,19 +97,15 @@ var jsonTests = map[string]jsonTest{ err = vm.AddDevice(dev) require.NoError(t, err) // virtio-fs - dev, err = VirtioFsNew("/virtiofs", "tag") - require.NoError(t, err) - err = vm.AddDevice(dev) + fs, err := VirtioFsNew("/virtiofs", "tag") require.NoError(t, err) // USB mass storage - dev, err = USBMassStorageNew("/usbmassstorage") - require.NoError(t, err) - err = vm.AddDevice(dev) + usb, err := USBMassStorageNew("/usbmassstorage") require.NoError(t, err) // rosetta - dev, err = RosettaShareNew("vz-rosetta") + rosetta, err := RosettaShareNew("vz-rosetta") require.NoError(t, err) - err = vm.AddDevice(dev) + err = vm.AddDevices(fs, usb, rosetta) require.NoError(t, err) return vm