generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 73
/
utils.go
412 lines (356 loc) · 9.43 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
mathrand "math/rand"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
)
// transforms the keys of a Map recursively with the given transform function
func TransformMapKeys(input map[string]interface{}, transformFunc func(string) string) map[string]interface{} {
output := make(map[string]interface{})
for k, v := range input {
transformedKey := transformFunc(k)
value, ok := v.(map[string]interface{})
if !ok {
output[transformedKey] = v
} else {
output[transformedKey] = TransformMapKeys(value, transformFunc)
}
}
return output
}
// unmarshal returns parses the JSON config data and stores the value in the reference to result
func Unmarshal(obj string, result interface{}) error {
obj = strings.TrimSpace(obj)
err := json.Unmarshal([]byte(obj), result)
if err != nil {
if e, ok := err.(*json.SyntaxError); ok {
return ErrUnmarshalSyntax(err, e.Offset)
}
if e, ok := err.(*json.UnmarshalTypeError); ok {
return ErrUnmarshalType(err, e.Value)
}
if e, ok := err.(*json.UnsupportedTypeError); ok {
return ErrUnmarshalUnsupportedType(err, e.Type)
}
if e, ok := err.(*json.UnsupportedValueError); ok {
return ErrUnmarshalUnsupportedValue(err, e.Value)
}
if e, ok := err.(*json.InvalidUnmarshalError); ok {
return ErrUnmarshalInvalid(err, e.Type)
}
return ErrUnmarshal(err)
}
return nil
}
// getBool function returns the boolean config data
func GetBool(key string) (bool, error) {
enabled, err := strconv.ParseBool(key)
if err != nil {
return false, ErrGetBool(key, err)
}
return enabled, nil
}
func StrConcat(s ...string) string {
var buf strings.Builder
for _, str := range s {
buf.WriteString(str)
}
return buf.String()
}
func Marshal(obj interface{}) (string, error) {
result, err := json.Marshal(obj)
if err != nil {
return " ", ErrMarshal(err)
}
return string(result), nil
}
func Filepath() string {
_, fn, line, _ := runtime.Caller(0)
return fmt.Sprintf("file: %s, line: %d", fn, line)
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("failed to get the file %d status code for %s file", resp.StatusCode, url)
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
// GetHome returns the home path
func GetHome() string {
usr, _ := user.Current()
return usr.HomeDir
}
// CreateFile creates a file with the given content on the given location with
// the given filename
func CreateFile(contents []byte, filename string, location string) error {
// Create file in -rw-r--r-- mode
fd, err := os.OpenFile(filepath.Join(location, filename), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
if _, err = fd.Write(contents); err != nil {
fd.Close()
return err
}
if err = fd.Close(); err != nil {
return err
}
return nil
}
// ReadFileSource supports "http", "https" and "file" protocols.
// it takes in the location as a uri and returns the contents of
// file as a string.
func ReadFileSource(uri string) (string, error) {
if strings.HasPrefix(uri, "http") {
return ReadRemoteFile(uri)
}
if strings.HasPrefix(uri, "file") {
return ReadLocalFile(uri)
}
return "", ErrInvalidProtocol
}
// ReadRemoteFile takes in the location of a remote file
// in the format 'http://location/of/file' or 'https://location/file'
// and returns the content of the file if the location is valid and
// no error occurs
func ReadRemoteFile(url string) (string, error) {
response, err := http.Get(url)
if err != nil {
return " ", err
}
if response.StatusCode == http.StatusNotFound {
return " ", ErrRemoteFileNotFound(url)
}
defer response.Body.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, response.Body)
if err != nil {
return " ", ErrReadingRemoteFile(err)
}
return buf.String(), nil
}
// ReadLocalFile takes in the location of a local file
// in the format `file://location/of/file` and returns
// the content of the file if the path is valid and no
// error occurs
func ReadLocalFile(location string) (string, error) {
// remove the protocol prefix
location = strings.TrimPrefix(location, "file://")
// Need to support variable file locations hence
// #nosec
data, err := os.ReadFile(location)
if err != nil {
return "", ErrReadingLocalFile(err)
}
return string(data), nil
}
// Gets the latest stable release tags from github for a given org name and repo name(in that org) in sorted order
func GetLatestReleaseTagsSorted(org string, repo string) ([]string, error) {
var url string = "https://github.com/" + org + "/" + repo + "/releases"
resp, err := http.Get(url)
if err != nil {
return nil, ErrGettingLatestReleaseTag(err)
}
defer safeClose(resp.Body)
if resp.StatusCode != http.StatusOK {
return nil, ErrGettingLatestReleaseTag(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, ErrGettingLatestReleaseTag(err)
}
re := regexp.MustCompile("/releases/tag/(.*?)\"")
releases := re.FindAllString(string(body), -1)
if len(releases) == 0 {
return nil, ErrGettingLatestReleaseTag(errors.New("no release found in this repository"))
}
var versions []string
for _, rel := range releases {
latest := strings.ReplaceAll(rel, "/releases/tag/", "")
latest = strings.ReplaceAll(latest, "\"", "")
versions = append(versions, latest)
}
versions = SortDottedStringsByDigits(versions)
return versions, nil
}
// SafeClose is a helper function help to close the io
func safeClose(co io.Closer) {
if cerr := co.Close(); cerr != nil {
log.Error(cerr)
}
}
func Contains[G []K, K comparable](slice G, ele K) bool {
for _, item := range slice {
if item == ele {
return true
}
}
return false
}
func Cast[K any](val interface{}) (K, error) {
var assertedValue K
if IsInterfaceNil(val) {
return assertedValue, ErrTypeCast(fmt.Errorf("nil interface cannot be type casted"))
}
var ok bool
assertedValue, ok = val.(K)
if !ok {
return assertedValue, ErrTypeCast(fmt.Errorf("the underlying type of the interface is %s", reflect.TypeOf(val).Name()))
}
return assertedValue, nil
}
func MarshalAndUnmarshal[fromType any, toType any](val fromType) (unmarshalledvalue toType, err error) {
data, err := Marshal(val)
if err != nil {
return
}
err = Unmarshal(data, &unmarshalledvalue)
if err != nil {
return
}
return
}
func IsClosed[K any](ch chan K) bool {
select {
case <-ch:
return true
default:
return false
}
}
// WriteToFile writes the given content to the given file path
func WriteToFile(path string, content string) error {
file, err := os.Create(path)
if err != nil {
return ErrCreateFile(err, path)
}
_, err = file.WriteString(content)
if err != nil {
return ErrWriteFile(err, path)
}
// Close the file to save the changes.
err = file.Close()
if err != nil {
return ErrWriteFile(err, path)
}
return nil
}
// FormatName formats the given string to by replacing " " with "-"
func FormatName(input string) string {
formatedName := strings.ReplaceAll(input, " ", "-")
formatedName = strings.ToLower(formatedName)
return formatedName
}
func GetRandomAlphabetsOfDigit(length int) (s string) {
charSet := "abcdedfghijklmnopqrstuvwxyz"
for i := 0; i < length; i++ {
random := mathrand.Intn(len(charSet))
randomChar := charSet[random]
s += string(randomChar)
}
return
}
// combineErrors merges a slice of error
// into one error separated by the given separator
func CombineErrors(errs []error, sep string) error {
if len(errs) == 0 {
return nil
}
var errString []string
for _, err := range errs {
errString = append(errString, err.Error())
}
return errors.New(strings.Join(errString, sep))
}
func MergeMaps(mergeInto, toMerge map[string]interface{}) map[string]interface{} {
if mergeInto == nil {
mergeInto = make(map[string]interface{})
}
for k, v := range toMerge {
mergeInto[k] = v
}
return mergeInto
}
func WriteJSONToFile[K any](outputPath string, data K) error {
byt, err := json.MarshalIndent(data, " ", " ")
if err != nil {
return ErrMarshal(err)
}
file, err := os.Create(outputPath)
if err != nil {
return ErrCreateFile(err, outputPath)
}
_, err = file.Write(byt)
if err != nil {
return ErrWriteFile(err, outputPath)
}
return nil
}
func CreateDirectory(path string) error {
err := os.MkdirAll(path, 0755)
if err != nil {
err = ErrCreateDir(err, path)
return err
}
return nil
}
func ReplaceSpacesAndConvertToLowercase(s string) string {
return strings.ToLower(strings.ReplaceAll(s, " ", ""))
}
func ExtractDomainFromURL(location string) string {
parsedURL, err := url.Parse(location)
// If unable to extract domain return the location as is.
if err != nil {
return location
}
return regexp.MustCompile(`(([a-zA-Z0-9]+\.)([a-zA-Z0-9]+))$`).FindString(parsedURL.Hostname())
}
func IsInterfaceNil(val interface{}) bool {
if val == nil {
return true
}
switch reflect.TypeOf(val).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(val).IsNil()
}
return false
}
func IsSchemaEmpty(schema string) (valid bool) {
if schema == "" {
return
}
m := make(map[string]interface{})
_ = json.Unmarshal([]byte(schema), &m)
if m["properties"] == nil {
return
}
valid = true
return
}