Skip to content

Commit

Permalink
up: update readme template and re-generate README
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 17, 2022
1 parent c59524a commit 6a1054c
Show file tree
Hide file tree
Showing 8 changed files with 655 additions and 495 deletions.
424 changes: 234 additions & 190 deletions README.md

Large diffs are not rendered by default.

431 changes: 237 additions & 194 deletions README.zh-CN.md

Large diffs are not rendered by default.

49 changes: 40 additions & 9 deletions internal/template/README.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,45 @@

💪 Useful utils package for the Go: int, string, array/slice, map, error, time, format, CLI, ENV, filesystem, system, testing and more.

> **[中文说明](README.zh-CN.md)**

**Basic packages:**

- [`arrutil`](./arrutil): Array/Slice util functions. eg: check, convert, formatting, enum, collections
- [`cflag`](./cflag): Wraps and extends go `flag.FlagSet` to build simple command line applications
- [`cliutil`](./cliutil) Command-line util functions. eg: colored print, read input, exec command
- [cmdline](./cliutil/cmdline) Provide cmdline parse, args build to cmdline
- [`dump`](./dump): GO value printing tool. print slice, map will auto wrap each element and display the call location
- [`errorx`](./errorx) Provide an enhanced error implements for go, allow with stacktrace and wrap another error.
- [`envutil`](./envutil) ENV util for current runtime env information. eg: get one, get info, parse var
- [`fmtutil`](./fmtutil) Format data util functions. eg: data, size, time
- [`fsutil`](./fsutil) Filesystem util functions, quick create, read and write file. eg: file and dir check, operate
- [`jsonutil`](./jsonutil) some util functions for quick read, write, encode, decode JSON data.
- [`maputil`](./maputil) Map data util functions. eg: convert, sub-value get, simple merge
- [`mathutil`](./mathutil) Math(int, number) util functions. eg: convert, math calc, random
- `netutil` Network util functions
- [httpreq](netutil/httpreq) An easier-to-use HTTP client that wraps http.Client
- [`reflects`](./reflects) Provide extends reflect util functions.
- [`stdutil`](./stdutil) Provide some commonly std util functions.
- [`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
- [textscan](strutil/textscan) Implemented a parser that quickly scans and analyzes text content. It can be used to parse INI, Properties and other formats
- [`sysutil`](./sysutil) System util functions. eg: sysenv, exec, user, process
- [process](./sysutil/process) Provide some process handle util functions.

**Advance packages:**

- [`cflag`](./cflag): Wraps and extends go `flag.FlagSet` to build simple command line applications
- cli util:
- [cmdline](./cliutil/cmdline) Provide cmdline parse, args build to cmdline
- [`dump`](./dump): GO value printing tool. print slice, map will auto wrap each element and display the call location
- [`errorx`](./errorx) Provide an enhanced error implements for go, allow with stacktrace and wrap another error.
- net util:
- [httpreq](netutil/httpreq) An easier-to-use HTTP client that wraps http.Client
- 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
- 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
- such as: DayStart(), DayAfter(), DayAgo(), DateFormat() and more.

> **[中文说明](README.zh-CN.md)**

## Go Doc

Please see [Go doc](https://pkg.go.dev/github.com/gookit/goutil)
Expand All @@ -49,6 +58,28 @@ Please see [Go doc](https://pkg.go.dev/github.com/gookit/goutil)
go get github.com/gookit/goutil
```

## Usage

```go
// github.com/gookit/goutil
is.True(goutil.IsEmpty(nil))
is.False(goutil.IsEmpty("abc"))

is.True(goutil.IsEqual("a", "a"))
is.True(goutil.IsEqual([]string{"a"}, []string{"a"}))
is.True(goutil.IsEqual(23, 23))

is.True(goutil.Contains("abc", "a"))
is.True(goutil.Contains([]string{"abc", "def"}, "abc"))
is.True(goutil.Contains(map[int]string{2: "abc", 4: "def"}, 4))

// convert type
str = goutil.String(23) // "23"
iVal = goutil.Int("-2") // 2
i64Val = goutil.Int64("-2") // -2
u64Val = goutil.Uint("2") // 2
```

## Packages
{{pgkFuncs}}
## Code Check & Testing
Expand Down
56 changes: 43 additions & 13 deletions internal/template/README.zh-CN.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,44 @@
[![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)

Go一些常用的工具函数实现、增强、收集和整理
Go一些常用的string、number、slice、map、struct、env、system等工具函数实现、增强、收集和整理。

> **[EN README](README.md)**

**基础工具包**

- [`arrutil`](./arrutil) array/slice 相关操作的函数工具包 如:类型转换,元素检查等等
- [`cflag`](./cflag): 包装和扩展 go `flag.FlagSet` 以构建简单的命令行应用程序
- [`cliutil`](./cliutil) CLI 的一些工具函数包. eg: read input, exec command
- [cmdline](./cliutil/cmdline) 提供 cmdline 解析,args 构建到 cmdline
- [`dump`](./dump) GO变量打印工具,打印 slice, map 会自动换行显示每个元素,同时会显示打印调用位置
- [`errorx`](./errorx) 为 go 提供增强的错误实现,允许带有堆栈跟踪信息和包装另一个错误。
- [`envutil`](./envutil) ENV 信息获取判断工具包. eg: get one, get info, parse var
- [`fmtutil`](./fmtutil) 格式化数据工具函数 eg:数据size
- [`fsutil`](./fsutil) 文件系统操作相关的工具函数包. eg: file and dir check, operate
- [`jsonutil`](./jsonutil) 一些用于快速读取、写入、编码、解码 JSON 数据的实用函数。
- [`maputil`](./maputil) map 相关操作的函数工具包. eg: convert, sub-value get, simple merge
- [`mathutil`](./mathutil) int/number 相关操作的函数工具包. eg: convert, math calc, random
- `netutil/httpreq` 包装 http.Client 实现的更加易于使用的HTTP客户端
- [`reflects`](./reflects) 提供一些扩展性的反射使用工具函数.
- [`stdutil`](./stdutil) 提供一些常用的 std util 函数。
- [`structs`](./structs) 为 struct 提供一些扩展 util 函数。 eg: tag parse, struct data
- [`strutil`](./strutil) string 相关操作的函数工具包. eg: bytes, check, convert, encode, format and more
- [textscan](strutil/textscan) 实现了一个快速扫描和分析文本内容的解析器. 可用于解析 INI, Properties 等格式内容
- [`sysutil`](./sysutil) system 相关操作的函数工具包. eg: sysenv, exec, user, process
- [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 实现。添加更多常用的功能方法
- 例如: DayStart(), DayAfter(), DayAgo(), DateFormat() 等等

> **[EN README](README.md)**
**扩展工具包**

- [`cflag`](./cflag): 包装和扩展 go `flag.FlagSet` 以方便快速的构建简单的命令行应用程序
- [`dump`](./dump) GO变量打印工具,打印 slice, map 会自动换行显示每个元素,同时会显示打印调用位置
- [`errorx`](./errorx) 为 go 提供增强的错误实现,允许带有堆栈跟踪信息和包装另一个错误。
- strutil:
- `netutil/httpreq` 包装 http.Client 实现的更加易于使用的HTTP客户端
- strutil:
- [textscan](strutil/textscan) 实现了一个快速扫描和分析文本内容的解析器. 可用于解析 INI, Properties 等格式内容
- sysutil:
- [clipboard](sysutil/clipboard) 提供简单的剪贴板读写操作工具库
- [cmdr](sysutil/cmdr) 提供快速构建和运行一个cmd,批量运行多个cmd任务
- [`testutil`](testutil) test help 相关操作的函数工具包. eg: http test, mock ENV value
- [assert](testutil/assert) 用于帮助测试的断言函数工具包
- [`timex`](timex) 提供增强的 time.Time 实现。添加更多常用的功能方法
- 例如: DayStart(), DayAfter(), DayAgo(), DateFormat() 等等

## GoDoc

Expand All @@ -48,6 +56,28 @@ Go一些常用的工具函数实现、增强、收集和整理
go get github.com/gookit/goutil
```

## Usage

```go
// github.com/gookit/goutil
is.True(goutil.IsEmpty(nil))
is.False(goutil.IsEmpty("abc"))

is.True(goutil.IsEqual("a", "a"))
is.True(goutil.IsEqual([]string{"a"}, []string{"a"}))
is.True(goutil.IsEqual(23, 23))

is.True(goutil.Contains("abc", "a"))
is.True(goutil.Contains([]string{"abc", "def"}, "abc"))
is.True(goutil.Contains(map[int]string{2: "abc", 4: "def"}, 4))

// convert type
str = goutil.String(23) // "23"
iVal = goutil.Int("-2") // 2
i64Val = goutil.Int64("-2") // -2
u64Val = goutil.Uint("2") // 2
```

## Packages
{{pgkFuncs}}
## Code Check & Testing
Expand Down
76 changes: 76 additions & 0 deletions sysutil/sysgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package sysutil

import (
"errors"
"regexp"
"runtime"
"strings"
)

// GoVersion get go runtime version. eg: "1.18.2"
func GoVersion() string {
return runtime.Version()[2:]
}

// GoInfo define
//
// On os by:
//
// go env GOVERSION GOOS GOARCH
// go version // "go version go1.19 darwin/amd64"
type GoInfo struct {
Version string
GoOS string
Arch string
}

// match "go version go1.19 darwin/amd64"
var goVerRegex = regexp.MustCompile(`\sgo([\d.]+)\s(\w+)/(\w+)`)

// ParseGoVersion get info by parse `go version` results.
//
// Examples:
//
// line, err := sysutil.ExecLine("go version")
// if err != nil {
// return err
// }
//
// info, err := sysutil.ParseGoVersion()
// dump.P(info)
func ParseGoVersion(line string) (*GoInfo, error) {
// eg: [" go1.19 darwin/amd64", "1.19", "darwin", "amd64"]
lines := goVerRegex.FindStringSubmatch(line)
if len(lines) != 4 {
return nil, errors.New("returns go info is not full")
}

info := &GoInfo{}
info.Version = strings.TrimPrefix(lines[1], "go")
info.GoOS = lines[2]
info.Arch = lines[3]

return info, nil
}

// OsGoInfo fetch and parse
func OsGoInfo() (*GoInfo, error) {
cmdArgs := []string{"env", "GOVERSION", "GOOS", "GOARCH"}
line, err := ExecCmd("go", cmdArgs)
if err != nil {
return nil, err
}

lines := strings.Split(strings.TrimSpace(line), "\n")

if len(lines) != len(cmdArgs)-1 {
return nil, errors.New("returns go info is not full")
}

info := &GoInfo{}
info.Version = strings.TrimPrefix(lines[0], "go")
info.GoOS = lines[1]
info.Arch = lines[2]

return info, nil
}
25 changes: 25 additions & 0 deletions sysutil/sysgo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sysutil_test

import (
"testing"

"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/sysutil"
"github.com/gookit/goutil/testutil/assert"
)

func TestGoVersion(t *testing.T) {
assert.NotEmpty(t, sysutil.GoVersion())

info, err := sysutil.ParseGoVersion("go version go1.19.2 darwin/amd64")
assert.NoErr(t, err)
assert.NotEmpty(t, info)
assert.Eq(t, "1.19.2", info.Version)
assert.Eq(t, "darwin", info.GoOS)
assert.Eq(t, "amd64", info.Arch)

info, err = sysutil.OsGoInfo()
assert.NoErr(t, err)
assert.NotEmpty(t, info)
dump.P(info)
}
72 changes: 0 additions & 72 deletions sysutil/sysutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
package sysutil

import (
"errors"
"os"
"path"
"regexp"
"runtime"
"strings"
)

// Workdir get
Expand All @@ -26,71 +22,3 @@ func BinDir() string {
func BinFile() string {
return os.Args[0]
}

// GoVersion get go runtime version. eg: "1.18.2"
func GoVersion() string {
return runtime.Version()[2:]
}

// GoInfo define
//
// On os by:
//
// go env GOVERSION GOOS GOARCH
// go version // "go version go1.19 darwin/amd64"
type GoInfo struct {
Version string
GoOS string
Arch string
}

// match "go version go1.19 darwin/amd64"
var goVerRegex = regexp.MustCompile(`\sgo([\d.]+)\s(\w+)/(\w+)`)

// ParseGoVersion get info by parse `go version` results.
//
// Examples:
//
// line, err := sysutil.ExecLine("go version")
// if err != nil {
// return err
// }
//
// info, err := sysutil.ParseGoVersion()
// dump.P(info)
func ParseGoVersion(line string) (*GoInfo, error) {
// eg: [" go1.19 darwin/amd64", "1.19", "darwin", "amd64"]
lines := goVerRegex.FindStringSubmatch(line)
if len(lines) != 4 {
return nil, errors.New("returns go info is not full")
}

info := &GoInfo{}
info.Version = strings.TrimPrefix(lines[1], "go")
info.GoOS = lines[2]
info.Arch = lines[3]

return info, nil
}

// OsGoInfo fetch and parse
func OsGoInfo() (*GoInfo, error) {
cmdArgs := []string{"env", "GOVERSION", "GOOS", "GOARCH"}
line, err := ExecCmd("go", cmdArgs)
if err != nil {
return nil, err
}

lines := strings.Split(strings.TrimSpace(line), "\n")

if len(lines) != len(cmdArgs)-1 {
return nil, errors.New("returns go info is not full")
}

info := &GoInfo{}
info.Version = strings.TrimPrefix(lines[0], "go")
info.GoOS = lines[1]
info.Arch = lines[2]

return info, nil
}
17 changes: 0 additions & 17 deletions sysutil/sysutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"testing"

"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/sysutil"
"github.com/gookit/goutil/testutil/assert"
)
Expand All @@ -19,19 +18,3 @@ func TestProcessExists(t *testing.T) {

assert.True(t, sysutil.ProcessExists(pid))
}

func TestGoVersion(t *testing.T) {
assert.NotEmpty(t, sysutil.GoVersion())

info, err := sysutil.ParseGoVersion("go version go1.19.2 darwin/amd64")
assert.NoErr(t, err)
assert.NotEmpty(t, info)
assert.Eq(t, "1.19.2", info.Version)
assert.Eq(t, "darwin", info.GoOS)
assert.Eq(t, "amd64", info.Arch)

info, err = sysutil.OsGoInfo()
assert.NoErr(t, err)
assert.NotEmpty(t, info)
dump.P(info)
}

0 comments on commit 6a1054c

Please sign in to comment.