支持通过空格拆分多文件(shell数组)

支持files参数的值为在shell中得到的多文件列表(空格拼接)
This commit is contained in:
kerwin612 2023-07-12 01:07:24 +00:00
parent 23a38ad334
commit f687e7dda9

38
main.go
View File

@ -8,6 +8,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"regexp"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
gha "github.com/sethvargo/go-githubactions" gha "github.com/sethvargo/go-githubactions"
@ -107,24 +108,31 @@ func getDirFiles(dir string) ([]string, error) {
func getFiles(parentDir, files string) ([]string, error) { func getFiles(parentDir, files string) ([]string, error) {
var fileList []string var fileList []string
lines := strings.Split(files, "\n") fmt.Printf("the value of the files is: %s\n", files)
for _, line := range lines { reg := regexp.MustCompile(`[^\s"']+|"([^"]*)"|'([^']*)'`)
line = strings.Trim(line, "'") arr := reg.FindAllString(files, -1)
line = strings.Trim(line, `"`) fmt.Printf("the value after splitting by spaces is: %s\n", strings.Join(arr, ", "))
if filepath.IsAbs(line) { for _, str := range arr {
return nil, fmt.Errorf("file path %s is absolute", line) lines := strings.Split(str, "\n")
} fmt.Printf("the value after splitting %s by \\n is: %s\n", str, strings.Join(lines, ", "))
line = filepath.Join(parentDir, line) for _, line := range lines {
matches, err := filepath.Glob(line) line = strings.Trim(line, "'")
if err != nil { line = strings.Trim(line, `"`)
return nil, err if filepath.IsAbs(line) {
} return nil, fmt.Errorf("file path %s is absolute", line)
for _, match := range matches { }
files, err := getDirFiles(match) line = filepath.Join(parentDir, line)
matches, err := filepath.Glob(line)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fileList = append(fileList, files...) for _, match := range matches {
files, err := getDirFiles(match)
if err != nil {
return nil, err
}
fileList = append(fileList, files...)
}
} }
} }
return fileList, nil return fileList, nil