[ Agent ] [ Base ] - nfs-server nfs
This commit is contained in:
@@ -115,6 +115,30 @@ func BasicGrepItemInFile(item string, fileName string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func BasicInstallSoftwares(installPrefix []string, isStrict bool, softwares ...string) (bool, []string) {
|
||||
|
||||
var installLog []string
|
||||
|
||||
for _, software := range softwares {
|
||||
log.DebugF("[BasicInstallSoftwares] - going to install [ %s ]", software)
|
||||
|
||||
if !PureResultSingleExecute(append(installPrefix, software)) {
|
||||
|
||||
failedInstall := fmt.Sprintf("[BasicInstallSoftwares] - software of [ %s ] install failed !", software)
|
||||
installLog = append(installLog, failedInstall)
|
||||
|
||||
if isStrict {
|
||||
return false, installLog
|
||||
}
|
||||
}
|
||||
|
||||
successInstall := fmt.Sprintf("[BasicInstallSoftwares] - software of [ %s ] install success !", software)
|
||||
installLog = append(installLog, successInstall)
|
||||
}
|
||||
|
||||
return true, installLog
|
||||
}
|
||||
|
||||
// BasicReplace 基础替换命令
|
||||
func BasicReplace(filename string, origin string, replace string) bool {
|
||||
|
||||
@@ -371,6 +395,7 @@ func BasicDownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog
|
||||
|
||||
// BasicAppendSourceToFile 将源文件的内容添加到目标文件,使用golang标准库完成,跨平台、安全性更强
|
||||
func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
|
||||
|
||||
// 打开源文件
|
||||
source, err := os.Open(sourceFile)
|
||||
if err != nil {
|
||||
@@ -396,3 +421,37 @@ func BasicAppendSourceToFile(sourceFile, targetFile string) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// BasicAppendContentToFile 向目标文件中追加写入一些内容
|
||||
func BasicAppendContentToFile(content string, targetFile string) bool {
|
||||
|
||||
// 打开文件用于追加。如果文件不存在,将会创建一个新文件。
|
||||
file, err := os.OpenFile(targetFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.ErrorF("[BasicAppendContentToFile] - Error opening file: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
defer file.Close() // 确保文件最终被关闭
|
||||
|
||||
// 写入内容到文件
|
||||
if _, err := file.WriteString(content); err != nil {
|
||||
log.ErrorF("[BasicAppendContentToFile] - Error writing to file: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// BasicAppendNullToFile 清空一个文件
|
||||
func BasicAppendNullToFile(targetFile string) bool {
|
||||
|
||||
// 使用os.O_TRUNC清空文件内容
|
||||
file, err := os.OpenFile(targetFile, os.O_TRUNC|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.ErrorF("[BasicAppendNullToFile] - Error opening file: %s", err.Error())
|
||||
return false
|
||||
}
|
||||
defer file.Close() // 确保在函数退出前关闭文件
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user