diff --git a/README.md b/README.md index 8958ca0..292e3e0 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.2.0-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.2.1-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -535,7 +535,7 @@ func GetOsEnv(key string) string //gets the value of the environment variable na func SetOsEnv(key, value string) error //sets the value of the environment variable named by the key. func RemoveOsEnv(key string) error //remove a single environment variable. func CompareOsEnv(key, comparedEnv string) bool //gets env named by the key and compare it with comparedEnv -func ExecCommand(command string) (err error, stdout, stderr string) //use shell /bin/bash -c to execute command +func ExecCommand(command string) (stdout, stderr string, err error) //use shell /bin/bash -c to execute command ``` ### 13. validator is for data validation diff --git a/README_zh-CN.md b/README_zh-CN.md index 9e9d9e6..d8eb16b 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -4,7 +4,7 @@

![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.2.0-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.2.1-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -536,7 +536,7 @@ func GetOsEnv(key string) string //获取名称为key的环境变量 func SetOsEnv(key, value string) error //设置环境变量 func RemoveOsEnv(key string) error //删除指定key的环境变量 func CompareOsEnv(key, comparedEnv string) bool //获取名称为key的环境变量并和comparedEnv比较 -func ExecCommand(command string) (err error, stdout, stderr string) //执行shell命令(/bin/bash) +func ExecCommand(command string) (stdout, stderr string, err error) //执行shell命令(/bin/bash) ``` ### 13. validator验证器包 diff --git a/cryptor/basic.go b/cryptor/basic.go index 16942c4..c4df12d 100644 --- a/cryptor/basic.go +++ b/cryptor/basic.go @@ -6,6 +6,7 @@ package cryptor import ( + "bufio" "crypto/hmac" "crypto/md5" "crypto/sha1" @@ -13,7 +14,9 @@ import ( "crypto/sha512" "encoding/base64" "encoding/hex" - "io/ioutil" + "fmt" + "io" + "os" ) // Base64StdEncode encode string with base64 encoding @@ -36,14 +39,34 @@ func Md5String(s string) string { // Md5File return the md5 value of file func Md5File(filename string) (string, error) { - f, err := ioutil.ReadFile(filename) + if fileInfo, err := os.Stat(filename); err != nil { + return "", err + } else if fileInfo.IsDir() { + return "", nil + } + + file, err := os.Open(filename) if err != nil { return "", err } + defer file.Close() - h := md5.New() - h.Write(f) - return hex.EncodeToString(h.Sum(nil)), nil + hash := md5.New() + + chunkSize := 65536 + for buf, reader := make([]byte, chunkSize), bufio.NewReader(file); ; { + n, err := reader.Read(buf) + if err != nil { + if err == io.EOF { + break + } + return "", err + } + hash.Write(buf[:n]) + } + + checksum := fmt.Sprintf("%x", hash.Sum(nil)) + return checksum, nil } // HmacMd5 return the hmac hash of string use md5 diff --git a/retry/retry.go b/retry/retry.go index f58c4fe..59bbdfd 100644 --- a/retry/retry.go +++ b/retry/retry.go @@ -15,7 +15,9 @@ import ( ) const ( - DefaultRetryTimes = 5 + // DefaultRetryTimes times of retry + DefaultRetryTimes = 5 + // DefaultRetryDuration time duration of two retries DefaultRetryDuration = time.Second * 3 ) @@ -26,7 +28,7 @@ type RetryConfig struct { retryDuration time.Duration } -// RetryFn is function that retry executes +// RetryFunc is function that retry executes type RetryFunc func() error // Option is for adding retry config diff --git a/system/os.go b/system/os.go index 5e5d23b..f9a290e 100644 --- a/system/os.go +++ b/system/os.go @@ -51,7 +51,7 @@ func CompareOsEnv(key, comparedEnv string) bool { } // ExecCommand use shell /bin/bash -c to execute command -func ExecCommand(command string) (err error, stdout, stderr string) { +func ExecCommand(command string) (stdout, stderr string, err error) { var out bytes.Buffer var errout bytes.Buffer diff --git a/system/os_test.go b/system/os_test.go index 4126d70..6e04008 100644 --- a/system/os_test.go +++ b/system/os_test.go @@ -23,10 +23,12 @@ func TestOsEnvOperation(t *testing.T) { func TestExecCommand(t *testing.T) { assert := internal.NewAssert(t, "TestExecCommand") - err, out, errout := ExecCommand("ls") + out, errout, err := ExecCommand("ls") + t.Log("std out: ", out) + t.Log("std err: ", errout) assert.IsNil(err) - err, out, errout = ExecCommand("abc") + out, errout, err = ExecCommand("abc") t.Log("std out: ", out) t.Log("std err: ", errout) if err != nil { diff --git a/validator/validator.go b/validator/validator.go index bd1e1b8..bb1b8bd 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -67,7 +67,7 @@ func ContainLetter(str string) bool { return containLetterRegexMatcher.MatchString(str) } -// Is checks if the string is valid JSON +// IsJSON checks if the string is valid JSON func IsJSON(str string) bool { var js json.RawMessage return json.Unmarshal([]byte(str), &js) == nil