Skip to content

Commit

Permalink
style: format all sub pkg codes by go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 9, 2022
1 parent 3a623fd commit 3f1a328
Show file tree
Hide file tree
Showing 23 changed files with 143 additions and 124 deletions.
8 changes: 5 additions & 3 deletions cliutil/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func ReadInput(question string) (string, error) {
// ReadLine read one line from user input.
//
// Usage:
// in := cliutil.ReadLine("")
// ans, _ := cliutil.ReadLine("your name?")
//
// in := cliutil.ReadLine("")
// ans, _ := cliutil.ReadLine("your name?")
func ReadLine(question string) (string, error) {
if len(question) > 0 {
color.Print(question)
Expand All @@ -47,7 +48,8 @@ func ReadFirst(question string) (string, error) {
// ReadFirstByte read first byte char
//
// Usage:
// ans, _ := cliutil.ReadFirstByte("continue?[y/n] ")
//
// ans, _ := cliutil.ReadFirstByte("continue?[y/n] ")
func ReadFirstByte(question string) (byte, error) {
if len(question) > 0 {
color.Print(question)
Expand Down
2 changes: 1 addition & 1 deletion cliutil/read_nonwin.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !windows
//go:build !windows

package cliutil

Expand Down
6 changes: 4 additions & 2 deletions errorx/errorx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type XErrorFace interface {
// ErrorX struct
//
// TIPS:
// fmt pkg call order: Format > GoString > Error > String
//
// fmt pkg call order: Format > GoString > Error > String
type ErrorX struct {
// trace stack
*stack
Expand Down Expand Up @@ -181,7 +182,8 @@ func (e *ErrorX) CallerFunc() *Func {
// Location information for the caller func. more please see CallerFunc
//
// Returns eg:
// github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34
//
// github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34
func (e *ErrorX) Location() string {
if e.stack == nil {
return "unknown"
Expand Down
8 changes: 5 additions & 3 deletions errorx/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func (f *Func) FileLine() (file string, line int) {
// Location simple location info for the func
//
// Returns eg:
// github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34
//
// github.com/gookit/goutil/errorx_test.TestWithPrev(), errorx_test.go:34
func (f *Func) Location() string {
file, line := f.FileLine()

Expand All @@ -117,8 +118,9 @@ func (f *Func) Location() string {
// String of the func
//
// Returns eg:
// github.com/gookit/goutil/errorx_test.TestWithPrev()
// At /path/to/github.com/gookit/goutil/errorx_test.go:34
//
// github.com/gookit/goutil/errorx_test.TestWithPrev()
// At /path/to/github.com/gookit/goutil/errorx_test.go:34
func (f *Func) String() string {
file, line := f.FileLine()

Expand Down
7 changes: 4 additions & 3 deletions errorx/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ func Is(err, target error) bool {
// NOTICE: target must be ptr and not nil. alias of errors.As()
//
// Usage:
//
// var ex *errorx.ErrorX
// err := doSomething()
// if errorx.To(err, &ex) {
// fmt.Println(ex.GoString())
// }
// if errorx.To(err, &ex) {
// fmt.Println(ex.GoString())
// }
func To(err error, target interface{}) bool {
return errors.As(err, target)
}
Expand Down
7 changes: 4 additions & 3 deletions fmtutil/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const (
// DataSize format bytes number friendly.
//
// Usage:
// file, err := os.Open(path)
// fl, err := file.Stat()
// fmtSize := DataSize(fl.Size())
//
// file, err := os.Open(path)
// fl, err := file.Stat()
// fmtSize := DataSize(fl.Size())
func DataSize(size uint64) string {
switch {
case size < 1024:
Expand Down
6 changes: 5 additions & 1 deletion fsutil/finder/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (fn FilterFunc) Filter(filePath, filename string) bool {
// ExtFilterFunc filter filepath by given file ext.
//
// Usage:
//
// f := EmptyFinder()
// f.AddFilter(ExtFilterFunc([]string{".go", ".md"}, true))
// f.AddFilter(ExtFilterFunc([]string{".log", ".tmp"}, false))
Expand All @@ -80,6 +81,7 @@ func ExtFilterFunc(exts []string, include bool) FileFilterFunc {
// SuffixFilterFunc filter filepath by given file ext.
//
// Usage:
//
// f := EmptyFinder()
// f.AddFilter(SuffixFilterFunc([]string{"util.go", ".md"}, true))
// f.AddFilter(SuffixFilterFunc([]string{"_test.go", ".log"}, false))
Expand Down Expand Up @@ -144,6 +146,7 @@ func ModTimeFilterFunc(limitSec int, op rune, include bool) FileFilterFunc {
// GlobFilterFunc filter filepath by given patterns.
//
// Usage:
//
// f := EmptyFiler()
// f.AddFilter(GlobFilterFunc([]string{"*_test.go"}, true))
func GlobFilterFunc(patterns []string, include bool) FileFilterFunc {
Expand All @@ -160,9 +163,10 @@ func GlobFilterFunc(patterns []string, include bool) FileFilterFunc {
// RegexFilterFunc filter filepath by given regex pattern
//
// Usage:
//
// f := EmptyFiler()
// f.AddFilter(RegexFilterFunc(`[A-Z]\w+`, true))
func RegexFilterFunc(pattern string, include bool) FileFilterFunc {
func RegexFilterFunc(pattern string, _ bool) FileFilterFunc {
reg := regexp.MustCompile(pattern)

return func(_, filename string) bool {
Expand Down
23 changes: 14 additions & 9 deletions fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,36 @@ const (
// OSTempFile create a temp file on os.TempDir()
//
// Usage:
// fsutil.OSTempFile("example.*.txt")
//
// fsutil.OSTempFile("example.*.txt")
func OSTempFile(pattern string) (*os.File, error) {
return ioutil.TempFile(os.TempDir(), pattern)
}

// TempFile is like ioutil.TempFile, but can custom temp dir.
//
// Usage:
// fsutil.TempFile("", "example.*.txt")
//
// fsutil.TempFile("", "example.*.txt")
func TempFile(dir, pattern string) (*os.File, error) {
return ioutil.TempFile(dir, pattern)
}

// OSTempDir creates a new temp dir on os.TempDir and return the temp dir path
//
// Usage:
// fsutil.OSTempDir("example.*")
//
// fsutil.OSTempDir("example.*")
func OSTempDir(pattern string) (string, error) {
return ioutil.TempDir(os.TempDir(), pattern)
}

// TempDir creates a new temp dir and return the temp dir path
//
// Usage:
// fsutil.TempDir("", "example.*")
// fsutil.TempDir("testdata", "example.*")
//
// fsutil.TempDir("", "example.*")
// fsutil.TempDir("testdata", "example.*")
func TempDir(dir, pattern string) (string, error) {
return ioutil.TempDir(dir, pattern)
}
Expand All @@ -58,10 +62,11 @@ func MimeType(path string) (mime string) {
// ReaderMimeType get the io.Reader mimeType
//
// Usage:
// file, err := os.Open(filepath)
// if err != nil {
// return
// }
//
// file, err := os.Open(filepath)
// if err != nil {
// return
// }
// mime := ReaderMimeType(file)
func ReaderMimeType(r io.Reader) (mime string) {
var buf [MimeSniffLen]byte
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ require (
github.com/mattn/go-isatty v0.0.16
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.4.0
)

require (
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
)
26 changes: 0 additions & 26 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,16 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
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=
Expand Down
12 changes: 7 additions & 5 deletions mathutil/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
// RandomInt return a random int at the [min, max)
//
// Usage:
// RandomInt(10, 99)
// RandomInt(100, 999)
// RandomInt(1000, 9999)
//
// RandomInt(10, 99)
// RandomInt(100, 999)
// RandomInt(1000, 9999)
func RandomInt(min, max int) int {
rand.Seed(time.Now().UnixNano())
return min + rand.Intn(max-min)
Expand All @@ -27,8 +28,9 @@ func RandIntWithSeed(min, max int, seed int64) int {
// RandomIntWithSeed return a random int at the [min, max)
//
// Usage:
// seed := time.Now().UnixNano()
// RandomIntWithSeed(1000, 9999, seed)
//
// 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)
Expand Down
21 changes: 12 additions & 9 deletions stdutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ func IsEqual(src, dst interface{}) bool {
// alias of the IsContains
//
// TIP: only support types: string, map, array, slice
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
func Contains(data, elem interface{}) bool {
_, found := CheckContains(data, elem)
return found
Expand All @@ -61,9 +62,10 @@ func Contains(data, elem interface{}) bool {
// IsContains try loop over the data check if the data includes the element.
//
// TIP: only support types: string, map, array, slice
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
func IsContains(data, elem interface{}) bool {
_, found := CheckContains(data, elem)
return found
Expand All @@ -72,9 +74,10 @@ func IsContains(data, elem interface{}) bool {
// CheckContains try loop over the data check if the data includes the element.
//
// TIP: only support types: string, map, array, slice
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// map - check key exists
// string - check sub-string exists
// array,slice - check sub-element exists
//
// return (false, false) if impossible.
// return (true, false) if element was not found.
Expand Down
4 changes: 3 additions & 1 deletion stdutil/gofunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func (ffn *FullFcName) String() string {
// FuncName get full func name, contains pkg path.
//
// eg:
// // OUTPUT: github.com/gookit/goutil/stdutil.PanicIf
//
// // OUTPUT: github.com/gookit/goutil/stdutil.PanicIf
// stdutil.FuncName(stdutil.PkgName)
func FuncName(fn interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
Expand All @@ -73,6 +74,7 @@ func CutFuncName(fullFcName string) (pkgPath, shortFnName string) {
// PkgName get current package name
//
// Usage:
//
// fullFcName := stdutil.FuncName(fn)
// pgkName := stdutil.PkgName(fullFcName)
func PkgName(fullFcName string) string {
Expand Down
1 change: 1 addition & 0 deletions stdutil/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func GetCallStacks(all bool) []byte {
// GetCallerInfo get caller func name and with base filename and line.
//
// returns:
//
// github.com/gookit/goutil/stdutil_test.someFunc2(),stack_test.go:26
func GetCallerInfo(skip int) string {
skip++ // ignore current func
Expand Down
3 changes: 2 additions & 1 deletion strutil/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
)

// global id:
//
// https://github.com/rs/xid
// https://github.com/satori/go.uuid
// https://github.com/satori/go.uuid
var (
DefMinInt = 1000
DefMaxInt = 9999
Expand Down
13 changes: 7 additions & 6 deletions strutil/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ func RandomBytes(length int) ([]byte, error) {

// RandomString generate.
// Example:
// // this will give us a 44 byte, base64 encoded output
// token, err := RandomString(32)
// if err != nil {
// // Serve an appropriately vague error to the
// // user, but log the details internally.
// }
//
// // this will give us a 44 byte, base64 encoded output
// token, err := RandomString(32)
// if err != nil {
// // Serve an appropriately vague error to the
// // user, but log the details internally.
// }
func RandomString(length int) (string, error) {
b, err := RandomBytes(length)
return base64.URLEncoding.EncodeToString(b), err
Expand Down

0 comments on commit 3f1a328

Please sign in to comment.