[ Cmii ] [ Octopus ] - 优化项目结构
This commit is contained in:
83
agent-common/utils/FileUtils.go
Normal file
83
agent-common/utils/FileUtils.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// AppendSourceToFile 将源文件的内容添加到目标文件,使用golang标准库完成,跨平台、安全性更强
|
||||
func AppendSourceToFile(sourceFile, targetFile string) bool {
|
||||
|
||||
// 打开源文件
|
||||
source, err := os.Open(sourceFile)
|
||||
if err != nil {
|
||||
log.ErrorF("[BasicAppendSourceToFile] - error open source file => %s, error is %s", sourceFile, err.Error())
|
||||
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, error is %s", sourceFile, err.Error())
|
||||
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
|
||||
}
|
||||
|
||||
// AppendContentToFile 向目标文件中追加写入一些内容
|
||||
func AppendContentToFile(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 , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
defer file.Close() // 确保文件最终被关闭
|
||||
|
||||
// 写入内容到文件
|
||||
if _, err := file.WriteString(content); err != nil {
|
||||
log.ErrorF("[BasicAppendContentToFile] - Error writing to file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// AppendNullToFile 清空一个文件
|
||||
func AppendNullToFile(targetFile string) bool {
|
||||
|
||||
// 使用os.O_TRUNC清空文件内容
|
||||
file, err := os.OpenFile(targetFile, os.O_TRUNC|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.ErrorF("[AppendNullToFile] - Error opening file: %s, error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
defer file.Close() // 确保在函数退出前关闭文件
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func WordSpaceCompletion(source string, totalLength int) string {
|
||||
sourceLength := len(source)
|
||||
|
||||
if sourceLength >= totalLength {
|
||||
return source
|
||||
}
|
||||
|
||||
for i := 0; i < totalLength-sourceLength; i++ {
|
||||
source += " "
|
||||
}
|
||||
|
||||
return source
|
||||
}
|
||||
22
agent-common/utils/MathUtils.go
Normal file
22
agent-common/utils/MathUtils.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package utils
|
||||
|
||||
func MinInt(x, y int) int {
|
||||
if x < y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
func MaxInt32(x, y int32) int32 {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
func MaxInt(x, y int) int {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
49
agent-common/utils/PrintUtils.go
Normal file
49
agent-common/utils/PrintUtils.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"wdd.io/agent-common/logger"
|
||||
)
|
||||
|
||||
var log = logger.Log
|
||||
|
||||
func BeautifulPrint(object interface{}) {
|
||||
|
||||
bytes, err := json.MarshalIndent(object, "", " ")
|
||||
if err != nil {
|
||||
log.ErrorF("[BeautifulPrint] - json marshal error ! => %v", object)
|
||||
}
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println(string(bytes))
|
||||
fmt.Println()
|
||||
|
||||
}
|
||||
|
||||
func BeautifulPrintToString(object interface{}) string {
|
||||
|
||||
bytes, err := json.MarshalIndent(object, "", " ")
|
||||
if err != nil {
|
||||
log.ErrorF("[BeautifulPrint] - json marshal error ! => %v", object)
|
||||
}
|
||||
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
func BeautifulPrintListWithTitle(contend []string, title string) {
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println(fmt.Sprintf("content tile is => %s", title))
|
||||
for _, line := range contend {
|
||||
bytes, _ := json.MarshalIndent(line, "", " ")
|
||||
fmt.Println(string(bytes))
|
||||
}
|
||||
fmt.Println("---------- end -----------")
|
||||
}
|
||||
|
||||
func SplitLinePrint() {
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
}
|
||||
29
agent-common/utils/TimeUtils.go
Normal file
29
agent-common/utils/TimeUtils.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ParseDateTimeTime 输出系统时间的格式为"2006-01-02 15:04:05"形式的时间字符串
|
||||
func ParseDateTimeTime() string {
|
||||
|
||||
now := time.Now()
|
||||
|
||||
/*loc := time.FixedZone("UTC+8", 8*60*60) // 创建东八区时区对象
|
||||
localTime := now.In(loc) // 转换为东八区时间*/
|
||||
|
||||
return now.Format(time.DateTime)
|
||||
}
|
||||
|
||||
// ParseISOLocalDateTime 时间格式为2023-08-11T10:48:15+08:00
|
||||
func ParseISOLocalDateTime() string {
|
||||
now := time.Now()
|
||||
return now.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func TimeSplitFormatString() string {
|
||||
now := time.Now()
|
||||
formattedTime := now.Format("2006-01-02-15-04-05")
|
||||
|
||||
return formattedTime
|
||||
}
|
||||
Reference in New Issue
Block a user