Skip to content

Commit

Permalink
👔 up: update some cli and map util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 16, 2023
1 parent a2257a1 commit ff8619f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 12 deletions.
9 changes: 9 additions & 0 deletions cliutil/cliutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import (
"github.com/gookit/goutil/sysutil"
)

// SplitMulti split multi string by sep string.
func SplitMulti(ss []string, sep string) []string {
ns := make([]string, 0, len(ss)+1)
for _, s := range ss {
ns = append(ns, strings.Split(s, sep)...)
}
return ns
}

// LineBuild build command line string by given args.
func LineBuild(binFile string, args []string) string {
return cmdline.NewBuilder(binFile, args...).String()
Expand Down
9 changes: 9 additions & 0 deletions cliutil/cliutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import (
"github.com/gookit/goutil/testutil/assert"
)

// test SplitMulti
func TestSplitMulti(t *testing.T) {
ss := cliutil.SplitMulti([]string{"a,b", "c,d"}, ",")
assert.Equal(t, []string{"a", "b", "c", "d"}, ss)

ss = cliutil.SplitMulti([]string{"a,b", "c,d"}, ",,")
assert.Equal(t, []string{"a,b", "c,d"}, ss)
}

func TestCurrentShell(t *testing.T) {
path := cliutil.CurrentShell(true)

Expand Down
4 changes: 2 additions & 2 deletions internal/template/README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- [`strutil`](strutil) String util functions. eg: bytes, check, convert, encode, format and more
- [`sysutil`](sysutil) System util functions. eg: sysenv, exec, user, process

**Advance packages:**
**Extra packages:**

- [`cflag`](cflag): Wraps and extends go `flag.FlagSet` to build simple command line applications
- cli util:
Expand All @@ -38,7 +38,7 @@
- file util:
- [`finder`](fsutil/finder) Provides a simple and convenient filedir lookup function, supports filtering, excluding, matching, ignoring, etc.
- net util:
- [httpreq](netutil/httpreq) An easier-to-use HTTP client that wraps http.Client
- [httpreq](netutil/httpreq) An easier-to-use HTTP client that wraps http.Client, and with some http utils.
- string util:
- [textscan](strutil/textscan) Implemented a parser that quickly scans and analyzes text content. It can be used to parse INI, Properties and other formats
- [textutil](strutil/textutil) Provide some extensions text handle util functions. eg: text replace, etc.
Expand Down
2 changes: 1 addition & 1 deletion internal/template/README.zh-CN.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- [`errorx`](./errorx) 为 go 提供增强的错误实现,允许带有堆栈跟踪信息和包装另一个错误。
- [`finder`](./fsutil/finder) 提供简单方便的file/dir查找功能,支持过滤、排除、匹配、忽略等。
- netutil 子包:
- `netutil/httpreq` 包装 http.Client 实现的更加易于使用的HTTP客户端
- `netutil/httpreq` 包装 http.Client 实现的更加易于使用的HTTP客户端, 和一些 http 工具函数
- strutil 子包:
- [textscan](strutil/textscan) 实现了一个快速扫描和分析文本内容的解析器. 可用于解析 INI, Properties 等格式内容
- [textutil](strutil/textutil) 提供一些常用的扩展文本处理功能函数。
Expand Down
6 changes: 3 additions & 3 deletions maputil/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "fmt"
type Aliases map[string]string

// AddAlias to the Aliases
func (as Aliases) AddAlias(real, alias string) {
func (as Aliases) AddAlias(alias, real string) {
if rn, ok := as[alias]; ok {
panic(fmt.Sprintf("The alias '%s' is already used by '%s'", alias, rn))
}
Expand All @@ -16,14 +16,14 @@ func (as Aliases) AddAlias(real, alias string) {
// AddAliases to the Aliases
func (as Aliases) AddAliases(real string, aliases []string) {
for _, a := range aliases {
as.AddAlias(real, a)
as.AddAlias(a, real)
}
}

// AddAliasMap to the Aliases
func (as Aliases) AddAliasMap(alias2real map[string]string) {
for a, r := range alias2real {
as.AddAlias(r, a)
as.AddAlias(a, r)
}
}

Expand Down
4 changes: 2 additions & 2 deletions maputil/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestAliases_AddAlias(t *testing.T) {
as := make(maputil.Aliases)
as.AddAlias("real", "a")
as.AddAlias("a", "real")
as.AddAliases("real", []string{"b"})
as.AddAliasMap(map[string]string{"a1": "real1"})

Expand All @@ -24,6 +24,6 @@ func TestAliases_AddAlias(t *testing.T) {
assert.Eq(t, "notExist", as.ResolveAlias("notExist"))

assert.PanicsMsg(t, func() {
as.AddAlias("real3", "a")
as.AddAlias("a", "real3")
}, "The alias 'a' is already used by 'real'")
}
8 changes: 4 additions & 4 deletions mathutil/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
// RandomInt(100, 999)
// RandomInt(1000, 9999)
func RandomInt(min, max int) int {
rand.Seed(time.Now().UnixNano())
return min + rand.Intn(max-min)
rr := rand.New(rand.NewSource(time.Now().UnixNano()))
return min + rr.Intn(max-min)
}

// RandInt alias of RandomInt()
Expand All @@ -32,6 +32,6 @@ func RandIntWithSeed(min, max int, seed int64) int {
// seed := time.Now().UnixNano()
// RandomIntWithSeed(1000, 9999, seed)
func RandomIntWithSeed(min, max int, seed int64) int {
rand.Seed(seed)
return min + rand.Intn(max-min)
rr := rand.New(rand.NewSource(seed))
return min + rr.Intn(max-min)
}

0 comments on commit ff8619f

Please sign in to comment.