-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappenv.go
61 lines (55 loc) · 1.08 KB
/
appenv.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
package appenv
import (
"os"
"strings"
)
// AppEnv represents an application runtime environment.
type AppEnv uint8
const (
// Unknown represents the unknown environment.
Unknown AppEnv = iota
// Test represents the test environment.
Test
// Dev represents the development environment.
Dev
// Stg represents the staging environment.
Stg
// Prod represents the production environment.
Prod
)
// String returns string environment.
func (e AppEnv) String() string {
switch e {
case Test:
return "test"
case Dev:
return "development"
case Stg:
return "staging"
case Prod:
return "production"
default:
return "unknown"
}
}
// AtoE converts from string to AppEnv.
func AtoE(str string) AppEnv {
str = strings.ToLower(str)
switch str {
case Test.String():
return Test
case Dev.String():
return Dev
case Stg.String():
return Stg
case Prod.String():
return Prod
default:
return Unknown
}
}
// Env gets the environment variable with the given key and returns the corresponding AppEnv.
func Env(key string) AppEnv {
str := os.Getenv(key)
return AtoE(str)
}