hzDocs

工具函数 (Helpers)

Cmdr 也包含一些辅助函数(Helpers functions)。

对 Option Store 的操作

func AsJSON

func AsJSON() (b []byte)

AsJSON returns a json string bytes about all options

func AsJSONExt

func AsJSONExt() (b []byte, err error)

AsJSONExt returns a json string bytes about all options

func AsToml

func AsToml() (b []byte)

AsToml returns a toml string bytes about all options

func AsTomlExt

func AsTomlExt() (b []byte, err error)

AsTomlExt returns a toml string bytes about all options

func AsYaml

func AsYaml() (b []byte)

AsYaml returns a yaml string bytes about all options

func AsYamlExt

func AsYamlExt() (b []byte, err error)

AsYamlExt returns a yaml string bytes about all options

func DeleteKey

func DeleteKey(key string)

DeleteKey deletes a key from cmdr options store

func GetHierarchyList

func GetHierarchyList() map[string]interface{}

GetHierarchyList returns the hierarchy data

func GetSectionFrom

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

func HasKey

func HasKey(key string) (ok bool)

HasKey detects whether a key exists in cmdr options store or not

func MergeWith

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

func ResetOptions()

ResetOptions to reset the exists Options, so that you could follow a LoadConfigFile() with it.

func SaveAsJSON

func SaveAsJSON(filename string) (err error)

SaveAsJSON to Save all config entries as a json file

func SaveAsToml

func SaveAsToml(filename string) (err error)

SaveAsToml to Save all config entries as a toml file

func SaveAsYaml

func SaveAsYaml(filename string) (err error)

SaveAsYaml to Save all config entries as a yaml file

func SaveObjAsToml

func SaveObjAsToml(obj interface{}, filename string) (err error)

SaveObjAsToml to Save an object as a toml file

func WrapWithRxxtPrefix

func WrapWithRxxtPrefix(key string) string

WrapWithRxxtPrefix wrap an key with [RxxtPrefix], for [GetXxx(key)] and [GetXxxP(prefix,key)]

文件、文件夹操作

func EnsureDir

func EnsureDir(dir string) (err error)

EnsureDir checks and creates the directory.

func EnsureDirEnh

func EnsureDirEnh(dir string) (err error)

EnsureDirEnh checks and creates the directory, via sudo if necessary.

func FileExists

func FileExists(name string) bool

FileExists returns the existence of an directory or file

func GetCurrentDir

func GetCurrentDir() string

GetCurrentDir returns the current workingFlag directory it should be equal with os.Getenv("PWD")

func GetExecutableDir

func GetExecutableDir() string

GetExecutableDir returns the executable file directory

func GetExecutablePath

func GetExecutablePath() string

GetExecutablePath returns the executable file path

func IsDirectory

func IsDirectory(path string) (bool, error)

IsDirectory tests whether path is a directory or not

func IsRegularFile

func IsRegularFile(path string) (bool, error)

IsRegularFile tests whether path is a normal regular file or not

func NormalizeDir

func NormalizeDir(s string) string

NormalizeDir make dir name normalized

func RemoveDirRecursive

func RemoveDirRecursive(dir string) (err error)

RemoveDirRecursive removes a directory and any children it contains.

Debugger/Debugging 操作

func GetDebugMode

func GetDebugMode() bool

GetDebugMode returns the flag value of --debug/-D

NOTE

log.GetDebugMode()/SetDebugMode() have higher universality

func GetNoColorMode

func GetNoColorMode() bool

GetNoColorMode return the flag value of --no-color

func GetQuietMode

func GetQuietMode() bool

GetQuietMode returns the flag value of --quiet/-q

func GetStrictMode

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

func GetTraceMode() bool

GetTraceMode returns the flag value of --trace/-tr

NOTE

log.GetTraceMode()/SetTraceMode() have higher universality

func GetVerboseMode

func GetVerboseMode() bool

GetVerboseMode returns the flag value of --verbose/-v

func InDebugging

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

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 mentioned here.

noinspection GoBoolExpressions

func InTesting

func InTesting() bool

InTesting detects whether is running under go test mode

func IsDebuggerAttached

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

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 mentioned here.

noinspection GoBoolExpressions

配置文件操作 (Config file Operations)

func GetPredefinedLocations

func GetPredefinedLocations() []string

GetPredefinedLocations return the searching locations for loading config files.

func GetUsedConfigFile

func GetUsedConfigFile() string

GetUsedConfigFile returns the main config filename (generally it's <appname>.yml)

func GetUsedConfigSubDir

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

func GetUsingConfigFiles() []string

GetUsingConfigFiles returns all loaded config files, includes the main config file and children in sub-directory conf.d.

func LoadConfigFile

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 子包中也包含一些工具函数。

🔚

How is this guide?

Edit on GitHub

Last updated on