forked from project-flogo/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
68 lines (60 loc) · 1.42 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
63
64
65
66
67
68
package common
import (
"crypto/rand"
"io"
"time"
"io/ioutil"
"log"
"os"
"strings"
"github.com/oklog/ulid"
)
//unique id (26 chars)
func GetUniqueId() (string, error) {
entropy, err := generateRandomBytes(10)
if err != nil {
return "", err
}
uuid, err := ulid.New(ulid.Timestamp(time.Now().UTC()), entropy)
if err != nil {
return "", err
}
return uuid.String(), nil
}
// generateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func generateRandomBytes(n int) (io.Reader, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return rand.Reader, nil
}
// GetAbsPathForResource gets the absolute resource path off $GOPATH
func GetAbsPathForResource(resourcepath string) string {
GOPATH := os.Getenv("GOPATH")
// fmt.Printf("GOPATH - [%s]\n", GOPATH)
paths := strings.Split(GOPATH, ":")
for _, path := range paths {
// fmt.Printf("path[%s]\n", path)
absPath := path + "/" + resourcepath
_, err := os.Stat(absPath)
if err == nil {
return absPath
}
}
return ""
}
// FileToString reads file content to string
func FileToString(fileName string) string {
dat, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
return ""
}
return string(dat)
}