diff --git a/README.md b/README.md index d7694318d..66658a976 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![Coverage Status](https://coveralls.io/repos/github/gookit/goutil/badge.svg?branch=master)](https://coveralls.io/github/gookit/goutil?branch=master) [![Go Reference](https://pkg.go.dev/badge/github.com/gookit/goutil.svg)](https://pkg.go.dev/github.com/gookit/goutil) -💪 Useful utils package for the Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem, system, testing and more. +💪 Useful utils(**500+**) package for the Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem, system, testing and more. > **[中文说明](README.zh-CN.md)** @@ -27,7 +27,6 @@ - [`structs`](./structs) Provide some extends util functions for struct. eg: tag parse, struct data init - [`strutil`](./strutil) String util functions. eg: bytes, check, convert, encode, format and more - [`sysutil`](./sysutil) System util functions. eg: sysenv, exec, user, process - - [process](./sysutil/process) Provide some process handle util functions. **Advance packages:** @@ -43,6 +42,7 @@ - sys util: - [clipboard](./sysutil/clipboard) Provide a simple clipboard read and write operations. - [cmdr](./sysutil/cmdr) Provide for quick build and run a cmd, batch run multi cmd tasks + - [process](./sysutil/process) Provide some process handle util functions. - [`testutil`](./testutil) Test help util functions. eg: http test, mock ENV value - [assert](./testutil/assert) Asserts functions for help testing - [`timex`](./timex) Provides an enhanced time.Time implementation. Add more commonly used functional methods @@ -95,7 +95,8 @@ func StringsMap(ss []string, mapFn func(s string) string) []string func TrimStrings(ss []string, cutSet ...string) []string func GetRandomOne[T any](arr []T) T func RandomOne[T any](arr []T) T -func Unique[T ~string | comdef.XintOrFloat](arr []T) []T +func Unique[T ~string | comdef.XintOrFloat](list []T) []T +func IndexOf[T ~string | comdef.XintOrFloat](val T, list []T) int // source at arrutil/check.go func IntsHas(ints []int, val int) bool func Int64sHas(ints []int64, val int64) bool @@ -171,6 +172,24 @@ ss, err := arrutil.ToStrings([]int{1, 2}) // ss: []string{"1", "2"} ``` +### Basefn + +> Package `github.com/gookit/goutil/basefn` + +```go +// source at basefn/basefunc.go +func Panicf(format string, v ...any) +func MustOK(err error) +func Must[T any](v T, err error) T +func ErrOnFail(cond bool, err error) error +func OrError(cond bool, err error) error +func FirstOr[T any](sl []T, elseVal T) T +func OrValue[T any](cond bool, okVal, elVal T) T +func OrReturn[T any](cond bool, okFn, elseFn func() T) T +func CallOn(cond bool, fn ErrFunc) error +func CallOrElse(cond bool, okFn, elseFn ErrFunc) error +``` + ### Bytes Utils > Package `github.com/gookit/goutil/byteutil` @@ -449,6 +468,7 @@ func GetInt(name string, def ...int) int func GetBool(name string, def ...bool) bool func GetMulti(names ...string) map[string]string func EnvPaths() []string +func EnvMap() map[string]string func Environ() map[string]string func SearchEnvKeys(keywords string) map[string]string func SearchEnv(keywords string, matchValue bool) map[string]string @@ -652,6 +672,8 @@ func TempDir(dir, pattern string) (string, error) func MimeType(path string) (mime string) func ReaderMimeType(r io.Reader) (mime string) func JoinPaths(elem ...string) string +func SlashPath(path string) string +func UnixPath(path string) string // source at fsutil/info.go func Dir(fpath string) string func PathName(fpath string) string @@ -673,6 +695,8 @@ func MkSubDirs(perm os.FileMode, parentDir string, subDirs ...string) error func MkParentDir(fpath string) error func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error) +func OpenAppendFile(filepath string) (*os.File, error) +func OpenTruncFile(filepath string) (*os.File, error) func OpenReadFile(filepath string) (*os.File, error) func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*os.File, error) func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File @@ -823,6 +847,7 @@ func MustUint(in any) uint64 func UintOrErr(in any) (uint64, error) func ToUint(in any) (u64 uint64, err error) func Int64(in any) (int64, error) +func SafeInt64(in any) int64 func QuietInt64(in any) int64 func MustInt64(in any) int64 func Int64OrErr(in any) (int64, error) @@ -915,6 +940,9 @@ func ReadString(r io.Reader) string func MustReadReader(r io.Reader) []byte func NewIOReader(in any) io.Reader func NewScanner(in any) *bufio.Scanner +func WriteBytes(bs []byte) +func WriteString(s string) +func Writeln(s string) // source at stdio/writer.go func NewWriteWrapper(w io.Writer) *WriteWrapper ``` @@ -1192,10 +1220,11 @@ func SplitInlineComment(val string, strict ...bool) (string, string) func FirstLine(output string) string // source at strutil/strutil.go func OrCond(cond bool, s1, s2 string) string -func OrElse(s, newVal string) string +func OrElse(s, orVal string) string func OrHandle(s string, fn func(s string) string) string func Valid(ss ...string) string func Replaces(str string, pairs map[string]string) string +func NewReplacer(pairs map[string]string) *strings.Replacer func PrettyJSON(v any) (string, error) func RenderTemplate(input string, data any, fns template.FuncMap, isFile ...bool) string func RenderText(input string, data any, fns template.FuncMap, isFile ...bool) string @@ -1231,6 +1260,7 @@ func Executable(binName string) (string, error) func HasExecutable(binName string) bool func Getenv(name string, def ...string) string func Environ() map[string]string +func EnvMapWith(newEnv map[string]string) map[string]string func EnvPaths() []string func SearchPath(keywords string, limit int) []string // source at sysutil/sysgo.go @@ -1244,7 +1274,7 @@ func BinFile() string func Open(fileOrUrl string) error func OpenBrowser(fileOrUrl string) error func OpenFile(path string) error -// source at sysutil/sysutil_darwin.go +// source at sysutil/sysutil_linux.go func IsWin() bool func IsWindows() bool func IsMac() bool diff --git a/README.zh-CN.md b/README.zh-CN.md index 85a74b77a..0c19e05ea 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -7,7 +7,7 @@ [![Coverage Status](https://coveralls.io/repos/github/gookit/goutil/badge.svg?branch=master)](https://coveralls.io/github/gookit/goutil?branch=master) [![Go Reference](https://pkg.go.dev/badge/github.com/gookit/goutil.svg)](https://pkg.go.dev/github.com/gookit/goutil) -`goutil` Go 常用功能的扩展工具库。包含:数字,byte, 字符串,slice/数组,Map,结构体,反射,文本,文件,错误,时间日期,测试,特殊处理,格式化,常用信息获取等等。 +`goutil` Go 常用功能的扩展工具库(**500+**)。包含:数字,byte, 字符串,slice/数组,Map,结构体,反射,文本,文件,错误,时间日期,测试,特殊处理,格式化,常用信息获取等等。 > **[EN README](README.md)** @@ -27,7 +27,6 @@ - [`structs`](./structs) 为 struct 提供一些扩展 util 函数。 eg: tag parse, struct data - [`strutil`](./strutil) string 相关操作的函数工具包. eg: bytes, check, convert, encode, format and more - [`sysutil`](./sysutil) system 相关操作的函数工具包. eg: sysenv, exec, user, process - - [process](./sysutil/process) 提供一些进程操作相关的实用功能。 **扩展工具包** @@ -41,6 +40,7 @@ - sysutil: - [clipboard](sysutil/clipboard) 提供简单的剪贴板读写操作工具库 - [cmdr](sysutil/cmdr) 提供快速构建和运行一个cmd,批量运行多个cmd任务 + - [process](./sysutil/process) 提供一些进程操作相关的实用功能。 - [`testutil`](testutil) test help 相关操作的函数工具包. eg: http test, mock ENV value - [assert](testutil/assert) 用于帮助测试的断言函数工具包 - [`timex`](timex) 提供增强的 time.Time 实现。添加更多常用的功能方法 @@ -94,7 +94,8 @@ func StringsMap(ss []string, mapFn func(s string) string) []string func TrimStrings(ss []string, cutSet ...string) []string func GetRandomOne[T any](arr []T) T func RandomOne[T any](arr []T) T -func Unique[T ~string | comdef.XintOrFloat](arr []T) []T +func Unique[T ~string | comdef.XintOrFloat](list []T) []T +func IndexOf[T ~string | comdef.XintOrFloat](val T, list []T) int // source at arrutil/check.go func IntsHas(ints []int, val int) bool func Int64sHas(ints []int64, val int64) bool @@ -170,6 +171,24 @@ ss, err := arrutil.ToStrings([]int{1, 2}) // ss: []string{"1", "2"} ``` +### Basefn + +> Package `github.com/gookit/goutil/basefn` + +```go +// source at basefn/basefunc.go +func Panicf(format string, v ...any) +func MustOK(err error) +func Must[T any](v T, err error) T +func ErrOnFail(cond bool, err error) error +func OrError(cond bool, err error) error +func FirstOr[T any](sl []T, elseVal T) T +func OrValue[T any](cond bool, okVal, elVal T) T +func OrReturn[T any](cond bool, okFn, elseFn func() T) T +func CallOn(cond bool, fn ErrFunc) error +func CallOrElse(cond bool, okFn, elseFn ErrFunc) error +``` + ### Bytes Utils > Package `github.com/gookit/goutil/byteutil` @@ -448,6 +467,7 @@ func GetInt(name string, def ...int) int func GetBool(name string, def ...bool) bool func GetMulti(names ...string) map[string]string func EnvPaths() []string +func EnvMap() map[string]string func Environ() map[string]string func SearchEnvKeys(keywords string) map[string]string func SearchEnv(keywords string, matchValue bool) map[string]string @@ -653,6 +673,8 @@ func TempDir(dir, pattern string) (string, error) func MimeType(path string) (mime string) func ReaderMimeType(r io.Reader) (mime string) func JoinPaths(elem ...string) string +func SlashPath(path string) string +func UnixPath(path string) string // source at fsutil/info.go func Dir(fpath string) string func PathName(fpath string) string @@ -674,6 +696,8 @@ func MkSubDirs(perm os.FileMode, parentDir string, subDirs ...string) error func MkParentDir(fpath string) error func OpenFile(filepath string, flag int, perm os.FileMode) (*os.File, error) func QuickOpenFile(filepath string, fileFlag ...int) (*os.File, error) +func OpenAppendFile(filepath string) (*os.File, error) +func OpenTruncFile(filepath string) (*os.File, error) func OpenReadFile(filepath string) (*os.File, error) func CreateFile(fpath string, filePerm, dirPerm os.FileMode, fileFlag ...int) (*os.File, error) func MustCreateFile(filePath string, filePerm, dirPerm os.FileMode) *os.File @@ -824,6 +848,7 @@ func MustUint(in any) uint64 func UintOrErr(in any) (uint64, error) func ToUint(in any) (u64 uint64, err error) func Int64(in any) (int64, error) +func SafeInt64(in any) int64 func QuietInt64(in any) int64 func MustInt64(in any) int64 func Int64OrErr(in any) (int64, error) @@ -916,6 +941,9 @@ func ReadString(r io.Reader) string func MustReadReader(r io.Reader) []byte func NewIOReader(in any) io.Reader func NewScanner(in any) *bufio.Scanner +func WriteBytes(bs []byte) +func WriteString(s string) +func Writeln(s string) // source at stdio/writer.go func NewWriteWrapper(w io.Writer) *WriteWrapper ``` @@ -1193,10 +1221,11 @@ func SplitInlineComment(val string, strict ...bool) (string, string) func FirstLine(output string) string // source at strutil/strutil.go func OrCond(cond bool, s1, s2 string) string -func OrElse(s, newVal string) string +func OrElse(s, orVal string) string func OrHandle(s string, fn func(s string) string) string func Valid(ss ...string) string func Replaces(str string, pairs map[string]string) string +func NewReplacer(pairs map[string]string) *strings.Replacer func PrettyJSON(v any) (string, error) func RenderTemplate(input string, data any, fns template.FuncMap, isFile ...bool) string func RenderText(input string, data any, fns template.FuncMap, isFile ...bool) string @@ -1232,6 +1261,7 @@ func Executable(binName string) (string, error) func HasExecutable(binName string) bool func Getenv(name string, def ...string) string func Environ() map[string]string +func EnvMapWith(newEnv map[string]string) map[string]string func EnvPaths() []string func SearchPath(keywords string, limit int) []string // source at sysutil/sysgo.go @@ -1245,7 +1275,7 @@ func BinFile() string func Open(fileOrUrl string) error func OpenBrowser(fileOrUrl string) error func OpenFile(path string) error -// source at sysutil/sysutil_darwin.go +// source at sysutil/sysutil_linux.go func IsWin() bool func IsWindows() bool func IsMac() bool diff --git a/dump/_examples/go.mod b/dump/_examples/go.mod index 04dbe451e..09352b315 100644 --- a/dump/_examples/go.mod +++ b/dump/_examples/go.mod @@ -1,12 +1,19 @@ module dump_example -go 1.16 +go 1.18 require ( - github.com/gookit/color v1.5.0 - github.com/gookit/goutil v0.0.0-00010101000000-000000000000 + github.com/gookit/color v1.5.2 + github.com/gookit/goutil v0.6.0-00010101000000-000000000000 github.com/kortschak/utter v1.0.1 github.com/kr/pretty v0.2.1 ) +require ( + github.com/kr/text v0.1.0 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/text v0.8.0 // indirect +) + replace github.com/gookit/goutil => ../../ diff --git a/dump/_examples/go.sum b/dump/_examples/go.sum index 9ddf4d80f..13d81ab8f 100644 --- a/dump/_examples/go.sum +++ b/dump/_examples/go.sum @@ -1,7 +1,8 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw= -github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gookit/color v1.5.2 h1:uLnfXcaFjlrDnQDT+NCBcfhrXqYTx/rcCa6xn01Y8yI= +github.com/gookit/color v1.5.2/go.mod h1:w8h4bGiHeeBpvQVePTutdbERIUf3oJE5lZ8HM0UgXyg= github.com/kortschak/utter v1.0.1 h1:AJVccwLrdrikvkH0aI5JKlzZIORLpfMeGBQ5tHfIXis= github.com/kortschak/utter v1.0.1/go.mod h1:vSmSjbyrlKjjsL71193LmzBOKgwePk9DH6uFaWHIInc= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= @@ -9,27 +10,23 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/template/README.md.tpl b/internal/template/README.md.tpl index 61d601028..a69760b1e 100644 --- a/internal/template/README.md.tpl +++ b/internal/template/README.md.tpl @@ -7,7 +7,7 @@ [![Coverage Status](https://coveralls.io/repos/github/gookit/goutil/badge.svg?branch=master)](https://coveralls.io/github/gookit/goutil?branch=master) [![Go Reference](https://pkg.go.dev/badge/github.com/gookit/goutil.svg)](https://pkg.go.dev/github.com/gookit/goutil) -💪 Useful utils package for the Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem, system, testing and more. +💪 Useful utils(**500+**) package for the Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem, system, testing and more. > **[中文说明](README.zh-CN.md)** @@ -27,7 +27,6 @@ - [`structs`](./structs) Provide some extends util functions for struct. eg: tag parse, struct data init - [`strutil`](./strutil) String util functions. eg: bytes, check, convert, encode, format and more - [`sysutil`](./sysutil) System util functions. eg: sysenv, exec, user, process - - [process](./sysutil/process) Provide some process handle util functions. **Advance packages:** @@ -43,6 +42,7 @@ - sys util: - [clipboard](./sysutil/clipboard) Provide a simple clipboard read and write operations. - [cmdr](./sysutil/cmdr) Provide for quick build and run a cmd, batch run multi cmd tasks + - [process](./sysutil/process) Provide some process handle util functions. - [`testutil`](./testutil) Test help util functions. eg: http test, mock ENV value - [assert](./testutil/assert) Asserts functions for help testing - [`timex`](./timex) Provides an enhanced time.Time implementation. Add more commonly used functional methods diff --git a/internal/template/README.zh-CN.md.tpl b/internal/template/README.zh-CN.md.tpl index 534491ec2..1957cc230 100644 --- a/internal/template/README.zh-CN.md.tpl +++ b/internal/template/README.zh-CN.md.tpl @@ -7,7 +7,7 @@ [![Coverage Status](https://coveralls.io/repos/github/gookit/goutil/badge.svg?branch=master)](https://coveralls.io/github/gookit/goutil?branch=master) [![Go Reference](https://pkg.go.dev/badge/github.com/gookit/goutil.svg)](https://pkg.go.dev/github.com/gookit/goutil) -`goutil` Go 常用功能的扩展工具库。包含:数字,byte, 字符串,slice/数组,Map,结构体,反射,文本,文件,错误,时间日期,测试,特殊处理,格式化,常用信息获取等等。 +`goutil` Go 常用功能的扩展工具库(**500+**)。包含:数字,byte, 字符串,slice/数组,Map,结构体,反射,文本,文件,错误,时间日期,测试,特殊处理,格式化,常用信息获取等等。 > **[EN README](README.md)** @@ -27,7 +27,6 @@ - [`structs`](./structs) 为 struct 提供一些扩展 util 函数。 eg: tag parse, struct data - [`strutil`](./strutil) string 相关操作的函数工具包. eg: bytes, check, convert, encode, format and more - [`sysutil`](./sysutil) system 相关操作的函数工具包. eg: sysenv, exec, user, process - - [process](./sysutil/process) 提供一些进程操作相关的实用功能。 **扩展工具包** @@ -41,6 +40,7 @@ - sysutil: - [clipboard](sysutil/clipboard) 提供简单的剪贴板读写操作工具库 - [cmdr](sysutil/cmdr) 提供快速构建和运行一个cmd,批量运行多个cmd任务 + - [process](./sysutil/process) 提供一些进程操作相关的实用功能。 - [`testutil`](testutil) test help 相关操作的函数工具包. eg: http test, mock ENV value - [assert](testutil/assert) 用于帮助测试的断言函数工具包 - [`timex`](timex) 提供增强的 time.Time 实现。添加更多常用的功能方法 diff --git a/sysutil/sysenv.go b/sysutil/sysenv.go index e3d9a927b..6573208bc 100644 --- a/sysutil/sysenv.go +++ b/sysutil/sysenv.go @@ -130,8 +130,8 @@ func Getenv(name string, def ...string) string { // Environ like os.Environ, but will returns key-value map[string]string data. func Environ() map[string]string { return comfunc.Environ() } -// EnvironWith like os.Environ, but will return key-value map[string]string data. -func EnvironWith(newEnv map[string]string) map[string]string { +// EnvMapWith like os.Environ, but will return key-value map[string]string data. +func EnvMapWith(newEnv map[string]string) map[string]string { envMp := comfunc.Environ() for name, value := range newEnv { envMp[name] = value diff --git a/sysutil/sysenv_test.go b/sysutil/sysenv_test.go index 093e9ff20..51189601f 100644 --- a/sysutil/sysenv_test.go +++ b/sysutil/sysenv_test.go @@ -14,13 +14,17 @@ func TestSysenv_common(t *testing.T) { ss := sysutil.EnvPaths() assert.NotEmpty(t, ss) assert.NotEmpty(t, sysutil.Environ()) - assert.NotEmpty(t, sysutil.EnvironWith(nil)) + assert.NotEmpty(t, sysutil.EnvMapWith(nil)) + assert.NotEmpty(t, sysutil.EnvMapWith(map[string]string{"NEW_KEY": "value"})) ss = sysutil.SearchPath("go", 3) assert.NotEmpty(t, ss) // dump.P(ss) ss = sysutil.SearchPath("o", 3) assert.NotEmpty(t, ss) + + assert.Empty(t, sysutil.Getenv("NOT_EXISTS_ENV")) + assert.Equal(t, "defVal", sysutil.Getenv("NOT_EXISTS_ENV", "defVal")) } func TestCurrentShell(t *testing.T) { diff --git a/testutil/assert/asserts.go b/testutil/assert/asserts.go index 753d2c277..46c9abbd2 100644 --- a/testutil/assert/asserts.go +++ b/testutil/assert/asserts.go @@ -494,7 +494,7 @@ func Gt(t TestingT, give, min int, fmtAndArgs ...any) bool { // Gte asserts that the give(intX) should not be greater than or equals to min func Gte(t TestingT, give, min int, fmtAndArgs ...any) bool { t.Helper() - return Gt(t, give, min+1, fmtAndArgs...) + return Gt(t, give, min-1, fmtAndArgs...) } // IsType assert data type equals