[ Agent ] [ BASE ] - add default ssh key install
This commit is contained in:
@@ -3,6 +3,8 @@ package executor
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
@@ -87,6 +89,32 @@ func BasicFileExistInFolder(fileName string, folderList ...string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func BasicGrepItemInFile(item string, fileName string) bool {
|
||||
|
||||
if !BasicFileExistAndNotNull(fileName) {
|
||||
log.ErrorF("[BasicGrepItemInFile] - fileName [ %s ] not exits !", fileName)
|
||||
return false
|
||||
}
|
||||
|
||||
ok, _ := PipelineCommandExecutor([][]string{
|
||||
{
|
||||
"cat",
|
||||
fileName,
|
||||
},
|
||||
{
|
||||
"grep",
|
||||
"-q",
|
||||
item,
|
||||
},
|
||||
})
|
||||
|
||||
if ok {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// BasicReplace 基础替换命令
|
||||
func BasicReplace(filename string, origin string, replace string) bool {
|
||||
|
||||
@@ -340,3 +368,31 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// BasicAppendSourceToFile 将源文件的内容添加到目标文件,使用golang标准库完成,跨平台、安全性更强
|
||||
func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
|
||||
// 打开源文件
|
||||
source, err := os.Open(sourceFile)
|
||||
if err != nil {
|
||||
log.ErrorF("[BasicAppendSourceToFile] - error open source file => %s", sourceFile)
|
||||
return false
|
||||
}
|
||||
defer source.Close()
|
||||
|
||||
// 打开目标文件,如果不存在则创建,如果存在则在末尾追加
|
||||
target, err := os.OpenFile(targetFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.ErrorF("[BasicAppendSourceToFile] - error open target file => %s", sourceFile)
|
||||
return false
|
||||
}
|
||||
defer target.Close()
|
||||
|
||||
// 将源文件内容复制到目标文件
|
||||
_, err = io.Copy(target, source)
|
||||
if err != nil {
|
||||
log.ErrorF("[BasicAppendSourceToFile] - Error appending to target file: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user