/
env.go
67 lines (60 loc) · 2.62 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
package bluemix
import (
"os"
)
var (
// EnvTrace is the environment variable `IBMCLOUD_TRACE` and `BLUEMIX_TRACE` (deprecated)
EnvTrace = newEnv("IBMCLOUD_TRACE", "BLUEMIX_TRACE")
// EnvColor is the environment variable `IBMCLOUD_COLOR` and `BLUEMIX_COLOR` (deprecated)
EnvColor = newEnv("IBMCLOUD_COLOR", "BLUEMIX_COLOR")
// EnvVersionCheck is the environment variable `IBMCLOUD_VERSION_CHECK` and `BLUEMIX_VERSION_CHECK` (deprecated)
EnvVersionCheck = newEnv("IBMCLOUD_VERSION_CHECK", "BLUEMIX_VERSION_CHECK")
// EnvAnalytics is the environment variable `IBMCLOUD_ANALYTICS` and `BLUEMIX_ANALYTICS` (deprecated)
EnvAnalytics = newEnv("IBMCLOUD_ANALYTICS", "BLUEMIX_ANALYTICS")
// EnvHTTPTimeout is the environment variable `IBMCLOUD_HTTP_TIMEOUT` and `BLUEMIX_HTTP_TIMEOUT` (deprecated)
EnvHTTPTimeout = newEnv("IBMCLOUD_HTTP_TIMEOUT", "BLUEMIX_HTTP_TIMEOUT")
// EnvAPIKey is the environment variable `IBMCLOUD_API_KEY` and `BLUEMIX_API_KEY` (deprecated)
EnvAPIKey = newEnv("IBMCLOUD_API_KEY", "BLUEMIX_API_KEY")
// EnvCRToken is the environment variable `IBMCLOUD_CR_TOKEN`
EnvCRTokenKey = newEnv("IBMCLOUD_CR_TOKEN")
// EnvCRProfile is the environment variable `IBMCLOUD_CR_PROFILE`
EnvCRProfile = newEnv("IBMCLOUD_CR_PROFILE")
// EnvCRVpcUrl is the environment variable `IBMCLOUD_CR_VPC_URL`
EnvCRVpcUrl = newEnv("IBMCLOUD_CR_VPC_URL")
// EnvConfigHome is the environment variable `IBMCLOUD_HOME` and `BLUEMIX_HOME` (deprecated)
EnvConfigHome = newEnv("IBMCLOUD_HOME", "BLUEMIX_HOME")
// EnvConfigDir is the environment variable `IBMCLOUD_CONFIG_HOME`
EnvConfigDir = newEnv("IBMCLOUD_CONFIG_HOME")
// EnvQuiet is the environment variable `IBMCLOUD_QUIET`
EnvQuiet = newEnv("IBMCLOUD_QUIET")
// for internal use
EnvCLIName = newEnv("IBMCLOUD_CLI", "BLUEMIX_CLI")
EnvPluginNamespace = newEnv("IBMCLOUD_PLUGIN_NAMESPACE", "BLUEMIX_PLUGIN_NAMESPACE")
)
// Env is an environment variable supported by IBM Cloud CLI for specific purpose
// An Env could be bound to multiple environment variables due to historical reasons (i.e. renaming)
// Make sure you define the latest environment variable first
type Env struct {
names []string
}
// Get will return the value of the environment variable, the first found non-empty value will be returned
func (e Env) Get() string {
for _, n := range e.names {
if v := os.Getenv(n); v != "" {
return v
}
}
return ""
}
// Set will set the value to **ALL** belonging environment variables
func (e Env) Set(val string) error {
for _, n := range e.names {
if err := os.Setenv(n, val); err != nil {
return err
}
}
return nil
}
func newEnv(names ...string) Env {
return Env{names: names}
}