comfunc

package
v0.0.0-...-f5003fd Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 12 Imported by: 0

README

common func for internal use

  • don't depend on other external packages

Documentation

Index

Constants

This section is empty.

Variables

View Source
var StrBySprintFn = func(v any) (string, error) {
	return fmt.Sprint(v), nil
}

StrBySprintFn convert any value to string by fmt.Sprint

Functions

func Bool

func Bool(v any) bool

Bool try to convert type to bool

func Cmdline

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

Cmdline build

func CurrentShell

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

func Environ() map[string]string

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

func ExecCmd

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

ExecCmd an command and return output.

Usage:

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

func ExpandHome

func ExpandHome(pathStr string) string

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

func FormatWithArgs

func FormatWithArgs(fmtAndArgs []any) string

FormatWithArgs format message with args

func HasShellEnv

func HasShellEnv(shell string) bool

HasShellEnv has shell env check.

Usage:

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

func IsDuration

func IsDuration(s string) bool

IsDuration check the string is a duration string.

func JoinPaths2

func JoinPaths2(basePath string, elems []string) string

JoinPaths2 elements, like the filepath.Join()

func JoinPaths3

func JoinPaths3(basePath, secPath string, elems []string) string

JoinPaths3 elements, like the filepath.Join()

func ShellExec

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

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

func StrToBool

func StrToBool(s string) (bool, error)

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

func ToBool

func ToBool(v any) (bool, error)

ToBool try to convert type to bool

func ToDuration

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 ToStringWith

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.

func WithHandlePtr

func WithHandlePtr(opt *ConvOption)

WithHandlePtr set ConvOption.HandlePtr option

func Workdir

func Workdir() string

Workdir get

Types

type ConvOption

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

func NewConvOption(optFns ...ConvOptionFn) *ConvOption

NewConvOption create a new ConvOption

func (*ConvOption) WithOption

func (opt *ConvOption) WithOption(optFns ...ConvOptionFn)

WithOption set convert option

type ConvOptionFn

type ConvOptionFn func(opt *ConvOption)

ConvOptionFn convert option func

func WithUserConvFn

func WithUserConvFn(fn comdef.ToStringFunc) ConvOptionFn

WithUserConvFn set ConvOption.UserConvFn option

Jump to

Keyboard shortcuts

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