-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
125 lines (114 loc) · 3.47 KB
/
utils.go
File metadata and controls
125 lines (114 loc) · 3.47 KB
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
package helper
import (
"errors"
"os"
"strconv"
)
// EnvOrDefault returns the value of the environment variable k or defaultVal if it is not set.
func EnvOrDefault(k string, defaultVal string) string {
v := os.Getenv(k)
if v == "" {
return defaultVal
}
return v
}
// EnvOrDefaultInt64 returns the value of the environment variable k or defaultVal if it is not set.
// It panics if the value is not an int64.
// Note: Panics are not recoverable. Use this function only in main() or init() functions.
// Deprecated: Use EnvAsInt64OrDefault instead.
func EnvOrDefaultInt64(k string, defaultVal int64) int64 {
v := os.Getenv(k)
if v == "" {
return defaultVal
}
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(errors.New("environment variable " + k + " is not an int64"))
}
return i
}
// EnvAsIntOrDefault returns the value of the environment variable k or defaultVal as int if it is not set.
func EnvAsIntOrDefault(k string, defaultVal int) int {
v := os.Getenv(k)
if v == "" {
return defaultVal
}
i, err := strconv.Atoi(v)
if err != nil {
panic(errors.New("environment variable " + k + " is not an int"))
}
return i
}
// EnvAsInt64OrDefault returns the value of the environment variable k or defaultVal if it is not set.
// It panics if the value is not an int64.
// Note: Panics are not recoverable. Use this function only in main() or init() functions.
func EnvAsInt64OrDefault(k string, defaultVal int64) int64 {
v := os.Getenv(k)
if v == "" {
return defaultVal
}
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(errors.New("environment variable " + k + " is not an int64"))
}
return i
}
// EnvAsFloat32OrDefault returns the value of the environment variable k or defaultVal if it is not set.
// It panics if the value is not a float32.
// Note: Panics are not recoverable. Use this function only in main() or init() functions.
func EnvAsFloat32OrDefault(k string, defaultVal float32) float32 {
v := os.Getenv(k)
if v == "" {
return defaultVal
}
i, err := strconv.ParseFloat(v, 32)
if err != nil {
panic(errors.New("environment variable " + k + " is not a float32"))
}
return float32(i)
}
// EnvAsBoolOrDefault returns the value of the environment variable k or defaultVal if it is not set.
// It panics if the value is not a bool.
// Note: Panics are not recoverable. Use this function only in main() or init() functions.
func EnvAsBoolOrDefault(k string, defaultVal bool) bool {
v := os.Getenv(k)
if v == "" {
return defaultVal
}
i, err := strconv.ParseBool(v)
if err != nil {
panic(errors.New("environment variable " + k + " is not a bool"))
}
return i
}
// GetEnv returns the value of the environment variable k or if it is not set an error.
func GetEnv(k string) (string, error) {
if v, ok := os.LookupEnv(k); ok {
return v, nil
}
return "", errors.New("environment variable " + k + " not set")
}
// MustGetEnv returns the value of the environment variable k or panics if it is not set.
// Note: Panics are not recoverable. Use this function only in main() or init() functions.
// Otherwise, use GetEnv to handle the error.
func MustGetEnv(k string) string {
v := os.Getenv(k)
if v == "" {
panic("environment variable " + k + " not set")
}
return v
}
// Short returns a shortened string of length 7.
// Useful for shortening commit hashes.
// And nothing else.
// Really.
func Short(s string) string {
if len(s) > 7 {
return s[0:7]
}
return s
}
// Ptr returns a pointer to the given value.
func Ptr[T any](value T) *T {
return &value
}