diff --git a/analysis-tool-sdk-golang/framework/executor.go b/analysis-tool-sdk-golang/framework/executor.go index cdf2cbc..04289a6 100644 --- a/analysis-tool-sdk-golang/framework/executor.go +++ b/analysis-tool-sdk-golang/framework/executor.go @@ -17,7 +17,17 @@ type Executor interface { // Analyze 执行分析 func Analyze(executor Executor) { - doAnalyze(executor, object.GetArgs()) + args := object.GetArgs() + for { + doAnalyze(executor, args) + if args.ShouldKeepRunning() { + if err := util.CleanWorkDir(); err != nil { + panic("clean work dir failed: " + err.Error()) + } + } else { + break + } + } } func doAnalyze(executor Executor, arguments *object.Arguments) { diff --git a/analysis-tool-sdk-golang/object/argument.go b/analysis-tool-sdk-golang/object/argument.go index ace0d5f..d30a91f 100644 --- a/analysis-tool-sdk-golang/object/argument.go +++ b/analysis-tool-sdk-golang/object/argument.go @@ -14,6 +14,7 @@ type Arguments struct { PullRetry int InputFilePath string OutputFilePath string + KeepRunning bool } var args *Arguments @@ -24,19 +25,21 @@ func newArguments() *Arguments { flag.StringVar(&args.Token, "token", "", "制品库临时令牌") flag.StringVar(&args.TaskId, "task-id", "", "扫描任务Id") flag.StringVar(&args.ExecutionCluster, "execution-cluster", "", "所在扫描执行集群名") - flag.IntVar(&args.PullRetry, "pull-retry", 3, "拉取模式下拉取任务的次数,-1表示一直拉取直到拉取到任务") + flag.IntVar(&args.PullRetry, "pull-retry", -1, "拉取模式下拉取任务的次数,-1表示一直拉取直到拉取到任务") + flag.BoolVar(&args.KeepRunning, "keep-running", true, "是否一直运行,仅在拉取任务模式下生效") flag.StringVar(&args.InputFilePath, "input", "", "输入文件路径") flag.StringVar(&args.OutputFilePath, "output", "", "输出文件路径") - flag.Parse() fmt.Printf( - "url: %s, token: %s, taskId: %s, executionCluster: %s, pull-retry: %d, inputFilePath: %s, outputFilePath: %s\n", + "url: %s, token: %s, taskId: %s, executionCluster: %s, pull-retry: %d, keep-running: %t, "+ + "inputFilePath: %s, outputFilePath: %s\n", args.Url, args.Token, args.TaskId, args.ExecutionCluster, args.PullRetry, + args.KeepRunning, args.InputFilePath, args.OutputFilePath, ) @@ -65,3 +68,8 @@ func (arg *Arguments) Offline() bool { func (arg *Arguments) Online() bool { return arg.Url != "" && arg.Token != "" && (arg.TaskId != "" || arg.ExecutionCluster != "") } + +// ShouldKeepRunning 是否无限循环拉取任务执行 +func (arg *Arguments) ShouldKeepRunning() bool { + return arg.Online() && arg.KeepRunning && arg.TaskId == "" +} diff --git a/analysis-tool-sdk-golang/util/file.go b/analysis-tool-sdk-golang/util/file.go index da45c8d..d5cff73 100644 --- a/analysis-tool-sdk-golang/util/file.go +++ b/analysis-tool-sdk-golang/util/file.go @@ -22,6 +22,11 @@ const PackageTypeDocker = "DOCKER" const WorkDir = "/bkrepo/workspace" const manifestPath = "manifest.json" +// CleanWorkDir 清理工作空间 +func CleanWorkDir() error { + return os.RemoveAll(WorkDir) +} + // WriteToFile 工具输出写入文件 func WriteToFile(outputFilePath string, toolOutput *object.ToolOutput) error { toolOutputContent, err := json.Marshal(toolOutput)