diff --git a/src/client/utils/utils.go b/src/client/utils/file.go similarity index 52% rename from src/client/utils/utils.go rename to src/client/utils/file.go index d41477e10a198dc2d037485b0e60a3939b9c2583..0f0f7fd6488c2bacc319f0143262084455e157d0 100644 --- a/src/client/utils/utils.go +++ b/src/client/utils/file.go @@ -5,38 +5,15 @@ import ( "bytes" "compress/gzip" "errors" - "fmt" "io" "io/fs" - "io/ioutil" - "net/mail" "os" - "os/signal" "path/filepath" "strings" - "syscall" "github.com/google/uuid" ) -func Print(a ...any) { - os.Stdout.WriteString(fmt.Sprint(a...)) -} - -func Println(a ...any) { - Print(a...) - Print("\n") -} - -func PrintErr(a ...any) { - os.Stderr.WriteString(fmt.Sprint(a...)) -} - -func PrintlnErr(a ...any) { - PrintErr(a...) - PrintErr("\n") -} - // Creates a tar.gz archive of dir and all its files and subdirectories. // Note: dir can also be a file. // Source code slightly modified from: https://gist.github.com/mimoo/25fc9716e0f1353791f5908f94d6e726 @@ -105,55 +82,3 @@ func GetRandomTempFilename() (string, error) { randName := "temp_" + uuid.String() return filepath.Join(tempDir, randName), nil } - -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 -} - -// Returns the content of file as a string -func FileToString(file string) (string, error) { - content, err := ioutil.ReadFile(file) - if err != nil { - return "", err - } - return string(content), nil -} - -// Removes an element from a string array at a given index -func RemoveArgAtIndex(slice []string, index int) []string { - return append(slice[:index], slice[index+1:]...) -} - -// TODO: better way of appending a portable "new line" to a string -func AppendNewLine(s string) string { - var newLine string - newLine = fmt.Sprintln(newLine, "") - return s + newLine -} - -// Wait on dedicated signals -func WaitForSignals() { - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) - - for { - sig := <-sigs // blocks on any of the above signals. - Println("Caught signal (" + sig.String() + ")") - break - } -} diff --git a/src/client/utils/print.go b/src/client/utils/print.go new file mode 100644 index 0000000000000000000000000000000000000000..bd1cb2c6e53ab65689f52f7b27c27396458060dc --- /dev/null +++ b/src/client/utils/print.go @@ -0,0 +1,24 @@ +package utils + +import ( + "fmt" + "os" +) + +func Print(a ...any) { + os.Stdout.WriteString(fmt.Sprint(a...)) +} + +func Println(a ...any) { + Print(a...) + Print("\n") +} + +func PrintErr(a ...any) { + os.Stderr.WriteString(fmt.Sprint(a...)) +} + +func PrintlnErr(a ...any) { + PrintErr(a...) + PrintErr("\n") +} diff --git a/src/client/utils/signal.go b/src/client/utils/signal.go new file mode 100644 index 0000000000000000000000000000000000000000..92964d25f02fedcbc73ee265b52fc48efdf31445 --- /dev/null +++ b/src/client/utils/signal.go @@ -0,0 +1,19 @@ +package utils + +import ( + "os" + "os/signal" + "syscall" +) + +// Wait on dedicated signals +func WaitForSignals() { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) + + for { + sig := <-sigs // blocks on any of the above signals. + Println("Caught signal (" + sig.String() + ")") + break + } +} diff --git a/src/client/utils/string.go b/src/client/utils/string.go new file mode 100644 index 0000000000000000000000000000000000000000..854d18605f4623c605c13acbae4628db587f24a8 --- /dev/null +++ b/src/client/utils/string.go @@ -0,0 +1,48 @@ +package utils + +import ( + "fmt" + "io/ioutil" + "net/mail" + "strings" +) + +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 +} + +// Returns the content of file as a string +func FileToString(file string) (string, error) { + content, err := ioutil.ReadFile(file) + if err != nil { + return "", err + } + return string(content), nil +} + +// Removes an element from a string array at a given index +func RemoveArgAtIndex(slice []string, index int) []string { + return append(slice[:index], slice[index+1:]...) +} + +// TODO: better way of appending a portable "new line" to a string +func AppendNewLine(s string) string { + var newLine string + newLine = fmt.Sprintln(newLine, "") + return s + newLine +}