forked from cockroachdb/cockroach
/
env.go
197 lines (175 loc) · 5.46 KB
/
env.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Raphael 'kena' Poss (knz@cockroachlabs.com)
package envutil
import (
"bytes"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/cockroachdb/cockroach/util/humanizeutil"
"github.com/cockroachdb/cockroach/util/log"
)
type envVarInfo struct {
consumer string
present bool
value string
}
var envVarRegistry struct {
mu sync.Mutex
cache map[string]envVarInfo
}
func init() {
ClearEnvCache()
}
// VarName returns the name of the environment variable
// corresponding to a configuration key or command-line flag name.
func VarName(name string) string {
return "COCKROACH_" + strings.ToUpper(strings.Replace(name, "-", "_", -1))
}
// getEnv retrieves an environment variable, keeps track of where
// it was accessed, and checks that each environment variable is accessed
// from at most one place.
// The bookkeeping enables a report of all influential environment
// variables with "cockroach debug env". To keep this report useful,
// all relevant environment variables should be read during start up.
func getEnv(name string) (string, bool) {
_, consumer, _, _ := runtime.Caller(2)
varName := VarName(name)
envVarRegistry.mu.Lock()
defer envVarRegistry.mu.Unlock()
if f, ok := envVarRegistry.cache[varName]; ok {
if f.consumer != consumer {
panic("environment variable " + varName + " already used from " + f.consumer)
}
return f.value, f.present
}
v, found := os.LookupEnv(varName)
envVarRegistry.cache[varName] = envVarInfo{consumer: consumer, present: found, value: v}
return v, found
}
// ClearEnvCache clears saved environment values so that
// a new read access the environment again. (Used for testing)
func ClearEnvCache() {
envVarRegistry.mu.Lock()
defer envVarRegistry.mu.Unlock()
envVarRegistry.cache = make(map[string]envVarInfo)
}
// GetEnvReport dumps all configuration variables that may have been
// used and their value.
func GetEnvReport() string {
envVarRegistry.mu.Lock()
defer envVarRegistry.mu.Unlock()
var b bytes.Buffer
for k, v := range envVarRegistry.cache {
if v.present {
fmt.Fprintf(&b, "%s = %s # %s\n", k, v.value, v.consumer)
} else {
fmt.Fprintf(&b, "# %s is not set (read from %s)\n", k, v.consumer)
}
}
return b.String()
}
// GetShellCommand returns a complete command to run with a prefix of the command line.
func GetShellCommand(cmd string) []string {
if runtime.GOOS == "windows" {
if shell := os.Getenv("COMSPEC"); len(shell) > 0 {
return []string{shell, "/C", cmd}
}
return []string{`C:\Windows\system32\cmd.exe`, "/C", cmd}
}
if shell := os.Getenv("SHELL"); len(shell) > 0 {
return []string{shell, "-c", cmd}
}
return []string{"/bin/sh", "-c", cmd}
}
// EnvOrDefaultString returns the value set by the specified
// environment variable, if any, otherwise the specified default
// value.
func EnvOrDefaultString(name string, value string) string {
if v, present := getEnv(name); present {
return v
}
return value
}
// EnvOrDefaultBool returns the value set by the specified environment
// variable, if any, otherwise the specified default value.
func EnvOrDefaultBool(name string, value bool) bool {
if str, present := getEnv(name); present {
v, err := strconv.ParseBool(str)
if err != nil {
log.Errorf("error parsing %s: %s", VarName(name), err)
return value
}
return v
}
return value
}
// EnvOrDefaultInt returns the value set by the specified environment
// variable, if any, otherwise the specified default value.
func EnvOrDefaultInt(name string, value int) int {
if str, present := getEnv(name); present {
v, err := strconv.ParseInt(str, 0, 0)
if err != nil {
log.Errorf("error parsing %s: %s", VarName(name), err)
return value
}
return int(v)
}
return value
}
// EnvOrDefaultInt64 returns the value set by the specified environment
// variable, if any, otherwise the specified default value.
func EnvOrDefaultInt64(name string, value int64) int64 {
if str, present := getEnv(name); present {
v, err := strconv.ParseInt(str, 0, 64)
if err != nil {
log.Errorf("error parsing %s: %s", VarName(name), err)
return value
}
return v
}
return value
}
// EnvOrDefaultBytes returns the value set by the specified environment
// variable, if any, otherwise the specified default value.
func EnvOrDefaultBytes(name string, value int64) int64 {
if str, present := getEnv(name); present {
v, err := humanizeutil.ParseBytes(str)
if err != nil {
log.Errorf("error parsing %s: %s", VarName(name), err)
return value
}
return v
}
return value
}
// EnvOrDefaultDuration returns the value set by the specified environment
// variable, if any, otherwise the specified default value.
func EnvOrDefaultDuration(name string, value time.Duration) time.Duration {
if str, present := getEnv(name); present {
v, err := time.ParseDuration(str)
if err != nil {
log.Errorf("error parsing %s: %s", VarName(name), err)
return value
}
return v
}
return value
}