-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
62 lines (55 loc) · 1.25 KB
/
utils.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
package in3d
import (
"errors"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"regexp"
"runtime"
"time"
)
// LoE : log if err is notnull
func LoE(msg string, err error) {
if err != nil {
log.Printf("\n❌ %s\n %v\n", msg, err)
}
}
// EoE : exit with error code 1 and print, if err is not nil
func EoE(msg string, err error) {
if err != nil {
fmt.Printf("\n❌ %s\n %v\n", msg, err)
os.Exit(1)
panic(err)
}
}
// SetRelPath : resolves the absolute path for provided relative path.
func SetRelPath(relPath string) {
if _, filename, _, ok := runtime.Caller(1); ok {
re := regexp.MustCompile("[a-zA-Z0-9-]*.go$")
path := filepath.Join(re.ReplaceAllString(filename, ""), relPath)
EoE("Error Accessing relPath:", os.Chdir(path))
} else {
EoE("Error Getting Caller Location", errors.New(filename))
}
}
// Random : return pseudo random number in range
func Random(min, max int) int {
rand.NewSource(time.Now().UnixNano())
return rand.Intn(max-min) + min
}
// RandomF : return pseudo random float32 number in range
func RandomF() float32 {
rand.NewSource(time.Now().UnixNano())
return rand.Float32()
}
// ExecPath :
func ExecPath() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
return exPath
}