github.com/arunkumar7540/cli@v6.45.0+incompatible/util/configv3/env.go (about) 1 package configv3 2 3 import ( 4 "strconv" 5 "strings" 6 "time" 7 ) 8 9 // EnvOverride represents all the environment variables read by the CF CLI 10 type EnvOverride struct { 11 BinaryName string 12 CFColor string 13 CFDialTimeout string 14 CFHome string 15 CFLogLevel string 16 CFPassword string 17 CFPluginHome string 18 CFStagingTimeout string 19 CFStartupTimeout string 20 CFTrace string 21 CFUsername string 22 DockerPassword string 23 Experimental string 24 ExperimentalLogin string 25 ForceTTY string 26 HTTPSProxy string 27 Lang string 28 LCAll string 29 } 30 31 // BinaryName returns the running name of the CF CLI 32 func (config *Config) BinaryName() string { 33 return config.ENV.BinaryName 34 } 35 36 // CFPassword returns the value of the "CF_PASSWORD" environment variable. 37 func (config *Config) CFPassword() string { 38 return config.ENV.CFPassword 39 } 40 41 // CFUsername returns the value of the "CF_USERNAME" environment variable. 42 func (config *Config) CFUsername() string { 43 return config.ENV.CFUsername 44 } 45 46 // DialTimeout returns the timeout to use when dialing. This is based off of: 47 // 1. The $CF_DIAL_TIMEOUT environment variable if set 48 // 2. Defaults to 5 seconds 49 func (config *Config) DialTimeout() time.Duration { 50 if config.ENV.CFDialTimeout != "" { 51 envVal, err := strconv.ParseInt(config.ENV.CFDialTimeout, 10, 64) 52 if err == nil { 53 return time.Duration(envVal) * time.Second 54 } 55 } 56 57 return DefaultDialTimeout 58 } 59 60 // DockerPassword returns the docker password from the environment. 61 func (config *Config) DockerPassword() string { 62 return config.ENV.DockerPassword 63 } 64 65 // Experimental returns whether or not to run experimental CLI commands. This 66 // is based off of: 67 // 1. The $CF_CLI_EXPERIMENTAL environment variable if set 68 // 2. Defaults to false 69 func (config *Config) Experimental() bool { 70 if config.ENV.Experimental != "" { 71 envVal, err := strconv.ParseBool(config.ENV.Experimental) 72 if err == nil { 73 return envVal 74 } 75 } 76 77 return false 78 } 79 80 // ExperimentalLogin is a temporary function during the rewrite of `cf login` that returns whether or not to run the rewritten login. This 81 // is based off of: 82 // 1. The $CF_EXPERIMENTAL_LOGIN environment variable if set 83 // 2. Defaults to false 84 func (config *Config) ExperimentalLogin() bool { 85 if config.ENV.ExperimentalLogin != "" { 86 envVal, err := strconv.ParseBool(config.ENV.ExperimentalLogin) 87 if err == nil { 88 return envVal 89 } 90 } 91 92 return false 93 } 94 95 // HTTPSProxy returns the proxy url that the CLI should use. The url is based 96 // off of: 97 // 1. The $https_proxy environment variable if set 98 // 2. Defaults to the empty string 99 func (config *Config) HTTPSProxy() string { 100 return config.ENV.HTTPSProxy 101 } 102 103 // LogLevel returns the global log level. The levels follow Logrus's log level 104 // scheme. This value is based off of: 105 // - The $CF_LOG_LEVEL and an int/warn/info/etc... 106 // - Defaults to PANIC/0 (ie no logging) 107 func (config *Config) LogLevel() int { 108 if config.ENV.CFLogLevel != "" { 109 envVal, err := strconv.ParseInt(config.ENV.CFLogLevel, 10, 32) 110 if err == nil { 111 return int(envVal) 112 } 113 114 switch strings.ToLower(config.ENV.CFLogLevel) { 115 case "fatal": 116 return 1 117 case "error": 118 return 2 119 case "warn": 120 return 3 121 case "info": 122 return 4 123 case "debug": 124 return 5 125 } 126 } 127 128 return 0 129 } 130 131 // StagingTimeout returns the max time an application staging should take. The 132 // time is based off of: 133 // 1. The $CF_STAGING_TIMEOUT environment variable if set 134 // 2. Defaults to the DefaultStagingTimeout 135 func (config *Config) StagingTimeout() time.Duration { 136 if config.ENV.CFStagingTimeout != "" { 137 timeoutInMin, err := strconv.ParseFloat(config.ENV.CFStagingTimeout, 64) 138 timeoutInSec := int64(timeoutInMin * 60) 139 if err == nil { 140 return time.Duration(timeoutInSec) * time.Second 141 } 142 } 143 144 return DefaultStagingTimeout 145 } 146 147 // StartupTimeout returns the max time an application should take to start. The 148 // time is based off of: 149 // 1. The $CF_STARTUP_TIMEOUT environment variable if set 150 // 2. Defaults to the DefaultStartupTimeout 151 func (config *Config) StartupTimeout() time.Duration { 152 if config.ENV.CFStartupTimeout != "" { 153 timeoutInMin, err := strconv.ParseFloat(config.ENV.CFStartupTimeout, 64) 154 timeoutInSec := int64(timeoutInMin * 60) 155 if err == nil { 156 return time.Duration(timeoutInSec) * time.Second 157 } 158 } 159 160 return DefaultStartupTimeout 161 }