-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
211 lines (195 loc) · 4.85 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package utils
import (
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"math/rand"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
// code is based on https://github.com/AlanBar13/pass-generator
const voc string = "abcdfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numbers string = "0123456789"
const symbols string = "!@#$%&*+_-="
// RandomString generates random string
func RandomString() string {
var str string
chars := voc + numbers
for i := 0; i < 16; i++ {
str += string([]rune(chars)[rand.Intn(len(chars))])
}
return str
}
// RecordSize returns actual record size of given interface object
func RecordSize(v interface{}) (int64, error) {
data, err := json.Marshal(v)
if err == nil {
return int64(binary.Size(data)), nil
}
return 0, err
}
// Stack returns full runtime stack
func Stack() string {
trace := make([]byte, 2048)
count := runtime.Stack(trace, false)
return fmt.Sprintf("\nStack of %d bytes: %s\n", count, trace)
}
// ErrPropagate helper function which can be used in defer ErrPropagate()
func ErrPropagate(api string) {
if err := recover(); err != nil {
log.Println("ERROR", api, "error", err, Stack())
panic(fmt.Sprintf("%s:%s", api, err))
}
}
// ErrPropagate2Channel helper function which can be used in goroutines as
// ch := make(chan interface{})
//
// go func() {
// defer ErrPropagate2Channel(api, ch)
// someFunction()
// }()
func ErrPropagate2Channel(api string, ch chan interface{}) {
if err := recover(); err != nil {
log.Println("ERROR", api, "error", err, Stack())
ch <- fmt.Sprintf("%s:%s", api, err)
}
}
// GoDeferFunc runs any given function in defered go routine
func GoDeferFunc(api string, f func()) {
ch := make(chan interface{})
go func() {
defer ErrPropagate2Channel(api, ch)
f()
ch <- "ok" // send to channel that we can read it later in case of success of f()
}()
err := <-ch
if err != nil && err != "ok" {
panic(err)
}
}
// BasePath function provides end-point path for given api string
func BasePath(base, api string) string {
if base != "" {
if strings.HasPrefix(api, "/") {
api = strings.Replace(api, "/", "", 1)
}
if strings.HasPrefix(base, "/") {
return fmt.Sprintf("%s/%s", base, api)
}
return fmt.Sprintf("/%s/%s", base, api)
}
return api
}
// GetHash generates SHA256 hash for given data blob
func GetHash(data []byte) string {
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])
}
// helper function to extract file name
func FileName(fname string) string {
arr := strings.Split(fname, "/")
f := arr[len(arr)-1]
arr = strings.Split(f, ".")
return arr[0]
}
// FindFiles find files in given path
func FindFiles(root string) []string {
var files []string
if root == "" {
return files
}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Printf("WARNING: unable to access %s/%s, error %v", root, path, err)
}
// log.Printf("dir: %v: name: %s\n", info.IsDir(), path)
files = append(files, path)
return nil
})
if err != nil {
log.Printf("FindFiles root %v, error %v\n", root, err)
}
return files
}
// TimeFormat helper function to convert Unix time into human readable form
func TimeFormat(ts interface{}) string {
var err error
var t int64
switch v := ts.(type) {
case int:
t = int64(v)
case int32:
t = int64(v)
case int64:
t = v
case float64:
t = int64(v)
case string:
t, err = strconv.ParseInt(v, 0, 64)
if err != nil {
return fmt.Sprintf("%v", ts)
}
default:
return fmt.Sprintf("%v", ts)
}
layout := "2006-01-02 15:04:05"
return time.Unix(t, 0).UTC().Format(layout)
}
// SizeFormat helper function to convert size into human readable form
func SizeFormat(val interface{}) string {
var size float64
var err error
switch v := val.(type) {
case int:
size = float64(v)
case int32:
size = float64(v)
case int64:
size = float64(v)
case float64:
size = v
case string:
size, err = strconv.ParseFloat(v, 64)
if err != nil {
return fmt.Sprintf("%v", val)
}
default:
return fmt.Sprintf("%v", val)
}
base := 1000. // CMS convert is to use power of 10
xlist := []string{"", "KB", "MB", "GB", "TB", "PB"}
for _, vvv := range xlist {
if size < base {
return fmt.Sprintf("%v (%3.1f%s)", val, size, vvv)
}
size = size / base
}
return fmt.Sprintf("%v (%3.1f%s)", val, size, xlist[len(xlist)])
}
// GetEnv fetches value from user environement
func GetEnv(key string) string {
for _, item := range os.Environ() {
value := strings.Split(item, "=")
if value[0] == key {
return value[1]
}
}
return ""
}
// FullPath returns full path of given file name wrt to current location
func FullPath(fname string) string {
if !strings.HasPrefix(fname, "/") {
// we got relative path (e.g. server_test.json)
if wdir, err := os.Getwd(); err == nil {
fname = filepath.Join(wdir, fname)
}
}
return fname
}