-
Notifications
You must be signed in to change notification settings - Fork 419
/
common.go
302 lines (254 loc) · 6.74 KB
/
common.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package sdk
import (
"bufio"
"bytes"
"crypto/md5"
"crypto/sha512"
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path"
"reflect"
"regexp"
"runtime"
"strings"
"unicode"
"github.com/go-gorp/gorp"
"github.com/pkg/errors"
)
// MaxIconSize is the maximum size of the icon in octet
const MaxIconSize = 120000
// IconFormat is the format prefix accepted for icon
const IconFormat = "data:image/"
// True of false
var (
True = true
False = false
TrueString = "true"
FalseString = "false"
)
// EncryptFunc is a common type
type EncryptFunc func(gorp.SqlExecutor, int64, string, string) (string, error)
// IDName is generally used when you want to get basic informations from db
type IDName struct {
ID int64 `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Description string `json:"description,omitempty" db:"description"`
Icon string `json:"icon,omitempty" db:"icon"`
Labels []Label `json:"labels,omitempty" db:"-"`
}
type IDNames []IDName
type EntitiesPermissions map[string]Permissions
func (e EntitiesPermissions) Level(s string) int {
if e == nil {
return 0
}
p, has := e[s]
if !has {
return 0
}
return p.Level()
}
func (e EntitiesPermissions) Permissions(s string) Permissions {
if e == nil {
return Permissions{}
}
p, has := e[s]
if !has {
return Permissions{}
}
return p
}
func (idNames IDNames) IDs() []int64 {
res := make([]int64, len(idNames))
for i := range idNames {
res[i] = idNames[i].ID
}
return res
}
func (idNames IDNames) Names() []string {
res := make([]string, len(idNames))
for i := range idNames {
res[i] = idNames[i].Name
}
return res
}
// NamePattern Pattern for project/application/pipeline/group name
const NamePattern = "^[a-zA-Z0-9._-]{1,}$"
// NamePatternRegex Pattern regexp
var NamePatternRegex = regexp.MustCompile(NamePattern)
// NamePatternSpace Pattern for stage name
const NamePatternSpace = `^[\sa-zA-Z0-9._-]{1,}$`
// NamePatternSpaceRegex Pattern regexp
var NamePatternSpaceRegex = regexp.MustCompile(NamePatternSpace)
// InterfaceSlice cast a untyped slice into a slice of untypes things. It will panic if the parameter is not a slice
func InterfaceSlice(slice interface{}) []interface{} {
s := reflect.ValueOf(slice)
if s.Kind() != reflect.Slice {
panic("interfaceSlice() given a non-slice type")
}
ret := make([]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
ret[i] = s.Index(i).Interface()
}
return ret
}
// JSONWithoutHTMLEncode return byte array of a struct into json without HTML encode
func JSONWithoutHTMLEncode(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}
// FileMd5sum returns the md5sum ofr a file
func FileMd5sum(filePath string) (string, error) {
file, errop := os.Open(filePath)
if errop != nil {
return "", fmt.Errorf("unable to copy file content to md5: %v", errop)
}
defer file.Close()
reader := bufio.NewReader(file)
hash := md5.New()
if _, err := io.Copy(hash, reader); err != nil {
return "", fmt.Errorf("error computing md5: %v", err)
}
hashInBytes := hash.Sum(nil)[:16]
sum := hex.EncodeToString(hashInBytes)
return sum, nil
}
func SHA512sum(s string) (string, error) {
hash := sha512.New()
if _, err := io.Copy(hash, strings.NewReader((s))); err != nil {
return "", fmt.Errorf("error computing sha512: %v", err)
}
hashInBytes := hash.Sum(nil)[:64]
sum := hex.EncodeToString(hashInBytes)
return sum, nil
}
// FileSHA512sum returns the sha512sum of a file
func FileSHA512sum(filePath string) (string, error) {
file, errop := os.Open(filePath)
if errop != nil {
return "", fmt.Errorf("error opening file for computing sha512: %v", errop)
}
defer file.Close()
reader := bufio.NewReader(file)
hash := sha512.New()
if _, err := io.Copy(hash, reader); err != nil {
return "", fmt.Errorf("error computing sha512: %v", err)
}
hashInBytes := hash.Sum(nil)[:64]
sum := hex.EncodeToString(hashInBytes)
return sum, nil
}
var rxURL = regexp.MustCompile(`http[s]?:\/\/(.*)`)
// IsURL returns if given path is a url according to the URL regex.
func IsURL(path string) bool {
return rxURL.MatchString(path)
}
// DirectoryExists checks if the directory exists
func DirectoryExists(path string) (bool, error) {
s, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return s.IsDir(), err
}
// StringSlice type used for database json storage.
type StringSlice []string
// Scan string slice.
func (s *StringSlice) Scan(src interface{}) error {
source, ok := src.([]byte)
if !ok {
return WithStack(errors.New("type assertion .([]byte) failed"))
}
return WrapError(json.Unmarshal(source, s), "cannot unmarshal StringSlice")
}
// Value returns driver.Value from string slice.
func (s StringSlice) Value() (driver.Value, error) {
j, err := json.Marshal(s)
return j, WrapError(err, "cannot marshal StringSlice")
}
func (s StringSlice) Contains(value string) bool {
for i := range s {
if s[i] == value {
return true
}
}
return false
}
// Int64Slice type used for database json storage.
type Int64Slice []int64
// Scan int64 slice.
func (s *Int64Slice) Scan(src interface{}) error {
source, ok := src.([]byte)
if !ok {
return WithStack(errors.New("type assertion .([]byte) failed"))
}
return WrapError(json.Unmarshal(source, s), "cannot unmarshal Int64Slice")
}
// Value returns driver.Value from int64 slice.
func (s Int64Slice) Value() (driver.Value, error) {
j, err := json.Marshal(s)
return j, WrapError(err, "cannot marshal Int64Slice")
}
// Contains return true if given value is in the slice.
func (s *Int64Slice) Contains(v int64) bool {
if s == nil || len(*s) == 0 {
return false
}
for _, i := range *s {
if i == v {
return true
}
}
return false
}
// Remove all occurrences of given value from the slice.
func (s *Int64Slice) Remove(v int64) {
filtered := make([]int64, 0, len(*s))
for _, i := range *s {
if i != v {
filtered = append(filtered, i)
}
}
*s = filtered
}
func RemoveNotPrintableChar(in string) string {
m := func(r rune) rune {
switch {
case unicode.IsLetter(r),
unicode.IsSpace(r),
unicode.IsDigit(r),
unicode.IsNumber(r),
unicode.In(r, unicode.Pd, // Filter some punctuation categories
unicode.Pe,
unicode.Pf,
unicode.Pi,
unicode.Po,
unicode.Ps,
),
unicode.IsSpace(r),
unicode.IsTitle(r),
unicode.IsUpper(r):
return r
default:
return ' '
}
}
return strings.Map(m, in)
}
var windowsPathRegex = regexp.MustCompile(`^[a-zA-Z]:\\[\\\S|*\S]?.*$`)
func PathIsAbs(s string) bool {
if GOOS == "windows" || runtime.GOOS == "windows" {
return windowsPathRegex.MatchString(s)
}
return path.IsAbs(s)
}