Skip to content

Commit

Permalink
feat: 支持配置不支持的文件名正则 #57
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl authored Aug 8, 2023
1 parent 70550ec commit 915d731
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion analysis-tool-sdk-golang/framework/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ func doAnalyze(executor Executor, arguments *object.Arguments) {
os.Exit(0)
}
file, err := client.GenerateInputFile(arguments.GetDownloaderClient())
defer file.Close()
if err != nil {
client.Failed(errors.New("Generate input file failed: " + err.Error()))
os.Exit(1)
}
// 返回的file为nil时表示文件被忽略,直接返回
if file == nil {
util.Info("Unsupported filename: %s", input.FileUrls[0].Name)
client.Finish(object.NewOutput(object.StatusSuccess, new(object.Result)))
return
}
defer file.Close()
util.Info("generate input file success")
output, err := executor.Execute(&input.ToolConfig, file)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions analysis-tool-sdk-golang/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)

const ArgKeyDownloaderWorkerCount = "downloaderWorker"
const ArgKeyDownloaderWorkerHeaders = "downloaderHeaders"
const ArgKeyUnsupportedFileNameRegex = "unsupportedFileNameRegex"
const ArgKeyPkgType = "packageType"
const PackageTypeDocker = "DOCKER"
const WorkDir = "/bkrepo/workspace"
Expand Down Expand Up @@ -58,6 +60,11 @@ func GenerateInputFile(toolInput *object.ToolInput, downloader Downloader) (*os.
return generateImageTar(toolInput, downloader)
} else {
fileUrl := toolInput.FileUrls[0]
fileNameRegex := toolInput.ToolConfig.GetStringArg(ArgKeyUnsupportedFileNameRegex)
if matched, err := matchUnsupportedFileNameRegex(fileNameRegex, fileUrl.Name); matched || err != nil {
// 不支持的文件类型直接返回
return nil, err
}
file, err := os.Create(filepath.Join(WorkDir, fileUrl.Name))
if err != nil {
return nil, err
Expand Down Expand Up @@ -149,6 +156,13 @@ func Extract(reader io.Reader, dstDir string, perm fs.FileMode) error {
return nil
}

func matchUnsupportedFileNameRegex(regex string, fileName string) (bool, error) {
if len(regex) == 0 || len(fileName) == 0 {
return false, nil
}
return regexp.MatchString(regex, fileName)
}

func generateImageTar(toolInput *object.ToolInput, downloader Downloader) (*os.File, error) {
// 获取manifest
manifest, err := loadManifest(&toolInput.FileUrls[0], downloader)
Expand Down

0 comments on commit 915d731

Please sign in to comment.