-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
71 lines (57 loc) · 1.26 KB
/
functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
)
const colorReset = "\033[0m"
const colorRed = "\033[31m"
const colorGreen = "\033[32m"
const colorYellow = "\033[33m"
const colorBlue = "\033[34m"
const colorPurple = "\033[35m"
const colorCyan = "\033[36m"
const colorWhite = "\033[37m"
/*
Write to file
*/
func writeFile(filepath string, filename string, content []byte, filePerm os.FileMode) {
fullFilepath := filename
if filepath != "." && filepath != "" {
fullFilepath = filepath + "/" + filename
if _, err := os.Stat(fullFilepath); os.IsNotExist(err) {
os.MkdirAll(filepath, 0700)
}
}
err := os.WriteFile(fullFilepath, content, filePerm)
if err != nil {
fmt.Printf("Unable to write file: %v", err)
}
}
/*
Read from file
*/
func readFile(filepath string, filename string) []byte {
fullFilepath := filename
if filepath != "." && filepath != "" {
fullFilepath = filepath + "/" + filename
if _, err := os.Stat(fullFilepath); os.IsNotExist(err) {
return []byte{}
}
}
content, err := os.ReadFile(fullFilepath)
if err != nil {
return []byte{}
}
return content
}
/*
Create a random string
*/
func randomString(n int) string {
var Rando = rand.Reader
b := make([]byte, n)
_, _ = Rando.Read(b)
return hex.EncodeToString(b)
}