Merge branch 'main' of https://git.apinb.com/bsm-sdk/core
This commit is contained in:
commit
1005e89e4f
14
utils/ext.go
14
utils/ext.go
|
@ -1,6 +1,10 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -12,7 +16,7 @@ func If(condition bool, trueValue, falseValue interface{}) interface{} {
|
||||||
return falseValue
|
return falseValue
|
||||||
}
|
}
|
||||||
|
|
||||||
//如果首字母是小写字母, 则变换为大写字母
|
// 如果首字母是小写字母, 则变换为大写字母
|
||||||
func FirstToUpper(str string) string {
|
func FirstToUpper(str string) string {
|
||||||
if str == "" {
|
if str == "" {
|
||||||
return ""
|
return ""
|
||||||
|
@ -33,3 +37,11 @@ func ParseParams(in map[string]string) map[string]interface{} {
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PrintJson(v any) {
|
||||||
|
jsonBy, _ := json.Marshal(v)
|
||||||
|
var out bytes.Buffer
|
||||||
|
json.Indent(&out, jsonBy, "", "\t")
|
||||||
|
out.WriteTo(os.Stdout)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -21,11 +22,37 @@ func StringToFile(path, content string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件复制
|
||||||
|
func CopyFile(src, dst string) (int64, error) {
|
||||||
|
sourceFileStat, err := os.Stat(src)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if !sourceFileStat.Mode().IsRegular() {
|
||||||
|
return 0, fmt.Errorf("%s is not a regular file", src)
|
||||||
|
}
|
||||||
|
|
||||||
|
source, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer source.Close()
|
||||||
|
|
||||||
|
destination, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer destination.Close()
|
||||||
|
|
||||||
|
nBytes, err := io.Copy(destination, source)
|
||||||
|
return nBytes, err
|
||||||
|
}
|
||||||
|
|
||||||
// 递归遍历文件夹
|
// 递归遍历文件夹
|
||||||
// rootDir: 文件夹根目录
|
// rootDir: 文件夹根目录
|
||||||
// s: 存储文件名的切片
|
// s: 存储文件名的切片
|
||||||
// filter: 过滤条件:".git", ".idea", ".vscode", ".gitignore", ".gitea", ".github", ".golangci.yml", "*.pyc"
|
// filter: 过滤条件:".git", ".idea", ".vscode", ".gitignore", ".gitea", ".github", ".golangci.yml", "*.pyc"
|
||||||
func FileTree(rootDir string, s []string, filter []string) ([]string, error) {
|
func FileTreeByFilter(rootDir string, s []string, filter []string) ([]string, error) {
|
||||||
rd, err := os.ReadDir(rootDir)
|
rd, err := os.ReadDir(rootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s, err
|
return s, err
|
||||||
|
@ -50,7 +77,48 @@ func FileTree(rootDir string, s []string, filter []string) ([]string, error) {
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
fullDir := rootDir + "/" + fi.Name()
|
fullDir := rootDir + "/" + fi.Name()
|
||||||
s, err = FileTree(fullDir, s, filter)
|
s, err = FileTreeByFilter(fullDir, s, filter)
|
||||||
|
if err != nil {
|
||||||
|
return s, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fullName := rootDir + "/" + fi.Name()
|
||||||
|
s = append(s, fullName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 递归遍历文件夹
|
||||||
|
// rootDir: 文件夹根目录
|
||||||
|
// s: 存储文件名的切片
|
||||||
|
// allow: 允许条件:".zip", ".check"
|
||||||
|
func FileTreeBySelect(rootDir string, s []string, allow []string) ([]string, error) {
|
||||||
|
rd, err := os.ReadDir(rootDir)
|
||||||
|
if err != nil {
|
||||||
|
return s, err
|
||||||
|
}
|
||||||
|
for _, fi := range rd {
|
||||||
|
// 检查文件名是否匹配任何一个过滤模式
|
||||||
|
matched := false
|
||||||
|
for _, item := range allow {
|
||||||
|
exists, err := filepath.Match(item, fi.Name())
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if exists {
|
||||||
|
matched = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !matched {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if fi.IsDir() {
|
||||||
|
fullDir := rootDir + "/" + fi.Name()
|
||||||
|
s, err = FileTreeBySelect(fullDir, s, allow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue