genv.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. "fmt"
  10. "os"
  11. "strings"
  12. "github.com/gogf/gf/v2/container/gvar"
  13. "github.com/gogf/gf/v2/errors/gerror"
  14. "github.com/gogf/gf/v2/internal/command"
  15. "github.com/gogf/gf/v2/internal/utils"
  16. )
  17. // All returns a copy of strings representing the environment,
  18. // in the form "key=value".
  19. func All() []string {
  20. return os.Environ()
  21. }
  22. // Map returns a copy of strings representing the environment as a map.
  23. func Map() map[string]string {
  24. return MapFromEnv(os.Environ())
  25. }
  26. // Get creates and returns a Var with the value of the environment variable
  27. // named by the `key`. It uses the given `def` if the variable does not exist
  28. // in the environment.
  29. func Get(key string, def ...interface{}) *gvar.Var {
  30. v, ok := os.LookupEnv(key)
  31. if !ok {
  32. if len(def) > 0 {
  33. return gvar.New(def[0])
  34. }
  35. return nil
  36. }
  37. return gvar.New(v)
  38. }
  39. // Set sets the value of the environment variable named by the `key`.
  40. // It returns an error, if any.
  41. func Set(key, value string) (err error) {
  42. err = os.Setenv(key, value)
  43. if err != nil {
  44. err = gerror.Wrapf(err, `set environment key-value failed with key "%s", value "%s"`, key, value)
  45. }
  46. return
  47. }
  48. // SetMap sets the environment variables using map.
  49. func SetMap(m map[string]string) (err error) {
  50. for k, v := range m {
  51. if err = Set(k, v); err != nil {
  52. return err
  53. }
  54. }
  55. return nil
  56. }
  57. // Contains checks whether the environment variable named `key` exists.
  58. func Contains(key string) bool {
  59. _, ok := os.LookupEnv(key)
  60. return ok
  61. }
  62. // Remove deletes one or more environment variables.
  63. func Remove(key ...string) (err error) {
  64. for _, v := range key {
  65. if err = os.Unsetenv(v); err != nil {
  66. err = gerror.Wrapf(err, `delete environment key failed with key "%s"`, v)
  67. return err
  68. }
  69. }
  70. return nil
  71. }
  72. // GetWithCmd returns the environment value specified `key`.
  73. // If the environment value does not exist, then it retrieves and returns the value from command line options.
  74. // It returns the default value `def` if none of them exists.
  75. //
  76. // Fetching Rules:
  77. // 1. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
  78. // 2. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
  79. func GetWithCmd(key string, def ...interface{}) *gvar.Var {
  80. envKey := utils.FormatEnvKey(key)
  81. if v := os.Getenv(envKey); v != "" {
  82. return gvar.New(v)
  83. }
  84. cmdKey := utils.FormatCmdKey(key)
  85. if v := command.GetOpt(cmdKey); v != "" {
  86. return gvar.New(v)
  87. }
  88. if len(def) > 0 {
  89. return gvar.New(def[0])
  90. }
  91. return nil
  92. }
  93. // Build builds a map to an environment variable slice.
  94. func Build(m map[string]string) []string {
  95. array := make([]string, len(m))
  96. index := 0
  97. for k, v := range m {
  98. array[index] = k + "=" + v
  99. index++
  100. }
  101. return array
  102. }
  103. // MapFromEnv converts environment variables from slice to map.
  104. func MapFromEnv(envs []string) map[string]string {
  105. m := make(map[string]string)
  106. i := 0
  107. for _, s := range envs {
  108. i = strings.IndexByte(s, '=')
  109. m[s[0:i]] = s[i+1:]
  110. }
  111. return m
  112. }
  113. // MapToEnv converts environment variables from map to slice.
  114. func MapToEnv(m map[string]string) []string {
  115. envs := make([]string, 0)
  116. for k, v := range m {
  117. envs = append(envs, fmt.Sprintf(`%s=%s`, k, v))
  118. }
  119. return envs
  120. }
  121. // Filter filters repeated items from given environment variables.
  122. func Filter(envs []string) []string {
  123. return MapToEnv(MapFromEnv(envs))
  124. }