Add base system configuration commands for agent-wdd
- Implemented new base commands for system configuration: * swap: Disable system swap * selinux: Disable SELinux * firewall: Stop and disable firewalld and ufw * sysconfig: Modify system sysctl configuration * ssh: Add SSH-related subcommands (key, port, config) - Updated Config.go to initialize ConfigCache with default values - Added new utility functions in FileUtils.go for file content manipulation - Extended Excutor.go with HardCodeCommandExecutor method
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AppendFileToFile 将源文件的内容添加到目标文件
|
||||
@@ -178,6 +179,15 @@ func FileExists(fileFullPath string) bool {
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
// CreateFolder 创建文件夹,如果文件夹存在,则返回true,否则返回false
|
||||
func CreateFolder(folderName string) bool {
|
||||
// 递归创建 类似于 mkdir -p folderName
|
||||
if _, err := os.Stat(folderName); os.IsNotExist(err) {
|
||||
return os.MkdirAll(folderName, 0755) == nil
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// FileOrFolderExists 文件或者目录是否返回true,不存在返回false
|
||||
func FileOrFolderExists(fileFullPath string) bool {
|
||||
_, err := os.Stat(fileFullPath)
|
||||
@@ -298,6 +308,89 @@ func ReadAllContentFromFile(fileFullPath string) (result []string) {
|
||||
return result
|
||||
}
|
||||
|
||||
// FindContentInFile 在文件中查找内容,如果存在,则返回true,否则返回false 类似于grep -q content targetFile
|
||||
func FindContentInFile(content string, targetFile string) bool {
|
||||
|
||||
// 读取文件内容
|
||||
fileContent, err := os.ReadFile(targetFile)
|
||||
if err != nil {
|
||||
log.Error("[FindContentInFile] - Error reading file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
// 将文件内容按行分割
|
||||
lines := strings.Split(string(fileContent), "\n")
|
||||
|
||||
// 遍历每一行
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, content) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// FindAndReplaceContentInFile 在文件中查找内容,如果存在,则替换,如果存在多个,则替换全部 类似于sed -i 's/oldContent/newContent/g' targetFile
|
||||
func FindAndReplaceContentInFile(oldContent string, newContent string, targetFile string) bool {
|
||||
|
||||
// 读取文件内容
|
||||
fileContent, err := os.ReadFile(targetFile)
|
||||
if err != nil {
|
||||
log.Error("[FindAndReplaceContentInFile] - Error reading file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
// 将文件内容按行分割
|
||||
lines := strings.Split(string(fileContent), "\n")
|
||||
|
||||
// 遍历每一行
|
||||
for i, line := range lines {
|
||||
if strings.Contains(line, oldContent) {
|
||||
lines[i] = strings.Replace(line, oldContent, newContent, -1)
|
||||
}
|
||||
}
|
||||
|
||||
// 将修改后的内容写回文件
|
||||
err = os.WriteFile(targetFile, []byte(strings.Join(lines, "\n")), 0644)
|
||||
if err != nil {
|
||||
log.Error("[FindAndReplaceContentInFile] - Error writing file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// FindAndDeleteContentInFile 在文件中查找内容,如果存在,则删除,如果存在多个,则删除全部 类似于sed -i '/content/d' targetFile
|
||||
func FindAndDeleteContentInFile(content string, targetFile string) bool {
|
||||
|
||||
// 读取文件内容
|
||||
fileContent, err := os.ReadFile(targetFile)
|
||||
if err != nil {
|
||||
log.Error("[FindAndDeleteContentInFile] - Error reading file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
// 将文件内容按行分割
|
||||
lines := strings.Split(string(fileContent), "\n")
|
||||
|
||||
// 过滤掉包含指定内容的行
|
||||
var newLines []string
|
||||
for _, line := range lines {
|
||||
if !strings.Contains(line, content) {
|
||||
newLines = append(newLines, line)
|
||||
}
|
||||
}
|
||||
|
||||
// 将过滤后的内容写回文件
|
||||
err = os.WriteFile(targetFile, []byte(strings.Join(newLines, "\n")), 0644)
|
||||
if err != nil {
|
||||
log.Error("[FindAndDeleteContentInFile] - Error writing file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// MoveFolerToAnother 将源文件夹的所有文件递归移动到目标文件夹
|
||||
func MoveFolerToAnother(srcDir, dstDir string) error {
|
||||
// 读取源文件夹中的所有条目
|
||||
|
||||
Reference in New Issue
Block a user