Skip to content

Commit

Permalink
Merge pull request #395 from huettern/fix/usb-port-id
Browse files Browse the repository at this point in the history
fix: usb port parsing
  • Loading branch information
Tinyblargon authored Jan 14, 2025
2 parents 1058743 + a7c2674 commit 4e26f5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions proxmox/config_qemu_usb.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func (id UsbDeviceID) String() string {
type UsbPortID string // regex: \d+-\d+

const (
UsbPortID_Error_Invalid string = "invalid usb port id"
UsbPortID_Error_Invalid string = "invalid usb port id. Expected expression of the form '<bus>-<port>(.<port>)*' where bus and port are integers"
)

func (id UsbPortID) String() string {
Expand All @@ -340,8 +340,11 @@ func (id UsbPortID) Validate() error {
if _, err := strconv.Atoi(idArray[0]); err != nil {
return errors.New(UsbPortID_Error_Invalid)
}
if _, err := strconv.Atoi(idArray[1]); err != nil {
return errors.New(UsbPortID_Error_Invalid)
parts := strings.Split(idArray[1], ".")
for _, part := range parts {
if _, err := strconv.Atoi(part); err != nil {
return errors.New(UsbPortID_Error_Invalid)
}
}
return nil
}
4 changes: 4 additions & 0 deletions proxmox/config_qemu_usb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func Test_UsbPortID_Validate(t *testing.T) {
}{
{name: "Valid",
input: "2-4"},
{name: "Valid",
input: "2-4.1"},
{name: "Valid",
input: "3-1.2.3.4.5.6.7.8.9"},
// Invalid
{name: "UsbPortID_Error_Invalid",
input: "2-4-5",
Expand Down

0 comments on commit 4e26f5d

Please sign in to comment.