-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
74 lines (64 loc) · 1.51 KB
/
helper.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
package helper
import (
"fmt"
"log"
"os"
"strings"
)
const WrongMessage = "What are you doing here?"
type Message struct {
Message string `json:"message"`
}
// MustGetenv validates if the environment variable is set and returns it
// otherwise it will log a fatal error and exit the program
// It is used to validate the environment variables
// Example:
// - os.Setenv("TEST", "test")
// - log.Println(MustGetenv("TEST"))
// - log.Println(MustGetenv("TEST2"))
//
// Output:
// - test
// - 2021/08/31 11:30:00 Missing environment variable: TEST2
// - exit status 1
func MustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Fatalln(" Missing environment variable: ", k)
}
return v
}
func Logger(message string, level string) {
var (
project = getGCPProject()
)
if project != "" {
gcpLogger(message, level, project)
} else {
log.Print(level + ": " + message + "\n")
}
}
// ConvertToBoolPointer is a simple function that converts a interface to a *bool
func ConvertToBoolPointer(value interface{}) *bool {
switch v := value.(type) {
case *bool:
return v
case bool:
return &v
default:
return nil
}
}
// List2String converts a list of strings to a string in the format ["value1","value2"]
func List2String(values []string) string {
var response strings.Builder
response.WriteString("[")
for i, valor := range values {
response.WriteString(fmt.Sprintf(`"%s"`, valor))
if i < len(values)-1 {
response.WriteString(",")
}
}
response.WriteString("]")
return response.String()
}