Total Lines | 48 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package env |
||
2 | |||
3 | import "os" |
||
4 | |||
5 | // IEnv is an interface for environment variables |
||
6 | type IEnv interface { |
||
7 | GetEnv(key string) string // GetEnv returns the value of the environment variable named by the key. |
||
8 | } |
||
9 | |||
10 | // Env is a struct for environment variables |
||
11 | type Env struct { |
||
12 | env IEnv // env is an interface for environment variables |
||
13 | } |
||
14 | |||
15 | // NewMyEnv returns a new Env struct |
||
16 | func NewMyEnv(env IEnv) *Env { |
||
17 | return &Env{env: env} |
||
18 | } |
||
19 | |||
20 | // GetEnv returns the value of the environment variable named by the key. |
||
21 | func (m *Env) GetEnv(key string) string { |
||
22 | return m.env.GetEnv(key) |
||
23 | } |
||
24 | |||
25 | // FakeEnv is a struct for fake environment variables |
||
26 | type FakeEnv struct{} |
||
27 | |||
28 | // GetEnv returns the value of the environment variable named by the key. |
||
29 | // It returns predefined value for testing. |
||
30 | func (*FakeEnv) GetEnv(key string) string { |
||
31 | fakeEnv := map[string]string{ |
||
32 | "APP_ENV": "dev", // development |
||
33 | "SECRET_KEY": "secret", // secret key |
||
34 | } |
||
35 | // return predefined value for existing key |
||
36 | if val, ok := fakeEnv[key]; ok { |
||
37 | return val |
||
38 | } |
||
39 | // return empty string for non-existing key |
||
40 | return "" |
||
41 | } |
||
42 | |||
43 | // OsEnv is a struct for os environment variables |
||
44 | type OsEnv struct{} |
||
45 | |||
46 | // GetEnv returns the value of the environment variable named by the key. |
||
47 | func (*OsEnv) GetEnv(key string) string { |
||
48 | return os.Getenv(key) // return os environment variable |
||
49 | } |
||
50 |