Skip to content

Commit

Permalink
👔 up(test,doc): update some tests and re-generate docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 18, 2023
1 parent e7c80d4 commit c72adcb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
30 changes: 28 additions & 2 deletions README.md
Expand Up @@ -188,6 +188,9 @@ 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
// source at basefn/extfunc.go
func DataSize(size uint64) string
func HowLongAgo(sec int64) string
```

### Bytes Utils
Expand All @@ -201,6 +204,8 @@ func NewBuffer() *Buffer
func FirstLine(bs []byte) []byte
func StrOrErr(bs []byte, err error) (string, error)
func SafeString(bs []byte, err error) string
func String(b []byte) string
func ToString(b []byte) string
// source at byteutil/bytex.go
func Md5(src any) []byte
// source at byteutil/check.go
Expand Down Expand Up @@ -639,6 +644,8 @@ runtime.goexit()
> Package `github.com/gookit/goutil/fmtutil`
```go
// source at fmtutil/fmtutil.go
func StringOrJSON(v any) ([]byte, error)
// source at fmtutil/format.go
func DataSize(size uint64) string
func SizeToString(size uint64) string
Expand All @@ -664,6 +671,19 @@ func IsFile(path string) bool
func IsAbsPath(aPath string) bool
func IsImageFile(path string) bool
func IsZipFile(filepath string) bool
func PathMatch(pattern, s string) bool
// source at fsutil/find.go
func SearchNameUp(dirPath, name string) string
func SearchNameUpx(dirPath, name string) (string, bool)
func WalkDir(dir string, fn fs.WalkDirFunc) error
func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)
func OnlyFindDir(_ string, ent fs.DirEntry) bool
func OnlyFindFile(_ string, ent fs.DirEntry) bool
func IncludeSuffix(ss ...string) FilterFunc
func ExcludeDotFile(_ string, ent fs.DirEntry) bool
func ExcludeSuffix(ss ...string) FilterFunc
func ApplyFilters(fPath string, ent fs.DirEntry, filters []FilterFunc) bool
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
// source at fsutil/fsutil.go
func OSTempFile(pattern string) (*os.File, error)
func TempFile(dir, pattern string) (*os.File, error)
Expand All @@ -674,6 +694,7 @@ func ReaderMimeType(r io.Reader) (mime string)
func JoinPaths(elem ...string) string
func SlashPath(path string) string
func UnixPath(path string) string
func ToAbsPath(p string) string
// source at fsutil/info.go
func Dir(fpath string) string
func PathName(fpath string) string
Expand All @@ -683,9 +704,8 @@ func Extname(fpath string) string
func Suffix(fpath string) string
func Expand(pathStr string) string
func ExpandPath(pathStr string) string
func ResolvePath(pathStr string) string
func SplitPath(pathStr string) (dir, name string)
func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
// source at fsutil/info_nonwin.go
func Realpath(pathStr string) string
// source at fsutil/operate.go
Expand Down Expand Up @@ -940,7 +960,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 WriteByte(b byte)
func WriteBytes(bs []byte)
func WritelnBytes(bs []byte)
func WriteString(s string)
func Writeln(s string)
// source at stdio/writer.go
Expand Down Expand Up @@ -1053,6 +1075,7 @@ func HasPrefix(s string, prefix string) bool
func IsStartOf(s, prefix string) bool
func HasSuffix(s string, suffix string) bool
func IsEndOf(s, suffix string) bool
func HasOneSuffix(s string, suffixes []string) bool
func IsValidUtf8(s string) bool
func IsSpace(c byte) bool
func IsEmpty(s string) bool
Expand All @@ -1066,7 +1089,9 @@ func IsVersion(s string) bool
func Compare(s1, s2, op string) bool
func VersionCompare(v1, v2, op string) bool
func QuickMatch(pattern, s string) bool
func PathMatch(pattern, s string) bool
func GlobMatch(pattern, s string) bool
func MatchNodePath(pattern, s string, sep string) bool
// source at strutil/convert.go
func Quote(s string) string
func Unquote(s string) string
Expand Down Expand Up @@ -1295,6 +1320,7 @@ func UserDir(subPath string) string
func UserCacheDir(subPath string) string
func UserConfigDir(subPath string) string
func ExpandPath(path string) string
func ExpandHome(path string) string
// source at sysutil/user_nonwin.go
func ChangeUserByName(newUname string) (err error)
func ChangeUserUidGid(newUid int, newGid int) (err error)
Expand Down
30 changes: 28 additions & 2 deletions README.zh-CN.md
Expand Up @@ -187,6 +187,9 @@ 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
// source at basefn/extfunc.go
func DataSize(size uint64) string
func HowLongAgo(sec int64) string
```

### Bytes Utils
Expand All @@ -200,6 +203,8 @@ func NewBuffer() *Buffer
func FirstLine(bs []byte) []byte
func StrOrErr(bs []byte, err error) (string, error)
func SafeString(bs []byte, err error) string
func String(b []byte) string
func ToString(b []byte) string
// source at byteutil/bytex.go
func Md5(src any) []byte
// source at byteutil/check.go
Expand Down Expand Up @@ -640,6 +645,8 @@ runtime.goexit()
> Package `github.com/gookit/goutil/fmtutil`
```go
// source at fmtutil/fmtutil.go
func StringOrJSON(v any) ([]byte, error)
// source at fmtutil/format.go
func DataSize(size uint64) string
func SizeToString(size uint64) string
Expand All @@ -665,6 +672,19 @@ func IsFile(path string) bool
func IsAbsPath(aPath string) bool
func IsImageFile(path string) bool
func IsZipFile(filepath string) bool
func PathMatch(pattern, s string) bool
// source at fsutil/find.go
func SearchNameUp(dirPath, name string) string
func SearchNameUpx(dirPath, name string) (string, bool)
func WalkDir(dir string, fn fs.WalkDirFunc) error
func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)
func OnlyFindDir(_ string, ent fs.DirEntry) bool
func OnlyFindFile(_ string, ent fs.DirEntry) bool
func IncludeSuffix(ss ...string) FilterFunc
func ExcludeDotFile(_ string, ent fs.DirEntry) bool
func ExcludeSuffix(ss ...string) FilterFunc
func ApplyFilters(fPath string, ent fs.DirEntry, filters []FilterFunc) bool
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
// source at fsutil/fsutil.go
func OSTempFile(pattern string) (*os.File, error)
func TempFile(dir, pattern string) (*os.File, error)
Expand All @@ -675,6 +695,7 @@ func ReaderMimeType(r io.Reader) (mime string)
func JoinPaths(elem ...string) string
func SlashPath(path string) string
func UnixPath(path string) string
func ToAbsPath(p string) string
// source at fsutil/info.go
func Dir(fpath string) string
func PathName(fpath string) string
Expand All @@ -684,9 +705,8 @@ func Extname(fpath string) string
func Suffix(fpath string) string
func Expand(pathStr string) string
func ExpandPath(pathStr string) string
func ResolvePath(pathStr string) string
func SplitPath(pathStr string) (dir, name string)
func GlobWithFunc(pattern string, fn func(filePath string) error) (err error)
func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error)
// source at fsutil/info_nonwin.go
func Realpath(pathStr string) string
// source at fsutil/operate.go
Expand Down Expand Up @@ -941,7 +961,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 WriteByte(b byte)
func WriteBytes(bs []byte)
func WritelnBytes(bs []byte)
func WriteString(s string)
func Writeln(s string)
// source at stdio/writer.go
Expand Down Expand Up @@ -1054,6 +1076,7 @@ func HasPrefix(s string, prefix string) bool
func IsStartOf(s, prefix string) bool
func HasSuffix(s string, suffix string) bool
func IsEndOf(s, suffix string) bool
func HasOneSuffix(s string, suffixes []string) bool
func IsValidUtf8(s string) bool
func IsSpace(c byte) bool
func IsEmpty(s string) bool
Expand All @@ -1067,7 +1090,9 @@ func IsVersion(s string) bool
func Compare(s1, s2, op string) bool
func VersionCompare(v1, v2, op string) bool
func QuickMatch(pattern, s string) bool
func PathMatch(pattern, s string) bool
func GlobMatch(pattern, s string) bool
func MatchNodePath(pattern, s string, sep string) bool
// source at strutil/convert.go
func Quote(s string) string
func Unquote(s string) string
Expand Down Expand Up @@ -1296,6 +1321,7 @@ func UserDir(subPath string) string
func UserCacheDir(subPath string) string
func UserConfigDir(subPath string) string
func ExpandPath(path string) string
func ExpandHome(path string) string
// source at sysutil/user_nonwin.go
func ChangeUserByName(newUname string) (err error)
func ChangeUserUidGid(newUid int, newGid int) (err error)
Expand Down
2 changes: 1 addition & 1 deletion fsutil/fsutil_test.go
Expand Up @@ -27,7 +27,7 @@ func TestMimeType(t *testing.T) {
buf.Reset()

assert.True(t, fsutil.IsImageFile("testdata/test.jpg"))
assert.True(t, fsutil.IsImageFile("testdata/not-exists"))
assert.False(t, fsutil.IsImageFile("testdata/not-exists"))
}

func TestTempDir(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions netutil/httpreq/util.go
Expand Up @@ -156,6 +156,18 @@ func ToRequestBody(data any) io.Reader {
return reqBody
}

// HeaderToString convert http Header to string
func HeaderToString(h http.Header) string {
var sb strings.Builder
for key, values := range h {
sb.WriteString(key)
sb.WriteString(": ")
sb.WriteString(strings.Join(values, ";"))
sb.WriteByte('\n')
}
return sb.String()
}

// RequestToString convert http Request to string
func RequestToString(r *http.Request) string {
buf := &bytes.Buffer{}
Expand Down
37 changes: 36 additions & 1 deletion sysutil/cmdr/runner.go
Expand Up @@ -2,13 +2,15 @@ package cmdr

import (
"fmt"
"strings"

"github.com/gookit/color"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/cliutil/cmdline"
"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/mathutil"
"github.com/gookit/goutil/strutil/textutil"
)

// Task struct
Expand Down Expand Up @@ -46,6 +48,24 @@ func (t *Task) ensureID(idx int) {
t.ID = id
}

var rpl = textutil.NewVarReplacer("$").DisableFlatten()

// RunWith command
func (t *Task) RunWith(ctx maputil.Data) error {
cmdVars := ctx.StringMap("cmdVars")

if len(cmdVars) > 0 {
// rpl := strutil.NewReplacer(cmdVars)
for i, val := range t.Cmd.Args {
if strings.ContainsRune(val, '$') {
t.Cmd.Args[i] = rpl.RenderSimple(val, cmdVars)
}
}
}

return t.Run()
}

// Run command
func (t *Task) Run() error {
if t.BeforeRun != nil {
Expand Down Expand Up @@ -76,6 +96,9 @@ func (t *Task) IsSuccess() bool {
return t.err == nil
}

// RunnerHookFn func
type RunnerHookFn func(r *Runner, t *Task) bool

// Runner use for batch run multi task commands
type Runner struct {
prev *Task
Expand Down Expand Up @@ -116,12 +139,19 @@ func NewRunner(fns ...func(rr *Runner)) *Runner {
Params: make(maputil.Map),
}

rr.OutToStd = true
for _, fn := range fns {
fn(rr)
}
return rr
}

// WithOutToStd set
func (r *Runner) WithOutToStd() *Runner {
r.OutToStd = true
return r
}

// Add multitask at once
func (r *Runner) Add(tasks ...*Task) *Runner {
for _, task := range tasks {
Expand Down Expand Up @@ -212,6 +242,11 @@ func (r *Runner) Run() error {
return r.Errs
}

// StepRun one command
func (r *Runner) StepRun() error {
return nil // TODO
}

// RunTask command
func (r *Runner) RunTask(task *Task) (goon bool) {
if len(r.EnvMap) > 0 {
Expand All @@ -228,7 +263,7 @@ func (r *Runner) RunTask(task *Task) (goon bool) {
}

// do running
if err := task.Run(); err != nil {
if err := task.RunWith(r.Params); err != nil {
r.Errs[task.ID] = err
color.Errorf("Task#%d run error: %s\n", task.Index()+1, err)

Expand Down

0 comments on commit c72adcb

Please sign in to comment.