-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
72 lines (63 loc) · 1.64 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
package config
import (
"bytes"
"encoding/json"
"github.com/vrischmann/envconfig"
"io/ioutil"
"os"
"reflect"
)
// LoadConfig loads a TemporalConfig from given filepath
func LoadConfig(configPath string) (*TemporalConfig, error) {
// if configPath is empty, load from env
if configPath == "" {
conf, err := LoadConfigFromEnv()
if err != nil {
return nil, err
}
// this will pass if we failed to pull config
// from the environment, and will then default
// to config file path based loading
if !reflect.DeepEqual(&TemporalConfig{}, conf) {
return conf, nil
}
}
raw, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}
var tCfg TemporalConfig
if err = json.Unmarshal(raw, &tCfg); err != nil {
return nil, err
}
tCfg.setDefaults()
return &tCfg, nil
}
// LoadConfigFromEnv is used to load a TemporalConfig object us env vars
func LoadConfigFromEnv() (*TemporalConfig, error) {
cfg := &TemporalConfig{}
err := envconfig.Init(cfg)
return cfg, err
}
// GenerateConfig writes a empty TemporalConfig template to given filepath
func GenerateConfig(configPath string) error {
template := &TemporalConfig{}
template.setDefaults()
b, err := json.Marshal(template)
if err != nil {
return err
}
var pretty bytes.Buffer
if err = json.Indent(&pretty, b, "", "\t"); err != nil {
return err
}
return ioutil.WriteFile(configPath, pretty.Bytes(), os.ModePerm)
}
func (t *TemporalConfig) setDefaults() {
if t.LogDir == "" {
t.LogDir = "/var/log/temporal/"
}
if len(t.API.Connection.CORS.AllowedOrigins) == 0 {
t.API.Connection.CORS.AllowedOrigins = []string{"temporal.cloud", "backup.temporal.cloud"}
}
}