package executor import ( "bytes" "fmt" "os/exec" "strings" ) var RemoveForcePrefix = []string{"rm", "-rf"} // 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 { // 暂不添加 //if !BasicFileExistAndNotNull(filename) { // log.DebugF("文件替换") //} 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 } } // BasicFileExistAndNotNull 文件不为空返回true 文件为空返回false func BasicFileExistAndNotNull(filename string) bool { cmd := exec.Command("test", "-s", filename) err := cmd.Run() if err != nil { log.DebugF("文件 %s 为空!", filename) return false } else { return true } } func BasicPrettyPrint(resultOk bool, resultLog []string) { fmt.Printf("resultOK is => %#v\n", resultOk) if resultLog == nil || len(resultLog) == 0 { return } for _, s := range resultLog { fmt.Println(s) } } func BasicSystemdShutdown(serviceName string) (ok bool, resultLog []string) { if !strings.HasSuffix(serviceName, ".service") { serviceName = serviceName + ".service" } systemdFilePath := []string{ "/lib/systemd/system/", "/etc/systemd/system", } // 检查是否存在 var existService bool for _, s := range systemdFilePath { resultOk, _ := PipelineCommandExecutor( [][]string{ {"ls", s, }, { "grep", serviceName, }, }) if resultOk { existService = true } } if !existService { return true, []string{ serviceName, "该服务不存在!", } } // 关闭 var shutDownCommand = [][]string{ { "systemctl", "stop", serviceName, }, { "sleep", "1", }, { "systemctl", "disable", serviceName, }, } resultOK := PureResultSingleExecuteBatch(shutDownCommand) return resultOK, resultLog } func BasicSystemdUp(serviceName string) (ok bool, resultLog []string) { if !strings.HasSuffix(serviceName, ".service") { serviceName = serviceName + ".service" } systemdFilePath := []string{ "/lib/systemd/system/", "/etc/systemd/system", } // 检查是否存在 var existService bool for _, s := range systemdFilePath { resultOk, _ := PipelineCommandExecutor( [][]string{ {"ls", s, }, { "grep", serviceName, }, }) if resultOk { existService = true } } if !existService { return true, []string{ serviceName, "该服务不存在!", } } // 关闭 var shutDownCommand = [][]string{ { "systemctl", "start", serviceName, }, { "sleep", "1", }, { "systemctl", "enable", serviceName, }, } resultOK := PureResultSingleExecuteBatch(shutDownCommand) return resultOK, resultLog } func BasicTransPipelineCommand(pipelineString string) (pipelineCommand [][]string) { var pipelineCommandC [][]string split := strings.Split(pipelineString, "|") if len(split) == 0 { log.WarnF("输入的pipelineString有误! %s", pipelineString) return pipelineCommandC } for _, s := range split { // 去除掉无效的空行 singleCommand := strings.Split(s, " ") var realSingleCommand []string for i := range singleCommand { if singleCommand[i] != "" { realSingleCommand = append(realSingleCommand, singleCommand[i]) } } pipelineCommandC = append(pipelineCommandC, realSingleCommand) } return pipelineCommandC } func BasicConvertBufferToSlice(outputBuffer bytes.Buffer) (resultLog []string) { s := outputBuffer.String() split := strings.Split(s, "\n") return split }