comfunc

package
v0.6.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2023 License: MIT Imports: 10 Imported by: 0

README

common func for internal use

  • don't depend on other external packages

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool added in v0.5.12

func Bool(v any) bool

Bool try to convert type to bool

func Cmdline added in v0.5.11

func Cmdline(args []string, binName ...string) string

Cmdline build

func CurrentShell added in v0.5.3

func CurrentShell(onlyName bool) (binPath string)

CurrentShell get current used shell env file.

eg "/bin/zsh" "/bin/bash". if onlyName=true, will return "zsh", "bash"

func Environ added in v0.5.11

func Environ() map[string]string

Environ like os.Environ, but will returns key-value map[string]string data.

func ExecCmd added in v0.5.3

func ExecCmd(binName string, args []string, workDir ...string) (string, error)

ExecCmd an command and return output.

Usage:

ExecCmd("ls", []string{"-al"})

func ExpandHome added in v0.6.8

func ExpandHome(pathStr string) string

ExpandHome will parse first `~` as user home dir path.

func FormatWithArgs added in v0.6.12

func FormatWithArgs(fmtAndArgs []any) string

FormatWithArgs format message with args

func HasShellEnv added in v0.5.3

func HasShellEnv(shell string) bool

HasShellEnv has shell env check.

Usage:

HasShellEnv("sh")
HasShellEnv("bash")

func IsDuration added in v0.6.9

func IsDuration(s string) bool

IsDuration check the string is a duration string.

func ShellExec added in v0.5.3

func ShellExec(cmdLine string, shells ...string) (string, error)

ShellExec exec command by shell cmdLine e.g. "ls -al"

func StrToBool added in v0.5.11

func StrToBool(s string) (bool, error)

StrToBool parse string to bool. like strconv.ParseBool()

func ToBool added in v0.5.11

func ToBool(v any) (bool, error)

ToBool try to convert type to bool

func ToDuration added in v0.6.9

func ToDuration(s string) (time.Duration, error)

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 extend unit d, w at the end of string. such as "1d", "2w".
  • support long string unit at end. such as "1hour", "2hours", "3minutes", "4mins", "5days", "1weeks".

If the string is not a valid duration string, it will return an error.

func Workdir added in v0.6.8

func Workdir() string

Workdir get

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL