[Agent] [Executor] 优化Functional Executor

This commit is contained in:
zeaslity
2023-10-13 17:22:21 +08:00
parent 0a294fa81b
commit 020fc76e05
8 changed files with 215 additions and 150 deletions

View File

@@ -0,0 +1,65 @@
package executor
import "os/exec"
// BasicCommandExists 判定命令是否存在
func BasicCommandExists(commandName string) bool {
cmd := exec.Command("command", "-v", commandName)
err := cmd.Run()
if err != nil {
log.DebugF("指令 %s 不存在", commandName)
return false
} else {
return true
}
}
// BasicCommandExistsBatch 判定批量命令是否存在
func BasicCommandExistsBatch(commandNameList []string) (bool, string) {
for _, commandName := range commandNameList {
if !BasicCommandExists(commandName) {
return false, commandName
}
}
return true, ""
}
// BasicReplace 基础替换命令
func BasicReplace(filename string, origin string, replace string) bool {
cmd := exec.Command("sed", "-i", "s/"+origin+"/"+replace+"/g", filename)
err := cmd.Run()
if err != nil {
log.DebugF("文件 %s [%s] => [%s] 错误!", filename, origin, replace)
return false
} else {
return true
}
}
func BasicFileExists(filename string) bool {
cmd := exec.Command("test", "-f", filename)
err := cmd.Run()
if err != nil {
log.DebugF("文件 %s 不存在!", filename)
return false
} else {
return true
}
}
func BasicFileExistAndNotNull(filename string) bool {
cmd := exec.Command("test", "-z", filename)
err := cmd.Run()
if err != nil {
log.DebugF("文件 %s 不存在!", filename)
return false
} else {
return true
}
}