Skip to content

Commit

Permalink
qemu,parseUSB: minor refactor
Browse files Browse the repository at this point in the history
Some comments from containers#20540

Signed-off-by: Aditya R <arajan@redhat.com>
  • Loading branch information
flouthoc committed Nov 13, 2023
1 parent 7dd33b3 commit 16ed237
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions pkg/machine/qemu/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,10 @@ func parseUSBs(usbs []string) ([]machine.USBConfig, error) {
return configs, fmt.Errorf("usb: fail to parse: missing '=': %s", str)
}

option := ""
if (left[0] == "bus" && right[0] == "devnum") ||
(right[0] == "bus" && left[0] == "devnum") {
option = "bus_devnum"
}
if (left[0] == "vendor" && right[0] == "product") ||
(right[0] == "vendor" && left[0] == "product") {
option = "vendor_product"
}
option := left[0] + "_" + right[0]

switch option {
case "bus_devnum":
case "bus_devnum", "devnum_bus":
bus, devnumber := left[1], right[1]
if right[0] == "bus" {
bus, devnumber = devnumber, bus
Expand All @@ -107,20 +99,20 @@ func parseUSBs(usbs []string) ([]machine.USBConfig, error) {
Bus: bus,
DevNumber: devnumber,
})
case "vendor_product":
case "vendor_product", "product_vendor":
vendorStr, productStr := left[1], right[1]
if right[0] == "vendor" {
vendorStr, productStr = productStr, vendorStr
}

vendor, err := strconv.ParseInt(vendorStr, 16, 0)
if err != nil {
return configs, fmt.Errorf("fail to convert vendor of %s: %s", str, err)
return configs, fmt.Errorf("usb: fail to convert vendor of %s: %s", str, err)
}

product, err := strconv.ParseInt(productStr, 16, 0)
if err != nil {
return configs, fmt.Errorf("fail to convert product of %s: %s", str, err)
return configs, fmt.Errorf("usb: fail to convert product of %s: %s", str, err)
}

configs = append(configs, machine.USBConfig{
Expand Down

0 comments on commit 16ed237

Please sign in to comment.