-
Notifications
You must be signed in to change notification settings - Fork 367
/
env.go
97 lines (90 loc) · 2.03 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
package common
import (
"os"
"reflect"
"strconv"
"strings"
"sync"
"github.com/mitchellh/go-homedir"
)
var (
envMutex sync.Mutex
onceClient sync.Once
commonClient *DatabricksClient
)
// NewClientFromEnvironment makes very good client for testing purposes
func NewClientFromEnvironment() *DatabricksClient {
client := DatabricksClient{}
for _, attr := range ClientAttributes() {
found := false
var value any
for _, envName := range attr.EnvVars {
v := os.Getenv(envName)
if v == "" {
continue
}
switch attr.Kind {
case reflect.String:
value = v
found = true
case reflect.Bool:
if vv, err := strconv.ParseBool(v); err == nil {
value = vv
found = true
}
case reflect.Int:
if vv, err := strconv.Atoi(v); err == nil {
value = vv
found = true
}
default:
continue
}
}
if found {
attr.Set(&client, value)
}
}
err := client.Configure()
if err != nil {
panic(err)
}
return &client
}
// ResetCommonEnvironmentClient resets test dummy
func ResetCommonEnvironmentClient() {
commonClient = nil
onceClient = sync.Once{}
}
// CommonEnvironmentClient configured once per run of application
func CommonEnvironmentClient() *DatabricksClient {
if commonClient != nil {
return commonClient
}
onceClient.Do(func() {
commonClient = NewClientFromEnvironment()
})
return commonClient
}
// CleanupEnvironment backs up environment - use as `defer CleanupEnvironment()()`
// clears it and restores it in the end. It's meant strictly for "unit" tests
// as last resort, because it slows down parallel execution with mutex.
func CleanupEnvironment() func() {
// make a backed-up pristine environment
envMutex.Lock()
prevEnv := os.Environ()
oldPath := os.Getenv("PATH")
pwd := os.Getenv("PWD")
os.Clearenv()
os.Setenv("PATH", oldPath)
os.Setenv("HOME", pwd)
homedir.DisableCache = true
// and return restore function
return func() {
for _, kv := range prevEnv {
kvs := strings.SplitN(kv, "=", 2)
os.Setenv(kvs[0], kvs[1])
}
envMutex.Unlock()
}
}