Skip to content

Commit

Permalink
feat: 支持无限循环拉取任务执行
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlkl committed Jul 13, 2023
1 parent 119bc23 commit e85f7d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
12 changes: 11 additions & 1 deletion analysis-tool-sdk-golang/framework/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions analysis-tool-sdk-golang/object/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Arguments struct {
PullRetry int
InputFilePath string
OutputFilePath string
KeepRunning bool
}

var args *Arguments
Expand All @@ -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,
)
Expand Down Expand Up @@ -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 == ""
}
5 changes: 5 additions & 0 deletions analysis-tool-sdk-golang/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e85f7d0

Please sign in to comment.