-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisprod.go
More file actions
156 lines (129 loc) · 4.12 KB
/
Copy pathisprod.go
File metadata and controls
156 lines (129 loc) · 4.12 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
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
package isprod
import (
"fmt"
"os"
"strings"
)
// DefaultConditions is a list of conditions that are used by default.
// It's initialized at package init.
var DefaultConditions Conditions
func init() {
commonProdNamesBase := []string{
"prod",
"production",
"staging",
"live",
"ci",
}
commonEnvVariableNamesBase := []string{
"env",
"environment",
"mode",
}
// Add UPPERCASE versions of the common names.
commonProdNamesUpper := make([]string, len(commonProdNamesBase))
for i, name := range commonProdNamesBase {
commonProdNamesUpper[i] = strings.ToUpper(name)
}
commonProdNames := make([]string, 0, len(commonProdNamesBase)+len(commonProdNamesUpper))
commonProdNames = append(commonProdNames, commonProdNamesBase...)
commonProdNames = append(commonProdNames, commonProdNamesUpper...)
for _, name := range commonProdNames {
DefaultConditions.Add(Condition{
EnvVarName: name,
AllowedValues: nil,
AllowAnyValue: true,
ExcludedValues: []string{"false"},
})
}
// Add UPPERCASE versions of the common names.
commonEnvVariableNamesUpper := make([]string, len(commonEnvVariableNamesBase))
for i, name := range commonEnvVariableNamesBase {
commonEnvVariableNamesUpper[i] = strings.ToUpper(name)
}
commonEnvVariableNames := make([]string, 0, len(commonEnvVariableNamesBase)+len(commonEnvVariableNamesUpper))
commonEnvVariableNames = append(commonEnvVariableNames, commonEnvVariableNamesBase...)
commonEnvVariableNames = append(commonEnvVariableNames, commonEnvVariableNamesUpper...)
for _, name := range commonEnvVariableNames {
DefaultConditions.Add(Condition{
EnvVarName: name,
AllowedValues: commonProdNames,
AllowAnyValue: false,
ExcludedValues: nil,
})
}
}
// Condition is a condition that checks if the environment is production.
type Condition struct {
// EnvVarName is the name of the environment variable to check.
EnvVarName string
// AllowedValues is a list of values that are considered valid for the environment variable.
AllowedValues []string
// AllowAnyValue can be set to true if any value for the environment variable is allowed.
AllowAnyValue bool
// ExcludedValues is a list of values that are specifically not allowed, even if AllowAnyValue is set to true.
ExcludedValues []string
}
// Check checks if the condition is met.
func (c Condition) Check() bool {
value, exists := os.LookupEnv(c.EnvVarName)
if !exists {
return false
}
value = strings.ToLower(value)
if c.AllowAnyValue {
if c.ExcludedValues != nil {
for _, excludedValue := range c.ExcludedValues {
if strings.EqualFold(value, excludedValue) {
return false
}
}
}
return true
}
if c.AllowedValues != nil {
for _, allowedValue := range c.AllowedValues {
if strings.EqualFold(value, allowedValue) {
return true
}
}
}
return false
}
func (c Condition) String() string {
if c.AllowAnyValue {
return fmt.Sprintf("If environment variable '%s' is set and its value is not one of [%s], consider it as production environment.",
c.EnvVarName, strings.Join(c.ExcludedValues, ", "))
}
return fmt.Sprintf("If environment variable '%s' is set and its value is one of [%s], consider it as production environment.",
c.EnvVarName, strings.Join(c.AllowedValues, ", "))
}
// Conditions is a list of conditions.
type Conditions []Condition
// Add adds a condition to the list.
func (c *Conditions) Add(condition Condition) {
*c = append(*c, condition)
}
// Check checks if any of the conditions is true.
func (c Conditions) Check() bool {
for _, condition := range c {
if condition.Check() {
return true
}
}
return false
}
// String returns a string representation of the conditions in plain english.
func (c Conditions) String() string {
conditionStrings := make([]string, 0, len(c))
for _, condition := range c {
conditionStrings = append(conditionStrings, condition.String())
}
return strings.Join(conditionStrings, "\n")
}
// Check checks if the application is running in production or not.
// It uses the DefaultConditions.
// If you want to use your own conditions, use the Conditions.Check() method.
func Check() bool {
return DefaultConditions.Check()
}