genv.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package genv provides operations for environment variables of system.
  7. package genv
  8. import (
  9. "github.com/gogf/gf/container/gvar"
  10. "github.com/gogf/gf/os/gcmd"
  11. "os"
  12. "strings"
  13. )
  14. // All returns a copy of strings representing the environment,
  15. // in the form "key=value".
  16. func All() []string {
  17. return os.Environ()
  18. }
  19. // Map returns a copy of strings representing the environment as a map.
  20. func Map() map[string]string {
  21. m := make(map[string]string)
  22. i := 0
  23. for _, s := range os.Environ() {
  24. i = strings.IndexByte(s, '=')
  25. m[s[0:i]] = s[i+1:]
  26. }
  27. return m
  28. }
  29. // Get returns the value of the environment variable named by the <key>.
  30. // It returns given <def> if the variable does not exist in the environment.
  31. func Get(key string, def ...string) string {
  32. v, ok := os.LookupEnv(key)
  33. if !ok && len(def) > 0 {
  34. return def[0]
  35. }
  36. return v
  37. }
  38. // GetVar creates and returns a Var with the value of the environment variable
  39. // named by the <key>. It uses the given <def> if the variable does not exist
  40. // in the environment.
  41. func GetVar(key string, def ...interface{}) *gvar.Var {
  42. v, ok := os.LookupEnv(key)
  43. if !ok && len(def) > 0 {
  44. return gvar.New(def[0])
  45. }
  46. return gvar.New(v)
  47. }
  48. // Set sets the value of the environment variable named by the <key>.
  49. // It returns an error, if any.
  50. func Set(key, value string) error {
  51. return os.Setenv(key, value)
  52. }
  53. // SetMap sets the environment variables using map.
  54. func SetMap(m map[string]string) error {
  55. for k, v := range m {
  56. if err := os.Setenv(k, v); err != nil {
  57. return err
  58. }
  59. }
  60. return nil
  61. }
  62. // Contains checks whether the environment variable named <key> exists.
  63. func Contains(key string) bool {
  64. _, ok := os.LookupEnv(key)
  65. return ok
  66. }
  67. // Remove deletes one or more environment variables.
  68. func Remove(key ...string) error {
  69. var err error
  70. for _, v := range key {
  71. err = os.Unsetenv(v)
  72. if err != nil {
  73. return err
  74. }
  75. }
  76. return nil
  77. }
  78. // GetWithCmd returns the environment value specified <key>.
  79. // If the environment value does not exist, then it retrieves and returns the value from command line options.
  80. // It returns the default value <def> if none of them exists.
  81. //
  82. // Fetching Rules:
  83. // 1. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
  84. // 2. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
  85. func GetWithCmd(key string, def ...interface{}) *gvar.Var {
  86. value := interface{}(nil)
  87. if len(def) > 0 {
  88. value = def[0]
  89. }
  90. envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
  91. if v := os.Getenv(envKey); v != "" {
  92. value = v
  93. } else {
  94. cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
  95. if v := gcmd.GetOpt(cmdKey); v != "" {
  96. value = v
  97. }
  98. }
  99. return gvar.New(value)
  100. }
  101. // Build builds a map to a environment variable slice.
  102. func Build(m map[string]string) []string {
  103. array := make([]string, len(m))
  104. index := 0
  105. for k, v := range m {
  106. array[index] = k + "=" + v
  107. index++
  108. }
  109. return array
  110. }