-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
111 lines (90 loc) · 2.38 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
package model
import (
"encoding/json"
"fmt"
"log"
"os"
)
type Config struct {
ProjectID string
RegionID string
ConfigFile string
LogType string
OutputType string
}
type ConfigBuilder struct {
projectID string
regionID string
configFile string
logType string
outputType string
}
func (b *ConfigBuilder) ProjectID(p string) *ConfigBuilder {
b.projectID = p
return b
}
// TODO - Need to document allowed values and validate input
func (b *ConfigBuilder) RegionID(r string) *ConfigBuilder {
b.regionID = r
return b
}
func (b *ConfigBuilder) ConfigFile(configFile string) *ConfigBuilder {
b.configFile = configFile
return b
}
// TODO - Need to document allowed values and validate input
func (b *ConfigBuilder) LogType(logType string) *ConfigBuilder {
b.logType = logType
return b
}
// TODO - Need to document allowed values and validate input
func (b *ConfigBuilder) OutputType(outputType string) *ConfigBuilder {
b.outputType = outputType
return b
}
func (b *ConfigBuilder) Describe() string {
return fmt.Sprintf("%+v", b)
}
func (b *ConfigBuilder) Build() (Config, error) {
cfg := Config{}
if b.projectID == "" {
log.Fatalln("Need a valid GCP project ID")
} else {
cfg.ProjectID = b.projectID
}
if b.regionID == "" {
cfg.RegionID = "us-central1"
} else {
cfg.RegionID = b.regionID
}
if b.configFile == "" {
cfg.ConfigFile = b.configFile
}
if b.logType == "" {
cfg.LogType = "none"
} else {
cfg.LogType = b.logType
}
if b.outputType == "" {
cfg.OutputType = b.outputType
}
return cfg, nil
}
// TODO - Revisit ReadModelConfigFile() and decide whether it should be exported or not
// and whether it should instead set the fields of the struct as a way
// to initialize a model configuration from a file. If thats the case, then this
// should be a method on the ConfigBuilder, that way when the Build() method is invoked
// it would validate the inputs and return a valid Config instance.
// readModelConfigFile reads the model configuration file (JSON text file)
func (cfg Config) ReadModelConfigFile() (map[string]interface{}, error) {
var config map[string]interface{}
data, err := os.ReadFile(cfg.ConfigFile)
if err != nil {
return config, fmt.Errorf("error reading model config: %v", err)
}
err = json.Unmarshal(data, &config)
if err != nil {
return config, fmt.Errorf("error unmarshalling model config: %v", err)
}
return config, nil
}