From 073c77e75129601c71ef3b24795ce3cb973a4a4e Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 19 Sep 2023 17:44:49 +0800 Subject: [PATCH 1/3] refactor: remove unused code --- random/random.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/random/random.go b/random/random.go index 0ba4fcd..db74924 100644 --- a/random/random.go +++ b/random/random.go @@ -29,14 +29,11 @@ func RandInt(min, max int) int { if min == max { return min } + if max < min { min, max = max, min } - // fix: https://github.com/duke-git/lancet/issues/75 - // r := rand.New(rand.NewSource(time.Now().UnixNano())) - // return r.Intn(max-min) + min - return rand.Intn(max-min) + min } From 781b89d51a33baf2e9dc844fa0bf8da36ef01f7e Mon Sep 17 00:00:00 2001 From: o98k <63107263+o98k-ok@users.noreply.github.com> Date: Sat, 23 Sep 2023 13:45:02 +0800 Subject: [PATCH 2/3] feat(fileutil): add ReadFile func (#140) --- docs/api/packages/fileutil.md | 39 ++++++++++++++++++++++++++++++++ docs/en/api/packages/fileutil.md | 39 ++++++++++++++++++++++++++++++++ fileutil/file.go | 23 +++++++++++++++++++ fileutil/file_example_test.go | 18 +++++++++++++++ fileutil/file_test.go | 19 ++++++++++++++++ 5 files changed, 138 insertions(+) diff --git a/docs/api/packages/fileutil.md b/docs/api/packages/fileutil.md index bb6d2d1..bc8d530 100644 --- a/docs/api/packages/fileutil.md +++ b/docs/api/packages/fileutil.md @@ -47,6 +47,7 @@ import ( - [WriteCsvFile](#WriteCsvFile) - [WriteStringToFile](#WriteStringToFile) - [WriteBytesToFile](#WriteBytesToFile) +- [ReadFile](#ReadFile)
@@ -839,3 +840,41 @@ func main() { // hello } ``` + +### ReadFile + +

读取文件或者URL

+ +函数签名: + +```go +func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + reader, fn, err := ReadFile("https://httpbin.org/robots.txt") + if err != nil { + return + } + defer fn() + + dat, err := io.ReadAll(reader) + if err != nil { + return + } + fmt.Println(string(dat)) + // Output: + // User-agent: * + // Disallow: /deny +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/fileutil.md b/docs/en/api/packages/fileutil.md index a4d4fd4..48fbb70 100644 --- a/docs/en/api/packages/fileutil.md +++ b/docs/en/api/packages/fileutil.md @@ -47,6 +47,7 @@ import ( - [WriteCsvFile](#WriteCsvFile) - [WriteStringToFile](#WriteStringToFile) - [WriteBytesToFile](#WriteBytesToFile) +- [ReadFile](#ReadFile)
@@ -839,3 +840,41 @@ func main() { // hello } ``` + +### ReadFile + +

Read File/URL

+ +Signature: + +```go +func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/fileutil" +) + +func main() { + reader, fn, err := ReadFile("https://httpbin.org/robots.txt") + if err != nil { + return + } + defer fn() + + dat, err := io.ReadAll(reader) + if err != nil { + return + } + fmt.Println(string(dat)) + // Output: + // User-agent: * + // Disallow: /deny +} +``` diff --git a/fileutil/file.go b/fileutil/file.go index 3a043dc..66cdd10 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -22,6 +22,8 @@ import ( "path/filepath" "runtime" "strings" + + "github.com/duke-git/lancet/v2/validator" ) // IsExist checks if a file or directory exists. @@ -624,3 +626,24 @@ func WriteBytesToFile(filepath string, content []byte) error { _, err = f.Write(content) return err } + +// ReadFile get file reader by a url or a local file +// Play: todo +func ReadFile(path string) (reader io.ReadCloser, closeFn func(), err error) { + switch { + case validator.IsUrl(path): + resp, err := http.Get(path) + if err != nil { + return nil, func() {}, err + } + return resp.Body, func() { resp.Body.Close() }, nil + case IsExist(path): + reader, err := os.Open(path) + if err != nil { + return nil, func() {}, err + } + return reader, func() { reader.Close() }, nil + default: + return nil, func() {}, errors.New("unknown file type") + } +} diff --git a/fileutil/file_example_test.go b/fileutil/file_example_test.go index 9bca78b..3f08528 100644 --- a/fileutil/file_example_test.go +++ b/fileutil/file_example_test.go @@ -2,6 +2,7 @@ package fileutil import ( "fmt" + "io" "os" ) @@ -385,3 +386,20 @@ func ExampleWriteBytesToFile() { // Output: // hello } + +func ExampleReadFile() { + reader, fn, err := ReadFile("https://httpbin.org/robots.txt") + if err != nil { + return + } + defer fn() + + dat, err := io.ReadAll(reader) + if err != nil { + return + } + fmt.Println(string(dat)) + // Output: + // User-agent: * + // Disallow: /deny +} diff --git a/fileutil/file_test.go b/fileutil/file_test.go index a4ae7a7..44d9784 100644 --- a/fileutil/file_test.go +++ b/fileutil/file_test.go @@ -1,6 +1,7 @@ package fileutil import ( + "io" "os" "testing" @@ -457,3 +458,21 @@ func TestWriteBytesToFile(t *testing.T) { os.Remove(filepath) } + +func TestReadFile(t *testing.T) { + reader, close, err := ReadFile("https://httpbin.org/robots.txt") + if err != nil { + t.Fail() + } + defer close() + + dat, err := io.ReadAll(reader) + if err != nil { + t.Fail() + } + + want := `User-agent: * +Disallow: /deny +` + internal.NewAssert(t, "TestReadFile").Equal(want, string(dat)) +} From a9f01d8a6919e265106167113b30d4802ddfe8cb Mon Sep 17 00:00:00 2001 From: flytutu <55188970+flytutu-susu@users.noreply.github.com> Date: Sat, 23 Sep 2023 13:45:21 +0800 Subject: [PATCH 3/3] [opt] currentPath support windows and linux (#139) Co-authored-by: hesu --- fileutil/file.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fileutil/file.go b/fileutil/file.go index 3a043dc..afd5da3 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -18,7 +18,6 @@ import ( "io/fs" "net/http" "os" - "path" "path/filepath" "runtime" "strings" @@ -491,7 +490,7 @@ func CurrentPath() string { var absPath string _, filename, _, ok := runtime.Caller(1) if ok { - absPath = path.Dir(filename) + absPath = filepath.Dir(filename) } return absPath