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