github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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 // FlagOverride represents all the global flags passed to the CF CLI 30 type FlagOverride struct { 31 Verbose bool 32 } 33 34 // detectedSettings are automatically detected settings determined by the CLI. 35 type detectedSettings struct { 36 currentDirectory string 37 terminalWidth int 38 tty bool 39 } 40 41 // Verbose returns true if verbose should be displayed to terminal, in addition 42 // a slice of full paths in which verbose text will appear. This is based off 43 // of: 44 // - The config file's trace value (true/false/file path) 45 // - The $CF_TRACE enviroment variable if set (true/false/file path) 46 // - The '-v/--verbose' global flag 47 // - Defaults to false 48 func (config *Config) Verbose() (bool, []string) { 49 var ( 50 verbose bool 51 envOverride bool 52 filePath []string 53 ) 54 if config.ENV.CFTrace != "" { 55 envVal, err := strconv.ParseBool(config.ENV.CFTrace) 56 verbose = envVal 57 if err != nil { 58 filePath = []string{config.ENV.CFTrace} 59 } else { 60 envOverride = true 61 } 62 } 63 if config.ConfigFile.Trace != "" { 64 envVal, err := strconv.ParseBool(config.ConfigFile.Trace) 65 if !envOverride { 66 verbose = envVal || verbose 67 } 68 if err != nil { 69 filePath = append(filePath, config.ConfigFile.Trace) 70 } 71 } 72 verbose = config.Flags.Verbose || verbose 73 74 for i, path := range filePath { 75 if !filepath.IsAbs(path) { 76 filePath[i] = filepath.Join(config.detectedSettings.currentDirectory, path) 77 } 78 } 79 80 return verbose, filePath 81 } 82 83 // IsTTY returns true based off of: 84 // - The $FORCE_TTY is set to true/t/1 85 // - Detected from the STDOUT stream 86 func (config *Config) IsTTY() bool { 87 if config.ENV.ForceTTY != "" { 88 envVal, err := strconv.ParseBool(config.ENV.ForceTTY) 89 if err == nil { 90 return envVal 91 } 92 } 93 94 return config.detectedSettings.tty 95 } 96 97 // TerminalWidth returns the width of the terminal from when the config 98 // was loaded. If the terminal width has changed since the config has loaded, 99 // it will **not** return the new width. 100 func (config *Config) TerminalWidth() int { 101 return config.detectedSettings.terminalWidth 102 } 103 104 func (config *Config) BinaryVersion() string { 105 return version.VersionString() 106 }