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

removed unused files

parent e29ddb09
No related branches found
No related tags found
No related merge requests found
package utils
import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
)
// 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
func TarGzDir(dir, archive string) error {
_, err := os.Stat(dir)
if errors.Is(err, fs.ErrNotExist) {
return errors.New("Error: \"" + dir + "\" does not exist")
}
var buf bytes.Buffer
gzWriter := gzip.NewWriter(&buf)
tarWriter := tar.NewWriter(gzWriter)
// Walks through every file in the directory
filepath.Walk(dir, func(file string, fi os.FileInfo, err error) error {
header, err := tar.FileInfoHeader(fi, file)
if err != nil {
return err
}
fileRel := strings.TrimPrefix(file, dir)
fileRel = filepath.Join(filepath.Base(dir), fileRel)
header.Name = filepath.ToSlash(fileRel)
if err := tarWriter.WriteHeader(header); err != nil {
return err
}
// if not a dir, write file content
if !fi.IsDir() {
data, err := os.Open(file)
if err != nil {
return err
}
if _, err := io.Copy(tarWriter, data); err != nil {
return err
}
}
return nil
})
if err := tarWriter.Close(); err != nil {
return err
}
if err := gzWriter.Close(); err != nil {
return err
}
fileToWrite, err := os.OpenFile(archive, os.O_CREATE|os.O_RDWR, os.FileMode(0750))
if err != nil {
return err
}
if _, err := io.Copy(fileToWrite, &buf); err != nil {
return err
}
return nil
}
func GetRandomTempFilename() (string, error) {
tempDir := os.TempDir()
uuid, err := uuid.NewRandom()
if err != nil {
return "", errors.New("Failed creating random UUID: " + err.Error())
}
randName := "temp_" + uuid.String()
return filepath.Join(tempDir, randName), nil
}
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
}
package utils
import (
"errors"
"os"
)
// Returns true if the specified file exists, false otherwise.
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return !errors.Is(err, os.ErrNotExist)
}
package utils
import (
"io"
"math/rand"
"net"
"nexus-server/logger"
"os"
"path/filepath"
"strconv"
"time"
"golang.org/x/crypto/bcrypt"
)
var log = logger.GetInstance()
// Returns the list of subdirectories present in dir.
func GetSubDirs(dir string) ([]string, error) {
subDirs := []string{}
currentDir, err := os.Open(dir)
if err != nil {
return nil, err
}
// Retrieves all files entries in the directory (0 = all files in the directory).
files, err := currentDir.Readdir(0)
if err != nil {
currentDir.Close()
return nil, err
}
currentDir.Close()
// Loop over file entries
for _, f := range files {
if f.IsDir() {
subDirs = append(subDirs, filepath.Join(dir, f.Name()))
}
}
return subDirs, nil
}
// Initializes the random number generator.
func RandInit() {
rand.Seed(time.Now().UnixNano())
}
// Returns an int in the range [min,max] (both inclusive).
func Rand(min, max int) int {
return rand.Intn(max-min+1) + min
}
func CopyFiles(source, dest string) error {
src, err := os.Open(source)
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(dest)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
return err
}
// Returns a hash of the "clear" password passed in argument.
func HashPassword(pwd string) string {
// Uses bcrypt to hash the password.
hashedPwd, _ := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
return string(hashedPwd)
}
// Returns a random MAC address as a string (e.g. "b3:30:49:f8:d0:4b").
func RandMacAddress() (string, error) {
buf := make([]byte, 6)
_, err := rand.Read(buf)
if err != nil {
return "", err
}
// Sets unicast mode (bit 0 to 0) and locally administered addresses (bit 1 to 1)
// For more details, see: https://en.wikipedia.org/wiki/MAC_address#Universal_vs._local_(U/L_bit)
buf[0] &= 0xFE
buf[0] |= 2
var mac net.HardwareAddr
mac = append(mac, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
return mac.String(), nil
}
// Returns true if the specified TCP port is available, false otherwise.
func IsPortAvailable(port int) bool {
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
return false
}
ln.Close()
return true
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment