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