github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/configv3/config.go (about)

     1  // Package configv3 package contains everything related to the CF CLI Configuration.
     2  package configv3
     3  
     4  import (
     5  	"path/filepath"
     6  	"strconv"
     7  
     8  	"code.cloudfoundry.org/cli/version"
     9  )
    10  
    11  // Config combines the settings taken from the .cf/config.json, os.ENV, and the
    12  // plugin config.
    13  type Config struct {
    14  	// ConfigFile stores the configuration from the .cf/config
    15  	ConfigFile JSONConfig
    16  
    17  	// ENV stores the configuration from os.ENV
    18  	ENV EnvOverride
    19  
    20  	// Flags stores the configuration from gobal flags
    21  	Flags FlagOverride
    22  
    23  	// detectedSettings are settings detected when the config is loaded.
    24  	detectedSettings detectedSettings
    25  
    26  	pluginsConfig PluginsConfig
    27  }
    28  
    29  // BinaryVersion is the current version of the CF binary.
    30  func (config *Config) BinaryVersion() string {
    31  	return version.VersionString()
    32  }
    33  
    34  // IsTTY returns true based off of:
    35  //   - The $FORCE_TTY is set to true/t/1
    36  //   - Detected from the STDOUT stream
    37  func (config *Config) IsTTY() bool {
    38  	if config.ENV.ForceTTY != "" {
    39  		envVal, err := strconv.ParseBool(config.ENV.ForceTTY)
    40  		if err == nil {
    41  			return envVal
    42  		}
    43  	}
    44  
    45  	return config.detectedSettings.tty
    46  }
    47  
    48  // TerminalWidth returns the width of the terminal from when the config
    49  // was loaded. If the terminal width has changed since the config has loaded,
    50  // it will **not** return the new width.
    51  func (config *Config) TerminalWidth() int {
    52  	return config.detectedSettings.terminalWidth
    53  }
    54  
    55  // Verbose returns true if verbose should be displayed to terminal, in addition
    56  // a slice of absolute paths in which verbose text will appear. This is based
    57  // off of:
    58  //   - The config file's trace value (true/false/file path)
    59  //   - The $CF_TRACE environment variable if set (true/false/file path)
    60  //   - The '-v/--verbose' global flag
    61  //   - Defaults to false
    62  func (config *Config) Verbose() (bool, []string) {
    63  	var (
    64  		verbose     bool
    65  		envOverride bool
    66  		filePath    []string
    67  	)
    68  	if config.ENV.CFTrace != "" {
    69  		envVal, err := strconv.ParseBool(config.ENV.CFTrace)
    70  		verbose = envVal
    71  		if err != nil {
    72  			filePath = []string{config.ENV.CFTrace}
    73  		} else {
    74  			envOverride = true
    75  		}
    76  	}
    77  	if config.ConfigFile.Trace != "" {
    78  		envVal, err := strconv.ParseBool(config.ConfigFile.Trace)
    79  		if !envOverride {
    80  			verbose = envVal || verbose
    81  		}
    82  		if err != nil {
    83  			filePath = append(filePath, config.ConfigFile.Trace)
    84  		}
    85  	}
    86  	verbose = config.Flags.Verbose || verbose
    87  
    88  	for i, path := range filePath {
    89  		if !filepath.IsAbs(path) {
    90  			filePath[i] = filepath.Join(config.detectedSettings.currentDirectory, path)
    91  		}
    92  		resolvedPath, err := filepath.EvalSymlinks(filePath[i])
    93  		if err == nil {
    94  			filePath[i] = resolvedPath
    95  		}
    96  	}
    97  
    98  	return verbose, filePath
    99  }