Documentation
¶
Index ¶
- Variables
- func Bool(v any) bool
- func Cmdline(args []string, binName ...string) string
- func CurrentShell(onlyName bool, fallbackShell ...string) (binPath string)
- func Environ() map[string]string
- func ExecCmd(binName string, args []string, workDir ...string) (string, error)
- func ExpandHome(pathStr string) string
- func FormatWithArgs(fmtAndArgs []any) string
- func HasShellEnv(shell string) bool
- func IsDuration(s string) bool
- func JoinPaths2(basePath string, elems []string) string
- func JoinPaths3(basePath, secPath string, elems []string) string
- func ParseEnvLines(text string, opt ParseEnvLineOption) (mp map[string]string, err error)
- func ShellExec(cmdLine string, shells ...string) (string, error)
- func ShellQuote(a string) string
- func SplitKvBySep(line, sep string, rmInlineComments bool) (key, val string)
- func SplitLineToKv(line, sep string) (string, string)
- func StrToBool(s string) (bool, error)
- func ToBool(v any) (bool, error)
- func ToDuration(s string) (time.Duration, error)
- func ToStringWith(in any, optFns ...ConvOptionFn) (str string, err error)
- func Workdir() string
- type ConvOption
- type ConvOptionFn
- type ParseEnvLineOption
Constants ¶
This section is empty.
Variables ¶
StrBySprintFn convert any value to string by fmt.Sprint
Functions ¶
func CurrentShell ¶ added in v0.5.3
CurrentShell get current used shell env file.
return like: "/bin/zsh" "/bin/bash". if onlyName=true, will return "zsh", "bash"
func Environ ¶ added in v0.5.11
Environ like os.Environ, but will returns key-value map[string]string data.
func ExecCmd ¶ added in v0.5.3
ExecCmd an command and return output.
Usage:
ExecCmd("ls", []string{"-al"})
func ExpandHome ¶ added in v0.6.8
ExpandHome will parse first `~` as user home dir path.
func FormatWithArgs ¶ added in v0.6.12
FormatWithArgs format message with args
- only one element, format to string
- first is format: fmt.Sprintf(firstElem, fmtAndArgs[1:]...)
- all is args: return fmt.Sprint(fmtAndArgs...)
func HasShellEnv ¶ added in v0.5.3
HasShellEnv has shell env check.
Usage:
HasShellEnv("sh")
HasShellEnv("bash")
func IsDuration ¶ added in v0.6.9
IsDuration check the string is a duration string.
func JoinPaths2 ¶ added in v0.6.18
JoinPaths2 elements, like the filepath.Join()
func JoinPaths3 ¶ added in v0.6.18
JoinPaths3 elements, like the filepath.Join()
func ParseEnvLines ¶ added in v0.7.1
func ParseEnvLines(text string, opt ParseEnvLineOption) (mp map[string]string, err error)
ParseEnvLines parse simple multiline k-v string to a string-map. Can use to parse simple INI or DOTENV file contents.
NOTE:
- It's like INI/ENV format contents.
- Support comments line starts with: "#", ";", "//"
- Support inline comments split with: " #" eg: "name=tom # a comments"
- DON'T support submap parse.
func ShellQuote ¶ added in v0.7.0
ShellQuote quote a string on contains ', ", SPACE. refer strconv.Quote()
func SplitKvBySep ¶ added in v0.7.3
SplitKvBySep parse string line to k-v, support parse comments.
- rmInlineComments: check and remove inline comments by ' #'
func SplitLineToKv ¶ added in v0.7.1
SplitLineToKv parse string line to k-v, not support comments.
Example:
'DEBUG=true' => ['DEBUG', 'true']
NOTE: line must contain '=', allow: 'ENV_KEY='
func ToDuration ¶ added in v0.6.9
ToDuration parses a duration string. such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
Diff of time.ParseDuration:
- support extends unit d, w at the end of string. such as "1d", "2w".
- support extends unit: month, week, day
- support long string unit at the end. such as "1hour", "2hours", "3minutes", "4mins", "5days", "1weeks".
If the string is not a valid duration string, it will return an error.
func ToStringWith ¶ added in v0.6.15
func ToStringWith(in any, optFns ...ConvOptionFn) (str string, err error)
ToStringWith try to convert value to string. can with some option func, more see ConvOption.
Types ¶
type ConvOption ¶ added in v0.6.15
type ConvOption struct {
// if ture: value is nil, will return convert error;
// if false(default): value is nil, will convert to zero value
NilAsFail bool
// HandlePtr auto convert ptr type(int,float,string) value. eg: *int to int
// - if true: will use real type try convert. default is false
// - NOTE: current T type's ptr is default support.
HandlePtr bool
// set custom fallback convert func for not supported type.
UserConvFn comdef.ToStringFunc
}
ConvOption convert options
func NewConvOption ¶ added in v0.6.15
func NewConvOption(optFns ...ConvOptionFn) *ConvOption
NewConvOption create a new ConvOption
func (*ConvOption) WithOption ¶ added in v0.6.15
func (opt *ConvOption) WithOption(optFns ...ConvOptionFn)
WithOption set convert option
type ConvOptionFn ¶ added in v0.6.15
type ConvOptionFn func(opt *ConvOption)
ConvOptionFn convert option func
func WithUserConvFn ¶ added in v0.6.15
func WithUserConvFn(fn comdef.ToStringFunc) ConvOptionFn
WithUserConvFn set ConvOption.UserConvFn option
type ParseEnvLineOption ¶ added in v0.7.1
type ParseEnvLineOption struct {
// NotInlineComments dont parse inline comments.
// - default: false. will parse inline comments
NotInlineComments bool
// SkipOnErrorLine skip error line, continue parse next line
// - False: return error, clear parsed map
SkipOnErrorLine bool
}
ParseEnvLineOption parse env line options