[agent-operator] - image bastion mode

This commit is contained in:
zeaslity
2024-04-19 16:50:56 +08:00
parent 6f9cba1270
commit c6ee9ed859
18 changed files with 609 additions and 83 deletions

View File

@@ -3,6 +3,7 @@ package utils
import (
"io"
"os"
"path/filepath"
)
// AppendSourceToFile 将源文件的内容添加到目标文件使用golang标准库完成跨平台、安全性更强
@@ -34,6 +35,17 @@ func AppendSourceToFile(sourceFile, targetFile string) bool {
return true
}
// AppendOverwriteContentToFile 向目标文件中写入一些内容,覆盖源文件
func AppendOverwriteContentToFile(content string, targetFile string) bool {
err := os.Remove(targetFile)
if err != nil {
log.WarnF("[BasicAppendOverwriteContentToFile] - Error removing file: %s , error is %s", targetFile, err.Error())
}
return AppendContentToFile(content, targetFile)
}
// AppendContentToFile 向目标文件中追加写入一些内容
func AppendContentToFile(content string, targetFile string) bool {
@@ -87,3 +99,64 @@ func FileExists(fileFullPath string) bool {
_, err := os.Stat(fileFullPath)
return err == nil
}
// FileExistAndNotNull 文件不为空返回true 文件为空返回false
func FileExistAndNotNull(filename string) bool {
// Check if the file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
log.DebugF("文件 %s 不存在!", filename)
return false
}
// Open the file for reading
file, err := os.Open(filename)
defer file.Close()
if err != nil {
log.DebugF("文件 %s 打开有误!", filename)
return false // Handle error according to your needs
}
// Get the file size
info, _ := file.Stat()
size := info.Size()
// Check if the file is not empty
return size > 0
}
func ListAllFileInFolder(folderName string) ([]string, error) {
files := make([]string, 0)
err := filepath.Walk(folderName, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, info.Name())
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
func RemoveFolderComplete(folderName string) bool {
err := filepath.Walk(folderName,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return os.Remove(path)
}
return nil
})
if err != nil {
return false
}
err = os.RemoveAll(folderName)
if err != nil {
return false
}
return true
}