github.com/wfusion/gofusion@v1.1.14/common/env/env.go (about) 1 package env 2 3 import ( 4 "os" 5 "path/filepath" 6 ) 7 8 const ( 9 Dev = "dev" 10 Online = "online" 11 Staging = "staging" 12 CI = "ci" 13 ) 14 15 var ( 16 WorkDir string 17 env string 18 svcName string 19 ) 20 21 // GetEnv get environment ENV 22 func GetEnv() string { 23 if env != "" { 24 return env 25 } 26 if env = os.Getenv("ENV"); env == "" { 27 env = Dev 28 } 29 return env 30 } 31 32 // IsDEV is dev 33 func IsDEV() bool { 34 return GetEnv() == Dev 35 } 36 37 // IsOnline is online 38 func IsOnline() bool { 39 return GetEnv() == Online 40 } 41 42 // IsStaging is staging 43 func IsStaging() bool { 44 return GetEnv() == Staging 45 } 46 47 // IsCI is ci 48 func IsCI() bool { 49 return GetEnv() == CI 50 } 51 52 func SetSvcName(name string) { 53 svcName = name 54 } 55 56 func SvcName() string { 57 if svcName != "" { 58 return svcName 59 } 60 if svcName = os.Getenv("SVC_NAME"); svcName != "" { 61 return svcName 62 } 63 if svcName = os.Getenv("SERVICE_NAME"); svcName != "" { 64 return svcName 65 } 66 return "" 67 } 68 69 func init() { 70 GetEnv() 71 SvcName() 72 73 dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) 74 WorkDir = dir 75 }