github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/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  //go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . UserConfig
    12  
    13  type UserConfig interface {
    14  	CurrentUser() (User, error)
    15  	CurrentUserName() (string, error)
    16  }
    17  
    18  // Config combines the settings taken from the .cf/config.json, os.ENV, and the
    19  // plugin config.
    20  type Config struct {
    21  	// ConfigFile stores the configuration from the .cf/config
    22  	ConfigFile JSONConfig
    23  
    24  	// ENV stores the configuration from os.ENV
    25  	ENV EnvOverride
    26  
    27  	// Flags stores the configuration from global flags
    28  	Flags FlagOverride
    29  
    30  	// detectedSettings are settings detected when the config is loaded.
    31  	detectedSettings detectedSettings
    32  
    33  	pluginsConfig PluginsConfig
    34  
    35  	UserConfig
    36  }
    37  
    38  // BinaryVersion is the current version of the CF binary.
    39  func (config *Config) BinaryVersion() string {
    40  	return version.VersionString()
    41  }
    42  
    43  // IsTTY returns true based off of:
    44  //   - The $FORCE_TTY is set to true/t/1
    45  //   - Detected from the STDOUT stream
    46  func (config *Config) IsTTY() bool {
    47  	if config.ENV.ForceTTY != "" {
    48  		envVal, err := strconv.ParseBool(config.ENV.ForceTTY)
    49  		if err == nil {
    50  			return envVal
    51  		}
    52  	}
    53  
    54  	return config.detectedSettings.tty
    55  }
    56  
    57  // TerminalWidth returns the width of the terminal from when the config
    58  // was loaded. If the terminal width has changed since the config has loaded,
    59  // it will **not** return the new width.
    60  func (config *Config) TerminalWidth() int {
    61  	return config.detectedSettings.terminalWidth
    62  }
    63  
    64  // Verbose returns true if verbose should be displayed to terminal, in addition
    65  // a slice of absolute paths in which verbose text will appear. This is based
    66  // off of:
    67  //   - The config file's trace value (true/false/file path)
    68  //   - The $CF_TRACE environment variable if set (true/false/file path)
    69  //   - The '-v/--verbose' global flag
    70  //   - Defaults to false
    71  func (config *Config) Verbose() (bool, []string) {
    72  	var (
    73  		verbose     bool
    74  		envOverride bool
    75  		filePath    []string
    76  	)
    77  	if config.ENV.CFTrace != "" {
    78  		envVal, err := strconv.ParseBool(config.ENV.CFTrace)
    79  		verbose = envVal
    80  		if err != nil {
    81  			filePath = []string{config.ENV.CFTrace}
    82  		} else {
    83  			envOverride = true
    84  		}
    85  	}
    86  	if config.ConfigFile.Trace != "" {
    87  		envVal, err := strconv.ParseBool(config.ConfigFile.Trace)
    88  		if !envOverride {
    89  			verbose = envVal || verbose
    90  		}
    91  		if err != nil {
    92  			filePath = append(filePath, config.ConfigFile.Trace)
    93  		}
    94  	}
    95  	verbose = config.Flags.Verbose || verbose
    96  
    97  	for i, path := range filePath {
    98  		if !filepath.IsAbs(path) {
    99  			filePath[i] = filepath.Join(config.detectedSettings.currentDirectory, path)
   100  		}
   101  		resolvedPath, err := filepath.EvalSymlinks(filePath[i])
   102  		if err == nil {
   103  			filePath[i] = resolvedPath
   104  		}
   105  	}
   106  
   107  	return verbose, filePath
   108  }
   109  
   110  func (config *Config) SetKubernetesAuthInfo(authInfo string) {
   111  	config.ConfigFile.CFOnK8s.AuthInfo = authInfo
   112  }