1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

Merge branch 'main' into v2

This commit is contained in:
dudaodong
2022-01-19 17:35:48 +08:00
7 changed files with 42 additions and 15 deletions

View File

@@ -4,7 +4,7 @@
</p>
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-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

View File

@@ -4,7 +4,7 @@
</p>
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-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验证器包

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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 {

View File

@@ -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