Using is Detectors
is.inDeveloping()?
介绍
hedzr.is
是一个以环境检测函数为主的基础支持库。
is
provides numerous detectors for checking the states of environment (build, executive, ...).
在 is
中提供了大量 Golang 运行环境检测工具,有的独立工作,有的需要配合 go build tags 协同工作。
总的来说,它们可以帮助你更好地确定当前运行环境,让你的代码能够据此进行适配。
Features
is.State(which) bool
: the universal detector entry - viaRegisterStateGetter(state string, getter func() bool)
to add your own ones. Since v0.5.11is.Env()
holds a global struct for CLI app basic states, such as: verbose/quiet/debug/trace....DebugMode
/DebugLevel
,TraceMode
/TraceLevel
,ColorMode
, ...
is.InDebugging() bool
,is.InTesting() bool
, andis.InTracing() bool
, ....is.DebugBuild() bool
.is.K8sBuild() bool
,is.DockerBuild() bool
, ....is.ColoredTty() bool
, ....is.Color()
to get an indexer for the functions in our term/color subpackage, ...- Terminal Colorizer, Detector, unescape tools.
- stringtool:
RandomStringPure
, case-converters ... - basics: closable, closer, signals.
- easier
Press any key to exit...
prompt:is.Signals().Catch()
- easier
- exec: Run, RunWithOutput, Sudo, ...
- go1.23.7+ required since v0.7.0
go 1.22.7+ required
To using environment detecting utilities better and smoother, some terminal (and stringtool, basics) tools are bundled together.
Since v0.6.0, is.InDebugging()
checks if the running process' parent is dlv
.
The old DebugMode
and DebugBuild
are still work:
InDebugging
: checks this process is being debugged bydlv
.DebugBuild
:-tags=delve
is set at building.DebugMode
:--debug
is specified at command line.
分类
基础环境
-
InDebugging
: checks this process is being debugged bydlv
. -
DebugBuild
:-tags=delve
is set at building. -
DebugMode
:--debug
is specified at command line. -
is.InTracing
/ InTestingT -
is.InTesting
/ InTestingT -
is.InDevelopingTime
-
is.InVscodeTerminal
-
is.InK8s
-
is.InIstio
-
is.InDocker
/ InDockerEnvSimple -
Build (need
-tags=xx
)is.K8sBuild
: need-tags=k8s
presentis.IstioBuild
: need-tags=istio
presentis.DockerBuild
: need-tags=docker
presentis.VerboseBuild
: need-tags=verbose
presentis.DebugBuild
: need-tags=delve
presentbuildtags.IsBuildTagExists
States / Env
- VerboseModeEnabled
- GetVerboseLevel / SetVerboseMode / SetVerboseLevel
- QuietModeEnabled
- GetQuietLevel / SetQuietMode / SetQuietLevel
- NoColorMode
- GetNoColorLevel / SetNoColorMode / SetNoColorLevel
- DebugMode
- GetDebugLevel / SetDebugMode / SetDebugLevel
- Tracing
- TraceMode
- GetTraceLevel / SetTraceMode / SetTraceLevel
Terminal / Tty
- is.Terminal(file)
- is.TerminalFd(fd)
- is.Tty(wr)
- is.ColoredTty(wr)
- is.AnsiEscaped(s) (
IsTtyEscaped(s)) - StripEscapes(s)
- ReadPassword
- GetTtySize
- is.GetTtySizeByName(filename) (cols,rows,err)
- is.GetTtySizeByFile(file) (cols,rows,err)
- is.GetTtySizeByFd(fd) (cols,rows,err)
- StartupByDoubleClick() bool
Basics
- closers
Peripheral
,Closable
, Closer- RegisterClosable
- RegisterClosers
- RegisterCloseFns
is.Signals().Catcher()
- is.FileExists(filepath)
- is.ToBool, StringToBool
utilities
dir
subpackageexec
subpackageterm/color
subpackage- escaping tools: GetCPT()/GetCPTC()/GetCPTNC()
- Highlight, Dimf, Text, Dim, ToDim, ToHighlight, ToColor, ...
Usages
Result is similar with:
NOTE that is.Signals().Catch()
will produce a prompt and enter a infinite loop to wait for user's keystroke pressed.
Build Tags
Some functions want special buildtags presented. These are including:
verbose
: See VerboseBuild, ...delve
: See DebugBuild, ...k8s
: See K8sBuildistio
: See IstioBuilddocker
: See DockerBuild- ...
buildtags.IsBuildTagExists(tag) bool
Colorizes
The test codes:
Result:
And more:
与 cmdr
相集成
Closers
is
提供一个子包 closers
来抽象你的基础设施。
一个基础设施意味着一个需要在应用程序结束前实施其关闭操作的对象,可以是外部资源如数据库连接等,也可以是一个文件句柄。
在你的 app 中,可以使用 RegisterPeripheral
注册一个基础设施,并提供 defer 调用以便关闭所有这些对象。
with cmdr
cmdr
在它的工作流程中会自动执行相似的调用以便关闭已经注册的基础设施,因此 is
+ cmdr
可以更无缝地协同工作。
具体实施可以参考:
Signals
Signals()
could catch OS signals and entering a infinite loop.
is.Signals()
能够拦截 POSIX 信号并回调你的响应函数。
For example, a tcp server could be:
:end:
How is this guide?
Last updated on