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