env.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package env
  2. import (
  3. "flag"
  4. "fmt"
  5. "strings"
  6. )
  7. var (
  8. active Environment
  9. dev Environment = &environment{value: "dev"}
  10. fat Environment = &environment{value: "fat"}
  11. uat Environment = &environment{value: "uat"}
  12. pro Environment = &environment{value: "pro"}
  13. )
  14. var _ Environment = (*environment)(nil)
  15. // Environment 环境配置
  16. type Environment interface {
  17. Value() string
  18. IsDev() bool
  19. IsFat() bool
  20. IsUat() bool
  21. IsPro() bool
  22. t()
  23. }
  24. type environment struct {
  25. value string
  26. }
  27. func (e *environment) Value() string {
  28. return e.value
  29. }
  30. func (e *environment) IsDev() bool {
  31. return e.value == "dev"
  32. }
  33. func (e *environment) IsFat() bool {
  34. return e.value == "fat"
  35. }
  36. func (e *environment) IsUat() bool {
  37. return e.value == "uat"
  38. }
  39. func (e *environment) IsPro() bool {
  40. return e.value == "pro"
  41. }
  42. func (e *environment) t() {}
  43. func init() {
  44. env := flag.String("env", "", "请输入运行环境:\n dev:开发环境\n fat:测试环境\n uat:预上线环境\n pro:正式环境\n")
  45. flag.Parse()
  46. switch strings.ToLower(strings.TrimSpace(*env)) {
  47. case "dev":
  48. active = dev
  49. case "fat":
  50. active = fat
  51. case "uat":
  52. active = uat
  53. case "pro":
  54. active = pro
  55. default:
  56. active = dev
  57. fmt.Println("Warning: '-env' cannot be found, or it is illegal. The default 'dev' will be used.")
  58. }
  59. }
  60. // Active 当前配置的env
  61. func Active() Environment {
  62. return active
  63. }