-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.go
168 lines (134 loc) · 4.05 KB
/
env.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
package mikros
import (
"fmt"
"os"
"strings"
"github.com/mikros-dev/mikros/components/definition"
"github.com/mikros-dev/mikros/components/env"
)
const (
stringEnvNotation = "@env"
)
// Env is the main framework environment structure. It holds only variables
// common for the whole project.
//
// It is also the mechanism to hold all environment variables declared directly
// inside the 'service.toml' file.
type Env struct {
DeploymentEnv definition.ServiceDeploy `env:"MIKROS_SERVICE_DEPLOY,default_value=local"`
TrackerHeaderName string `env:"MIKROS_TRACKER_HEADER_NAME,default_value=X-Request-ID"`
// CI/CD settings
IsCICD bool `env:"MIKROS_CICD_TEST,default_value=false"`
// Coupled clients
CoupledNamespace string `env:"MIKROS_COUPLED_NAMESPACE"`
CoupledPort int32 `env:"MIKROS_COUPLED_PORT,default_value=7070"`
// Default connection ports
GrpcPort int32 `env:"MIKROS_GRPC_PORT,default_value=7070"`
HttpPort int32 `env:"MIKROS_HTTP_PORT,default_value=8080"`
// definedEnvs holds all variables pointed directly into the 'service.toml'
// file.
definedEnvs map[string]string `env:",skip"`
}
func newEnv(defs *definition.Definitions) (*Env, error) {
var envs Env
if err := env.Load(defs.ServiceName(), &envs); err != nil {
return nil, err
}
envs.autoAdjust()
// Load service defined environment variables (through service.toml 'envs' key)
definedEnvs, err := loadDefinedEnvVars(defs)
if err != nil {
return nil, err
}
envs.definedEnvs = definedEnvs
return &envs, nil
}
// loadDefinedEnvVars loads envs defined in the 'service.toml' file as mandatory
// values, í.e., they must be available when the service starts.
func loadDefinedEnvVars(defs *definition.Definitions) (map[string]string, error) {
var (
envs = make(map[string]string)
)
for _, e := range defs.Envs {
v, err := mustGetEnv(e)
if err != nil {
return nil, err
}
envs[e] = v
}
return envs, nil
}
// mustGetEnv retrieves a value from an environment variable and aborts
// if it is not set.
func mustGetEnv(name string) (string, error) {
value := os.Getenv(name)
if value == "" {
return "", fmt.Errorf("environment variable '%v' must be set", name)
}
return value, nil
}
// autoAdjust verifies if the local environment has any modification that
// needs to be reflected in the structure members.
func (e *Env) autoAdjust() {
// Checks our real deployment environment
if e.isRunningTest() {
e.DeploymentEnv = definition.ServiceDeploy_Test
}
}
// isRunningTest returns if the current session is being executed in test mode.
func (e *Env) isRunningTest() bool {
for _, arg := range os.Args {
if strings.HasSuffix(arg, ".test") || strings.Contains(arg, "-test") {
return true
}
}
return false
}
func (e *Env) DefinedEnv(name string) (string, bool) {
v, ok := e.definedEnvs[name]
return v, ok
}
func (e *Env) ToMapEnv() *MapEnv {
return &MapEnv{
env: e,
}
}
// MapEnv is an Env subtype that can be passed to the plugin package options.
type MapEnv struct {
env *Env
}
func (m *MapEnv) DeploymentEnv() definition.ServiceDeploy {
return m.env.DeploymentEnv
}
func (m *MapEnv) TrackerHeaderName() string {
return m.env.TrackerHeaderName
}
func (m *MapEnv) IsCICD() bool {
return m.env.IsCICD
}
func (m *MapEnv) CoupledNamespace() string {
return m.env.CoupledNamespace
}
func (m *MapEnv) CoupledPort() int32 {
return m.env.CoupledPort
}
func (m *MapEnv) GrpcPort() int32 {
return m.env.GrpcPort
}
func (m *MapEnv) HttpPort() int32 {
return m.env.HttpPort
}
func (m *MapEnv) Get(key string) interface{} {
key = strings.TrimSuffix(key, stringEnvNotation)
return m.env.definedEnvs[key]
}
// HasEnvNotation checks if a string has the mikros framework env notation
// indicating that it should be loaded from environment variables.
func HasEnvNotation(s string) bool {
return strings.HasSuffix(s, stringEnvNotation)
}
// GetEnv is a helper function that retrieves a value from an environment
// variable independently if is has the env notation or not.
func GetEnv(s string) string {
return os.Getenv(strings.TrimSuffix(s, stringEnvNotation))
}