# Helpers
Cmdr 也包含一些辅助函数(Helpers functions)。
# 对 Option Store 的操作
# func AsJSON (opens new window)
func AsJSON() (b []byte)
AsJSON returns a json string bytes about all options
# func AsJSONExt (opens new window)
func AsJSONExt() (b []byte, err error)
AsJSONExt returns a json string bytes about all options
# func AsToml (opens new window)
func AsToml() (b []byte)
AsToml returns a toml string bytes about all options
# func AsTomlExt (opens new window)
func AsTomlExt() (b []byte, err error)
AsTomlExt returns a toml string bytes about all options
# func AsYaml (opens new window)
func AsYaml() (b []byte)
AsYaml returns a yaml string bytes about all options
# func AsYamlExt (opens new window)
func AsYamlExt() (b []byte, err error)
AsYamlExt returns a yaml string bytes about all options
# func DeleteKey (opens new window)
func DeleteKey(key string)
DeleteKey deletes a key from cmdr options store
# func GetHierarchyList (opens new window)
func GetHierarchyList() map[string]interface{}
GetHierarchyList returns the hierarchy data
# func GetSectionFrom (opens new window)
func GetSectionFrom(sectionKeyPath string, holder interface{}) (err error)
GetSectionFrom returns error while cannot yaml Marshal and Unmarshal cmdr.GetSectionFrom(sectionKeyPath, &holder)
could load all sub-tree nodes from sectionKeyPath and transform them into holder structure, such as:
type ServerConfig struct {
Port int
HttpMode int
EnableTls bool
}
var serverConfig = new(ServerConfig)
cmdr.GetSectionFrom("server", &serverConfig)
assert serverConfig.Port == 7100
2
3
4
5
6
7
8
# func HasKey (opens new window)
func HasKey(key string) (ok bool)
HasKey detects whether a key exists in cmdr options store or not
# func MergeWith (opens new window)
func MergeWith(m map[string]interface{}) (err error)
MergeWith will merge a map recursive. You could merge a yaml/json/toml options into cmdr Hierarchy Options.
# func ResetOptions (opens new window)
func ResetOptions()
ResetOptions to reset the exists Options
, so that you could follow a LoadConfigFile()
with it.
# func SaveAsJSON (opens new window)
func SaveAsJSON(filename string) (err error)
SaveAsJSON to Save all config entries as a json file
# func SaveAsToml (opens new window)
func SaveAsToml(filename string) (err error)
SaveAsToml to Save all config entries as a toml file
# func SaveAsYaml (opens new window)
func SaveAsYaml(filename string) (err error)
SaveAsYaml to Save all config entries as a yaml file
# func SaveObjAsToml (opens new window)
func SaveObjAsToml(obj interface{}, filename string) (err error)
SaveObjAsToml to Save an object as a toml file
# func WrapWithRxxtPrefix (opens new window)
func WrapWithRxxtPrefix(key string) string
WrapWithRxxtPrefix wrap an key with [RxxtPrefix], for [GetXxx(key)] and [GetXxxP(prefix,key)]
# 文件、文件夹操作
# func EnsureDir (opens new window)
func EnsureDir(dir string) (err error)
EnsureDir checks and creates the directory.
# func EnsureDirEnh (opens new window)
func EnsureDirEnh(dir string) (err error)
EnsureDirEnh checks and creates the directory, via sudo if necessary.
# func FileExists (opens new window)
func FileExists(name string) bool
FileExists returns the existence of an directory or file
# func GetCurrentDir (opens new window)
func GetCurrentDir() string
GetCurrentDir returns the current workingFlag directory it should be equal with os.Getenv("PWD")
# func GetExecutableDir (opens new window)
func GetExecutableDir() string
GetExecutableDir returns the executable file directory
# func GetExecutablePath (opens new window)
func GetExecutablePath() string
GetExecutablePath returns the executable file path
# func IsDirectory (opens new window)
func IsDirectory(path string) (bool, error)
IsDirectory tests whether path
is a directory or not
# func IsRegularFile (opens new window)
func IsRegularFile(path string) (bool, error)
IsRegularFile tests whether path
is a normal regular file or not
# func NormalizeDir (opens new window)
func NormalizeDir(s string) string
NormalizeDir make dir name normalized
# func RemoveDirRecursive (opens new window)
func RemoveDirRecursive(dir string) (err error)
RemoveDirRecursive removes a directory and any children it contains.
# Debugger/Debugging 操作
# func GetDebugMode (opens new window)
func GetDebugMode() bool
GetDebugMode returns the flag value of --debug
/-D
NOTE
log.GetDebugMode()/SetDebugMode() have higher universality
# func GetNoColorMode (opens new window)
func GetNoColorMode() bool
GetNoColorMode return the flag value of --no-color
# func GetQuietMode (opens new window)
func GetQuietMode() bool
GetQuietMode returns the flag value of --quiet
/-q
# func GetStrictMode (opens new window)
func GetStrictMode() bool
GetStrictMode enables error when opt value missed. such as: xxx a b --prefix'' => error: prefix opt has no value specified. xxx a b --prefix'/' => ok.
ENV: use CMDR_APP_STRICT_MODE=true
to enable strict-mode. NOTE: CMDR_APP_
prefix could be set by user (via: EnvPrefix
&& RxxtPrefix
).
the flag value of --strict-mode
.
# func GetTraceMode (opens new window)
func GetTraceMode() bool
GetTraceMode returns the flag value of --trace
/-tr
NOTE
log.GetTraceMode()/SetTraceMode() have higher universality
# func GetVerboseMode (opens new window)
func GetVerboseMode() bool
GetVerboseMode returns the flag value of --verbose
/-v
# func InDebugging (opens new window)
func InDebugging() bool
InDebugging return the status if cmdr was built with debug mode / or the app running under a debugger attached.
To enable the debugger attached mode for cmdr, run go build
with -tags=delve
options. eg:
go run -tags=delve ./cli
go build -tags=delve -o my-app ./cli
2
For Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'
-tags=delve
InDebugging() is a synonym to IsDebuggerAttached().
NOTE that isdelve
algor is from https://stackoverflow.com/questions/47879070/how-can-i-see-if-the-goland-debugger-is-running-in-the-program
noinspection GoBoolExpressions
# func InTesting (opens new window)
func InTesting() bool
InTesting detects whether is running under go test mode
# func IsDebuggerAttached (opens new window)
func IsDebuggerAttached() bool
IsDebuggerAttached return the status if cmdr was built with debug mode / or the app running under a debugger attached.
To enable the debugger attached mode for cmdr, run go build
with -tags=delve
options. eg:
go run -tags=delve ./cli
go build -tags=delve -o my-app ./cli
2
For Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'
-tags=delve
IsDebuggerAttached() is a synonym to InDebugging().
NOTE that isdelve
algor is from https://stackoverflow.com/questions/47879070/how-can-i-see-if-the-goland-debugger-is-running-in-the-program
noinspection GoBoolExpressions
# 配置文件操作 (Config file Operations)
# func GetPredefinedLocations (opens new window)
func GetPredefinedLocations() []string
GetPredefinedLocations return the searching locations for loading config files.
# func GetUsedConfigFile (opens new window)
func GetUsedConfigFile() string
GetUsedConfigFile returns the main config filename (generally it's <appname>.yml
)
# func GetUsedConfigSubDir (opens new window)
func GetUsedConfigSubDir() string
GetUsedConfigSubDir returns the sub-directory conf.d
of config files. Note that it be always normalized now. Sometimes it might be empty string ("") if conf.d
have not been found.
# func GetUsingConfigFiles (opens new window)
func GetUsingConfigFiles() []string
GetUsingConfigFiles returns all loaded config files, includes the main config file and children in sub-directory conf.d
.
# func LoadConfigFile (opens new window)
func LoadConfigFile(file string) (err error)
LoadConfigFile loads a yaml config file and merge the settings into rxxtOptions
and load files in the conf.d
child directory too.
# More
tool
子包中也包含一些工具函数。
...