[Agent] [Executor] 优化Functional Executor
This commit is contained in:
65
agent-go/executor/BasicFunction.go
Normal file
65
agent-go/executor/BasicFunction.go
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user