From ff2838ab21dbd9cb35b1b4d96701dc3669641b83 Mon Sep 17 00:00:00 2001
From: Florent Gluck <florent.gluck@hesge.ch>
Date: Sat, 21 Jan 2023 11:33:36 +0100
Subject: [PATCH] Completed client-side of USB redirection Bumped client to
 version 1.6.0

---
 src/cmdVM/vmCreate.go | 17 +----------------
 src/cmdVM/vmEdit.go   | 18 ++++++++++++++----
 src/utils/utils.go    | 15 +++++++++++++++
 3 files changed, 30 insertions(+), 20 deletions(-)

diff --git a/src/cmdVM/vmCreate.go b/src/cmdVM/vmCreate.go
index e1c3f79..d3f7e9f 100644
--- a/src/cmdVM/vmCreate.go
+++ b/src/cmdVM/vmCreate.go
@@ -2,7 +2,6 @@ package cmdVM
 
 import (
     "fmt"
-    "strings"
     "strconv"
     u "nexus-client/utils"
     g "nexus-client/globals"
@@ -68,7 +67,7 @@ func (cmd *Create)Run(args []string) int {
         return 1
     }
     nic := NicType(args[3])
-    usbDevs := cmd.str2UsbDevices(args[4])
+    usbDevs := u.Str2UsbDevices(args[4])
     templateID, err := uuid.Parse(args[5])
     if err != nil {
         u.PrintlnErr(err)
@@ -153,17 +152,3 @@ func (cmd *Create)Run(args []string) int {
 
     return statusCode
 }
-
-// Convert a string of USB devices of the form "1fc9:001d,067b:2303"
-// into a slice of string where each element is a string of the form "1fc9:001d".
-// Returns an empty slice if the input string is "none".
-func (cmd *Create)str2UsbDevices(s string) []string {
-    usbDevs := []string{}
-    if s != "none" {
-        devs := strings.Split(s, ",")  // Extracts USB devices
-        for _, dev := range devs {
-            usbDevs = append(usbDevs, dev)
-        }
-    }
-    return usbDevs
-}
diff --git a/src/cmdVM/vmEdit.go b/src/cmdVM/vmEdit.go
index c268536..58d30cf 100644
--- a/src/cmdVM/vmEdit.go
+++ b/src/cmdVM/vmEdit.go
@@ -16,7 +16,8 @@ type vmEditParameters struct {
     Name string
     Cpus int
     Ram int
-    Nic string
+    Nic NicType
+    UsbDevs []string
 }
 
 func (cmd *Edit)GetName() string {
@@ -34,13 +35,16 @@ func (cmd *Edit)PrintUsage() {
         u.PrintlnErr(desc)
     }
     u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
-    u.PrintlnErr("USAGE: "+cmd.GetName()+" [ID ...] [regex ...] [name=\"new name\"] [cpus=n] [ram=n] [nic=none/user]")
+    u.PrintlnErr("USAGE: "+cmd.GetName()+" [ID ...] [regex ...] [name=...] [cpus=...] [ram=...] [nic=...] [usb=...]")
     u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――")
     const usage string = `Parameters that can be changed (at least one must be specified):
 name    Name of the VM.
 cpus    Number of CPUs, between 1 and 16.
 ram     Amount of RAM in MB,  between 512 and 32768.
-nic     Network interface, either "none" (no network) or "user" (network access).`
+nic     Network interface, either "none" (no network) or "user" (network access).
+usb     List of USB devices exposed in the VM; either "none" or a list of comma separated
+        vendorID:productID (4-digit hex number), each specifying a USB device; example
+        exposing two USB devices: 1fc9:001d,067b:2303`
     u.PrintlnErr(usage)
     u.PrintlnErr("")
     printRegexUsageDetails()
@@ -141,7 +145,13 @@ func (cmd *Edit)parseArgs(args []string) (*vmEditParameters, []string, error) {
         }
         s = getStringVal(arg, "nic=")
         if s != "" {
-            vmParams.Nic = s
+            vmParams.Nic = NicType(s)
+            atLeastOneArg = true
+            continue
+        }
+        s = getStringVal(arg, "usb=")
+        if s != "" {
+            vmParams.UsbDevs = u.Str2UsbDevices(s)
             atLeastOneArg = true
             continue
         }
diff --git a/src/utils/utils.go b/src/utils/utils.go
index 5f9d2d1..edd2e8c 100644
--- a/src/utils/utils.go
+++ b/src/utils/utils.go
@@ -7,6 +7,7 @@ import (
     "io/fs"
     "bytes"
     "errors"
+    "strings"
     "net/mail"
     "archive/tar"
     "path/filepath"
@@ -112,3 +113,17 @@ func IsEmail(email string) bool {
     _, err := mail.ParseAddress(email)
     return err == nil
 }
+
+// Convert a string of USB devices of the form "1fc9:001d,067b:2303"
+// into a slice of string where each element is a string of the form "1fc9:001d".
+// Returns an empty slice if the input string is "none".
+func Str2UsbDevices(s string) []string {
+    usbDevs := []string{}
+    if s != "none" {
+        devs := strings.Split(s, ",")  // Extracts USB devices
+        for _, dev := range devs {
+            usbDevs = append(usbDevs, dev)
+        }
+    }
+    return usbDevs
+}
-- 
GitLab