Skip to content
Snippets Groups Projects
Commit d888cd6d authored by Florent Gluck's avatar Florent Gluck
Browse files

split src/client/utils/utils.go into several go files

parent ea33f6d9
No related branches found
No related tags found
No related merge requests found
...@@ -5,38 +5,15 @@ import ( ...@@ -5,38 +5,15 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"errors" "errors"
"fmt"
"io" "io"
"io/fs" "io/fs"
"io/ioutil"
"net/mail"
"os" "os"
"os/signal"
"path/filepath" "path/filepath"
"strings" "strings"
"syscall"
"github.com/google/uuid" "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. // Creates a tar.gz archive of dir and all its files and subdirectories.
// Note: dir can also be a file. // Note: dir can also be a file.
// Source code slightly modified from: https://gist.github.com/mimoo/25fc9716e0f1353791f5908f94d6e726 // Source code slightly modified from: https://gist.github.com/mimoo/25fc9716e0f1353791f5908f94d6e726
...@@ -105,55 +82,3 @@ func GetRandomTempFilename() (string, error) { ...@@ -105,55 +82,3 @@ func GetRandomTempFilename() (string, error) {
randName := "temp_" + uuid.String() randName := "temp_" + uuid.String()
return filepath.Join(tempDir, randName), nil 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
}
}
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")
}
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
}
}
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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment