diff --git a/cliutil/cmdline/cmdline.go b/cliutil/cmdline/cmdline.go new file mode 100644 index 000000000..6dbebc50d --- /dev/null +++ b/cliutil/cmdline/cmdline.go @@ -0,0 +1,2 @@ +// Package cmdline provide quick build and parse cmd line string. +package cmdline diff --git a/fmtutil/fmtutil.go b/fmtutil/fmtutil.go new file mode 100644 index 000000000..fcfd27ddd --- /dev/null +++ b/fmtutil/fmtutil.go @@ -0,0 +1,2 @@ +// Package fmtutil provide some format util functions. +package fmtutil diff --git a/fsutil/finder/finder.go b/fsutil/finder/finder.go index e30363a8b..ceabf074c 100644 --- a/fsutil/finder/finder.go +++ b/fsutil/finder/finder.go @@ -1,3 +1,4 @@ +// Package finder provide a finder tool for find files package finder import ( diff --git a/fsutil/fsutil.go b/fsutil/fsutil.go index fe8781f79..36ac591b3 100644 --- a/fsutil/fsutil.go +++ b/fsutil/fsutil.go @@ -1,3 +1,4 @@ +// Package fsutil Filesystem util functions, quick create, read and write file. eg: file and dir check, operate package fsutil import ( diff --git a/fsutil/info.go b/fsutil/info.go index 30f11292b..14e680664 100644 --- a/fsutil/info.go +++ b/fsutil/info.go @@ -70,13 +70,15 @@ func GlobWithFunc(pattern string, fn func(filePath string) error) (err error) { return } -// filter and handle func for FindInDir type ( + // FilterFunc type for FindInDir FilterFunc func(fPath string, fi os.FileInfo) bool + // HandleFunc type for FindInDir HandleFunc func(fPath string, fi os.FileInfo) error ) // FindInDir code refer the go pkg: path/filepath.glob() +// // filters: return false will skip the file. func FindInDir(dir string, handleFn HandleFunc, filters ...FilterFunc) (e error) { fi, err := os.Stat(dir) diff --git a/jsonutil/jsonutil.go b/jsonutil/jsonutil.go index efdae259a..11be66ed3 100644 --- a/jsonutil/jsonutil.go +++ b/jsonutil/jsonutil.go @@ -1,3 +1,4 @@ +// Package jsonutil provide some util functions for quick operate JSON data package jsonutil import ( diff --git a/maputil/README.md b/maputil/README.md index 51623783f..e58055647 100644 --- a/maputil/README.md +++ b/maputil/README.md @@ -1,5 +1,7 @@ # Map Utils +`maputil` provide map data util functions. eg: convert, sub-value get, simple merge + - use `map[string]any` as Data - deep get value by key path - deep set value by key path diff --git a/maputil/maputil.go b/maputil/maputil.go index 7992a7e50..e661f06d9 100644 --- a/maputil/maputil.go +++ b/maputil/maputil.go @@ -1,3 +1,4 @@ +// Package maputil provide map data util functions. eg: convert, sub-value get, simple merge package maputil import ( diff --git a/netutil/netutil.go b/netutil/netutil.go index 437c4a12a..33db7abe9 100644 --- a/netutil/netutil.go +++ b/netutil/netutil.go @@ -1,11 +1,13 @@ +// Package netutil provide some network util functions. package netutil import ( "net" + "net/netip" ) -// InternalIP get internal IP -func InternalIP() (ip string) { +// InternalIPOld get internal IP buy old logic +func InternalIPOld() (ip string) { addrs, err := net.InterfaceAddrs() if err != nil { panic("Oops: " + err.Error()) @@ -22,3 +24,38 @@ func InternalIP() (ip string) { } return } + +// InternalIP get internal IP +func InternalIP() (ip string) { + addr := netip.IPv4Unspecified() + if addr.IsValid() { + return addr.String() + } + + addr = netip.IPv6Unspecified() + if addr.IsValid() { + return addr.String() + } + + return "" +} + +// InternalIPv4 get internal IPv4 +func InternalIPv4() (ip string) { + addr := netip.IPv4Unspecified() + + if addr.IsValid() { + return addr.String() + } + return "" +} + +// InternalIPv6 get internal IPv6 +func InternalIPv6() (ip string) { + addr := netip.IPv6Unspecified() + + if addr.IsValid() { + return addr.String() + } + return "" +} diff --git a/netutil/netutil_go118.go b/netutil/netutil_go118.go deleted file mode 100644 index 8ffcc54e3..000000000 --- a/netutil/netutil_go118.go +++ /dev/null @@ -1,41 +0,0 @@ -//go:build go1.18 -// +build go1.18 - -package netutil - -import "net/netip" - -// InternalIP get internal IP -// func InternalIP() (ip string) { -// addr := netip.IPv4Unspecified() -// if addr.IsValid() { -// return addr.String() -// } -// -// addr = netip.IPv6Unspecified() -// if addr.IsValid() { -// return addr.String() -// } -// -// return "" -// } - -// InternalIPv4 get internal IPv4 -func InternalIPv4() (ip string) { - addr := netip.IPv4Unspecified() - - if addr.IsValid() { - return addr.String() - } - return "" -} - -// InternalIPv6 get internal IPv6 -func InternalIPv6() (ip string) { - addr := netip.IPv6Unspecified() - - if addr.IsValid() { - return addr.String() - } - return "" -} diff --git a/netutil/netutil_ne118.go b/netutil/netutil_ne118.go deleted file mode 100644 index 44deb0cf0..000000000 --- a/netutil/netutil_ne118.go +++ /dev/null @@ -1,4 +0,0 @@ -//go:build !go1.18 -// +build !go1.18 - -package netutil diff --git a/reflects/README.md b/reflects/README.md index 41cf4b55e..84269fe60 100644 --- a/reflects/README.md +++ b/reflects/README.md @@ -1,5 +1,7 @@ # Reflects +`reflects` Provide extends reflect util functions. + - some features ## Install diff --git a/reflects/reflects.go b/reflects/reflects.go new file mode 100644 index 000000000..2cbdae9cd --- /dev/null +++ b/reflects/reflects.go @@ -0,0 +1,2 @@ +// Package reflects Provide extends reflect util functions. +package reflects diff --git a/stdio/README.md b/stdio/README.md new file mode 100644 index 000000000..5a8370c68 --- /dev/null +++ b/stdio/README.md @@ -0,0 +1,29 @@ +# Stdio + +`stdio` provide some standard IO util functions. + +## Install + +```bash +go get github.com/gookit/goutil/stdio +``` + +## Go docs + +- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/stdio) + +## Usage + +Please see tests. + +## Testings + +```shell +go test -v ./stdio/... +``` + +Test limit by regexp: + +```shell +go test -v -run ^TestSetByKeys ./stdio/... +``` diff --git a/stdio/stdio.go b/stdio/stdio.go new file mode 100644 index 000000000..8a0146e8c --- /dev/null +++ b/stdio/stdio.go @@ -0,0 +1,2 @@ +// Package stdio provide some standard IO util functions. +package stdio diff --git a/stdio/writer.go b/stdio/writer.go index ba43b0ebc..76070cbb1 100644 --- a/stdio/writer.go +++ b/stdio/writer.go @@ -5,7 +5,7 @@ import ( "io" ) -// WriteWrapper struct +// WriteWrapper warp io.Writer support more operate methods. type WriteWrapper struct { Out io.Writer } diff --git a/testutil/README.md b/testutil/README.md index 002f37521..37dc07c07 100644 --- a/testutil/README.md +++ b/testutil/README.md @@ -1,5 +1,7 @@ # Test Utils +`testutil` provide some test help util functions. eg: http test, mock ENV value + - `assert` go tests helper - env variable mocks - http request mock @@ -7,7 +9,7 @@ ## Install ```bash -go get github.com/gookit/goutil/sysutil +go get github.com/gookit/goutil/testutil ``` ## [`assert`](./assert) tests diff --git a/testutil/testutil.go b/testutil/testutil.go index 0d78f90bc..b6650f658 100644 --- a/testutil/testutil.go +++ b/testutil/testutil.go @@ -1,3 +1,4 @@ +// Package testutil provide some test help util functions. eg: http test, mock ENV value package testutil import ( diff --git a/testutil/writer.go b/testutil/writer.go index b597b06f0..1fa8cf857 100644 --- a/testutil/writer.go +++ b/testutil/writer.go @@ -21,18 +21,21 @@ func NewTestWriter() *TestWriter { } // SetErrOnWrite method -func (w *TestWriter) SetErrOnWrite() { +func (w *TestWriter) SetErrOnWrite() *TestWriter { w.ErrOnWrite = true + return w } // SetErrOnFlush method -func (w *TestWriter) SetErrOnFlush() { +func (w *TestWriter) SetErrOnFlush() *TestWriter { w.ErrOnFlush = true + return w } // SetErrOnClose method -func (w *TestWriter) SetErrOnClose() { +func (w *TestWriter) SetErrOnClose() *TestWriter { w.ErrOnClose = true + return w } // Flush implements