-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathconfig.go
310 lines (275 loc) · 7.58 KB
/
config.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
package cmd
import (
"encoding/json"
"fmt"
"os"
"strconv"
"github.com/gilcrest/diygoapi/errs"
"github.com/gilcrest/diygoapi/sqldb"
)
const (
// local JSON Config File path - relative to project root
localJSONConfigFile = "./config/local.json"
// staging JSON Config File path - relative to project root
stagingJSONConfigFile = "./config/staging.json"
// production JSON Config File path - relative to project root
productionJSONConfigFile = "./config/production.json"
// genesisRequestFile is the local JSON Genesis Request File path
// (relative to project root)
genesisRequestFile = "./config/genesis/request.json"
)
// Env defines the environment
type Env uint8
const (
// Existing environment - current environment is not overridden
Existing Env = iota
// Local environment (Local machine)
Local
// Staging environment (GCP)
Staging
// Production environment (GCP)
Production
// Invalid defines an invalid environment option
Invalid Env = 99
)
func (e Env) String() string {
switch e {
case Existing:
return "existing"
case Local:
return "local"
case Staging:
return "staging"
case Production:
return "production"
case Invalid:
return "invalid"
}
return "unknown_env_config"
}
// ParseEnv converts an env string into an Env value.
// returns Invalid if the input string does not match known values.
func ParseEnv(envStr string) Env {
switch envStr {
case "existing":
return Existing
case "local":
return Local
case "staging":
return Staging
case "prod":
return Production
default:
return Invalid
}
}
// ConfigFile defines the configuration file. It is the superset of
// fields for the various environments/builds. For example, when setting
// the local environment based on the ConfigFile, you do not need
// to fill any of the GCP fields.
type ConfigFile struct {
Config struct {
HTTPServer struct {
ListenPort int `json:"listenPort"`
} `json:"httpServer"`
Logger struct {
MinLogLevel string `json:"minLogLevel"`
LogLevel string `json:"logLevel"`
LogErrorStack bool `json:"logErrorStack"`
} `json:"logger"`
Database struct {
Host string `json:"host"`
Port int `json:"port"`
Name string `json:"name"`
User string `json:"user"`
Password string `json:"password"`
SearchPath string `json:"searchPath"`
} `json:"database"`
EncryptionKey string `json:"encryptionKey"`
GCP struct {
ProjectID string `json:"projectID"`
ArtifactRegistry struct {
RepoLocation string `json:"repoLocation"`
RepoName string `json:"repoName"`
ImageID string `json:"imageID"`
Tag string `json:"tag"`
} `json:"artifactRegistry"`
CloudSQL struct {
InstanceName string `json:"instanceName"`
InstanceConnectionName string `json:"instanceConnectionName"`
} `json:"cloudSQL"`
CloudRun struct {
ServiceName string `json:"serviceName"`
} `json:"cloudRun"`
} `json:"gcp"`
} `json:"config"`
}
// LoadEnv conditionally sets the environment from a config file
// relative to whichever environment is being set. If Existing is
// passed as EnvConfig, the current environment is used and not overridden.
func LoadEnv(env Env) (err error) {
const op errs.Op = "cmd/LoadEnv"
var f ConfigFile
f, err = NewConfigFile(env)
if err != nil {
return errs.E(op, err)
}
err = overrideEnv(f)
if err != nil {
return errs.E(op, err)
}
return nil
}
// overrideEnv sets the environment
func overrideEnv(f ConfigFile) error {
const op errs.Op = "cmd/overrideEnv"
var err error
// minimum accepted log level
err = os.Setenv(logLevelMinEnv, f.Config.Logger.MinLogLevel)
if err != nil {
return errs.E(op, err)
}
// log level
err = os.Setenv(loglevelEnv, f.Config.Logger.LogLevel)
if err != nil {
return errs.E(op, err)
}
// log error stack
err = os.Setenv(logErrorStackEnv, fmt.Sprintf("%t", f.Config.Logger.LogErrorStack))
if err != nil {
return errs.E(op, err)
}
// server port
err = os.Setenv(portEnv, strconv.Itoa(f.Config.HTTPServer.ListenPort))
if err != nil {
return errs.E(op, err)
}
// database host
err = os.Setenv(sqldb.DBHostEnv, f.Config.Database.Host)
if err != nil {
return errs.E(op, err)
}
// database port
err = os.Setenv(sqldb.DBPortEnv, strconv.Itoa(f.Config.Database.Port))
if err != nil {
return errs.E(op, err)
}
// database name
err = os.Setenv(sqldb.DBNameEnv, f.Config.Database.Name)
if err != nil {
return errs.E(op, err)
}
// database user
err = os.Setenv(sqldb.DBUserEnv, f.Config.Database.User)
if err != nil {
return errs.E(op, err)
}
// database user password
err = os.Setenv(sqldb.DBPasswordEnv, f.Config.Database.Password)
if err != nil {
return errs.E(op, err)
}
// database search path
err = os.Setenv(sqldb.DBSearchPathEnv, f.Config.Database.SearchPath)
if err != nil {
return errs.E(op, err)
}
// encryption key
err = os.Setenv(encryptKeyEnv, f.Config.EncryptionKey)
if err != nil {
return errs.E(op, err)
}
return nil
}
// NewConfigFile initializes a ConfigFile struct from a JSON file at a
// predetermined file path for each environment (paths are relative to project root)
//
// Production: ./config/production.json
//
// Staging: ./config/staging.json
//
// Local: ./config/local.json
func NewConfigFile(env Env) (ConfigFile, error) {
const op errs.Op = "cmd/NewConfigFile"
var (
b []byte
err error
)
switch env {
case Existing:
return ConfigFile{}, nil
case Local:
b, err = os.ReadFile(localJSONConfigFile)
if err != nil {
return ConfigFile{}, errs.E(op, err)
}
case Staging:
b, err = os.ReadFile(stagingJSONConfigFile)
if err != nil {
return ConfigFile{}, errs.E(op, err)
}
case Production:
b, err = os.ReadFile(productionJSONConfigFile)
if err != nil {
return ConfigFile{}, errs.E(op, err)
}
default:
return ConfigFile{}, errs.E(op, "Invalid environment")
}
f := ConfigFile{}
err = json.Unmarshal(b, &f)
if err != nil {
return ConfigFile{}, errs.E(op, err)
}
return f, nil
}
// ConfigCueFilePaths defines the paths for config files processed through CUE.
type ConfigCueFilePaths struct {
// Input defines the list of paths for files to be taken as input for CUE
Input []string
// Output defines the path for the JSON output of CUE
Output string
}
// CUEPaths returns the ConfigCueFilePaths given the environment.
// Paths are relative to the project root.
func CUEPaths(env Env) (ConfigCueFilePaths, error) {
const (
schemaInput = "./config/cue/schema.cue"
localInput = "./config/cue/local.cue"
stagingInput = "./config/cue/staging.cue"
prodInput = "./config/cue/production.cue"
op errs.Op = "cmd/CUEPaths"
)
switch env {
case Local:
return ConfigCueFilePaths{
Input: []string{schemaInput, localInput},
Output: localJSONConfigFile,
}, nil
case Staging:
return ConfigCueFilePaths{
Input: []string{schemaInput, stagingInput},
Output: stagingJSONConfigFile,
}, nil
case Production:
return ConfigCueFilePaths{
Input: []string{schemaInput, prodInput},
Output: productionJSONConfigFile,
}, nil
default:
return ConfigCueFilePaths{}, errs.E(op, fmt.Sprintf("There is no path configuration for the %s environment", env))
}
}
// CUEGenesisPaths returns the ConfigCueFilePaths for the Genesis config.
// Paths are relative to the project root.
func CUEGenesisPaths() ConfigCueFilePaths {
const (
schemaInput = "./config/genesis/cue/schema.cue"
authInput = "./config/genesis/cue/auth.cue"
userInput = "./config/genesis/cue/input.cue"
)
return ConfigCueFilePaths{
Input: []string{schemaInput, authInput, userInput},
Output: genesisRequestFile,
}
}