-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
57 lines (49 loc) · 1.3 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
package internal
import (
"encoding/json"
"io/fs"
"os"
"strconv"
)
const (
EnvNameServiceRestEnabled = "BLUDGEON_REST_ENABLED"
EnvNameServiceGrpcEnabled = "BLUDGEON_GRPC_ENABLED"
)
const (
DefaultServiceRestEnabled = true
DefaultServiceGrpcEnabled = true
)
type Configuration struct {
ServiceGrpcEnabled bool `json:"service_grpc_enabled"`
ServiceRestEnabled bool `json:"service_rest_enabled"`
}
func (c *Configuration) Read(configFile string) error {
bytes, err := os.ReadFile(configFile)
if err != nil {
return err
}
return json.Unmarshal(bytes, c)
}
func (c *Configuration) Write(configFile string) error {
bytes, err := json.MarshalIndent(&c, "", " ")
if err != nil {
return err
}
return os.WriteFile(configFile, bytes, fs.FileMode(0644))
}
func (c *Configuration) Default(pwd string) {
c.ServiceRestEnabled = DefaultServiceRestEnabled
c.ServiceGrpcEnabled = DefaultServiceGrpcEnabled
}
func (c *Configuration) FromEnv(pwd string, envs map[string]string) {
if s, ok := envs[EnvNameServiceRestEnabled]; ok {
if serviceRestEnabled, err := strconv.ParseBool(s); err == nil {
c.ServiceRestEnabled = serviceRestEnabled
}
}
if s, ok := envs[EnvNameServiceGrpcEnabled]; ok {
if serviceGrpcEnabled, err := strconv.ParseBool(s); err == nil {
c.ServiceGrpcEnabled = serviceGrpcEnabled
}
}
}