github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/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  	ForceTTY         string
    25  	HTTPSProxy       string
    26  	Lang             string
    27  	LCAll            string
    28  }
    29  
    30  // BinaryName returns the running name of the CF CLI
    31  func (config *Config) BinaryName() string {
    32  	return config.ENV.BinaryName
    33  }
    34  
    35  // CFPassword returns the value of the "CF_PASSWORD" environment variable.
    36  func (config *Config) CFPassword() string {
    37  	return config.ENV.CFPassword
    38  }
    39  
    40  // CFUsername returns the value of the "CF_USERNAME" environment variable.
    41  func (config *Config) CFUsername() string {
    42  	return config.ENV.CFUsername
    43  }
    44  
    45  // DialTimeout returns the timeout to use when dialing. This is based off of:
    46  //   1. The $CF_DIAL_TIMEOUT environment variable if set
    47  //   2. Falling back to the default
    48  func (config *Config) DialTimeout() time.Duration {
    49  	if config.ENV.CFDialTimeout != "" {
    50  		envVal, err := strconv.ParseInt(config.ENV.CFDialTimeout, 10, 64)
    51  		if err == nil {
    52  			return time.Duration(envVal) * time.Second
    53  		}
    54  	}
    55  
    56  	return DefaultDialTimeout
    57  }
    58  
    59  // DockerPassword returns the docker password from the environment.
    60  func (config *Config) DockerPassword() string {
    61  	return config.ENV.DockerPassword
    62  }
    63  
    64  // Experimental returns whether or not to run experimental CLI commands. This
    65  // is based on the following:
    66  //   1. The $CF_CLI_EXPERIMENTAL environment variable if set
    67  //   2. Defaults to false
    68  func (config *Config) Experimental() bool {
    69  	if config.ENV.Experimental != "" {
    70  		envVal, err := strconv.ParseBool(config.ENV.Experimental)
    71  		if err == nil {
    72  			return envVal
    73  		}
    74  	}
    75  
    76  	return false
    77  }
    78  
    79  // HTTPSProxy returns the proxy url that the CLI should use. The url is based
    80  // off of:
    81  //   1. The $https_proxy environment variable if set
    82  //   2. Defaults to the empty string
    83  func (config *Config) HTTPSProxy() string {
    84  	return config.ENV.HTTPSProxy
    85  }
    86  
    87  // LogLevel returns the global log level. The levels follow Logrus's log level
    88  // scheme. This value is based off of:
    89  //   - The $CF_LOG_LEVEL and an int/warn/info/etc...
    90  //   - Defaults to PANIC/0 (ie no logging)
    91  func (config *Config) LogLevel() int {
    92  	if config.ENV.CFLogLevel != "" {
    93  		envVal, err := strconv.ParseInt(config.ENV.CFLogLevel, 10, 32)
    94  		if err == nil {
    95  			return int(envVal)
    96  		}
    97  
    98  		switch strings.ToLower(config.ENV.CFLogLevel) {
    99  		case "fatal":
   100  			return 1
   101  		case "error":
   102  			return 2
   103  		case "warn":
   104  			return 3
   105  		case "info":
   106  			return 4
   107  		case "debug":
   108  			return 5
   109  		}
   110  	}
   111  
   112  	return 0
   113  }
   114  
   115  // StagingTimeout returns the max time an application staging should take. The
   116  // time is based off of:
   117  //   1. The $CF_STAGING_TIMEOUT environment variable if set
   118  //   2. Defaults to the DefaultStagingTimeout
   119  func (config *Config) StagingTimeout() time.Duration {
   120  	if config.ENV.CFStagingTimeout != "" {
   121  		timeoutInMin, err := strconv.ParseFloat(config.ENV.CFStagingTimeout, 64)
   122  		timeoutInSec := int64(timeoutInMin * 60)
   123  		if err == nil {
   124  			return time.Duration(timeoutInSec) * time.Second
   125  		}
   126  	}
   127  
   128  	return DefaultStagingTimeout
   129  }
   130  
   131  // StartupTimeout returns the max time an application should take to start. The
   132  // time is based off of:
   133  //   1. The $CF_STARTUP_TIMEOUT environment variable if set
   134  //   2. Defaults to the DefaultStartupTimeout
   135  func (config *Config) StartupTimeout() time.Duration {
   136  	if config.ENV.CFStartupTimeout != "" {
   137  		timeoutInMin, err := strconv.ParseFloat(config.ENV.CFStartupTimeout, 64)
   138  		timeoutInSec := int64(timeoutInMin * 60)
   139  		if err == nil {
   140  			return time.Duration(timeoutInSec) * time.Second
   141  		}
   142  	}
   143  
   144  	return DefaultStartupTimeout
   145  }