初始化项目
This commit is contained in:
113
agent-common/utils/DownloadUtils.go
Normal file
113
agent-common/utils/DownloadUtils.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/proxy"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
)
|
||||
|
||||
// DownloadFile 下载一个文件,调用http.Get()下载
|
||||
func DownloadFile(downloadUrl, desFile string) (downloadOk bool, resultLog []string) {
|
||||
|
||||
log.InfoF("Downloading file: %s", downloadUrl)
|
||||
resp, err := http.Get(downloadUrl)
|
||||
if err != nil {
|
||||
return false, []string{fmt.Sprintf("Error downloading file: %v", err)}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false, []string{fmt.Sprintf("Unexpected status code: %d", resp.StatusCode)}
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false, []string{fmt.Sprintf("Error reading response body: %v", err)}
|
||||
}
|
||||
|
||||
err = os.WriteFile(desFile, body, 0644)
|
||||
if err != nil {
|
||||
return false, []string{fmt.Sprintf("Error writing file to disk: %v", err)}
|
||||
}
|
||||
|
||||
log.InfoF("Downloading complete: %s", downloadUrl)
|
||||
return true, []string{}
|
||||
}
|
||||
|
||||
func DownloadFileBySocks5(downloadUrl string, socksProxyUrl string, proxyUser string, proxyPass string, desFile string) (downloadOk bool, resultLog []string) {
|
||||
// 解析下载URL
|
||||
_, err := url.Parse(downloadUrl)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error parsing download URL: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 创建文件
|
||||
out, err := os.Create(desFile)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error creating file: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// 创建HTTP客户端
|
||||
client := &http.Client{}
|
||||
|
||||
// 如果提供了代理URL
|
||||
if socksProxyUrl != "" {
|
||||
// 解析代理URL
|
||||
parsedProxyUrl, err := url.Parse(socksProxyUrl)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error parsing proxy URL: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 使用SOCKS5代理
|
||||
auth := &proxy.Auth{User: proxyUser, Password: proxyPass}
|
||||
dialer, err := proxy.SOCKS5("tcp", parsedProxyUrl.Host, auth, proxy.Direct)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error creating SOCKS5 dialer: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 设置HTTP和HTTPS代理
|
||||
httpTransport := &http.Transport{
|
||||
Dial: dialer.Dial,
|
||||
}
|
||||
client.Transport = httpTransport
|
||||
}
|
||||
|
||||
// 发送HTTP GET请求
|
||||
resp, err := client.Get(downloadUrl)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error making GET request: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 检查HTTP响应状态码
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
resultLog = append(resultLog, "Server returned HTTP status "+resp.Status)
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// 将HTTP响应内容写入文件
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
if err != nil {
|
||||
resultLog = append(resultLog, "Error writing to file: "+err.Error())
|
||||
return false, resultLog
|
||||
}
|
||||
|
||||
// check file exists
|
||||
existAndNotNull := FileExistAndNotNull(desFile)
|
||||
if !existAndNotNull {
|
||||
return false, []string{
|
||||
"[DownloadFileBySocks5] - file not exist download error!",
|
||||
}
|
||||
}
|
||||
|
||||
return true, resultLog
|
||||
}
|
||||
322
agent-common/utils/FileUtils.go
Normal file
322
agent-common/utils/FileUtils.go
Normal file
@@ -0,0 +1,322 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// AppendFileToFile 将源文件的内容添加到目标文件
|
||||
func AppendFileToFile(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
|
||||
}
|
||||
|
||||
// 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 向目标文件(targetFile 文件的绝对路径)中追加写入一些内容, 如果文件不存在,那么就创建相应的目录及文件
|
||||
func AppendContentToFile(content string, targetFile string) bool {
|
||||
|
||||
// 创建目标文件的目录(递归创建)
|
||||
dir := filepath.Dir(targetFile)
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// 打开文件用于追加。如果文件不存在,将会创建一个新文件。
|
||||
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 content != "" {
|
||||
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
|
||||
}
|
||||
|
||||
// AppendOverwriteListContentToFile 将一个字符串列表中的内容,一行一行的写入文件中
|
||||
func AppendOverwriteListContentToFile(contentList []string, targetFile string) bool {
|
||||
|
||||
err := os.Remove(targetFile)
|
||||
if err != nil {
|
||||
log.WarnF("[AppendOverwriteListContentToFile] - Error removing file: %s , error is %s", targetFile, err.Error())
|
||||
}
|
||||
|
||||
// 打开文件用于追加。如果文件不存在,将会创建一个新文件。
|
||||
file, err := os.OpenFile(targetFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.ErrorF("[AppendOverwriteListContentToFile] - Error opening file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
defer file.Close() // 确保文件最终被关闭
|
||||
|
||||
// 写入内容到文件
|
||||
for _, contentLine := range contentList {
|
||||
//bytes, _ := json.Marshal(contentLine)
|
||||
if _, err := file.WriteString(contentLine + "\n"); err != nil {
|
||||
log.ErrorF("[AppendOverwriteListContentToFile] - Error writing to file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// AppendK8sYamlWithSplitLineToFile 专门为k8s的yaml文件设计的,在每次写入内容之前,先写入一行分隔符
|
||||
func AppendK8sYamlWithSplitLineToFile(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("---"); err != nil {
|
||||
log.ErrorF("[BasicAppendContentToFile] - Error writing to file: %s , error is %s", targetFile, err.Error())
|
||||
return false
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
// AppendNullOverWriteToFile 清空一个文件
|
||||
func AppendNullOverWriteToFile(targetFile string) bool {
|
||||
|
||||
// 使用os.O_TRUNC清空文件内容
|
||||
file, err := os.OpenFile(targetFile, os.O_TRUNC|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.ErrorF("[AppendNullOverWriteToFile] - 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
|
||||
}
|
||||
|
||||
// IsDirOrFile 如果是目录则返回true,是文件则返回false
|
||||
func IsDirOrFile(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return info.IsDir()
|
||||
}
|
||||
|
||||
// FileExists 文件存在返回true,不存在返回false,如果文件是一个目录,也返回false
|
||||
func FileExists(fileFullPath string) bool {
|
||||
info, err := os.Stat(fileFullPath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
// FileOrFolderExists 文件或者目录是否返回true,不存在返回false
|
||||
func FileOrFolderExists(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
|
||||
}
|
||||
|
||||
// ListAllFileInFolder 列出一个目录中的所有文件,返回文件名,忽略folder,不带全路径
|
||||
func ListAllFileInFolder(folderName string) ([]string, error) {
|
||||
return listAllFileInFolderWithFullPath(folderName, false)
|
||||
}
|
||||
|
||||
func ListAllFileInFolderWithFullPath(folderName string) ([]string, error) {
|
||||
return listAllFileInFolderWithFullPath(folderName, true)
|
||||
}
|
||||
|
||||
func listAllFileInFolderWithFullPath(folderName string, fullPath bool) ([]string, error) {
|
||||
files := make([]string, 0)
|
||||
err := filepath.Walk(folderName, func(path string, info os.FileInfo, err error) error {
|
||||
if !info.IsDir() {
|
||||
if fullPath {
|
||||
files = append(files, path)
|
||||
} else {
|
||||
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
|
||||
}
|
||||
|
||||
func ReadAllContentFromFile(fileFullPath string) (result []string) {
|
||||
|
||||
f, err := os.Open(fileFullPath)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return result
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if len(line) > 0 { // ignore empty lines
|
||||
result = append(result, line)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
fmt.Println(err)
|
||||
return result
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// MoveFolerToAnother 将源文件夹中除了子文件夹外的所有文件移动到目标文件夹
|
||||
func MoveFolerToAnother(srcDir, dstDir string) error {
|
||||
// 读取源文件夹中的所有条目
|
||||
entries, err := os.ReadDir(srcDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取源文件夹失败: %w", err)
|
||||
}
|
||||
|
||||
// 遍历所有条目
|
||||
for _, entry := range entries {
|
||||
// 跳过子文件夹
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
// 构造源文件路径和目标文件路径
|
||||
srcPath := filepath.Join(srcDir, entry.Name())
|
||||
dstPath := filepath.Join(dstDir, entry.Name())
|
||||
|
||||
// 移动文件
|
||||
if err := os.Rename(srcPath, dstPath); err != nil {
|
||||
return fmt.Errorf("移动文件失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCurrentUserFolder 获取运行环境当前用户的根目录
|
||||
func GetCurrentUserFolder() string {
|
||||
usr, err := user.Current()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return ""
|
||||
}
|
||||
|
||||
return usr.HomeDir
|
||||
}
|
||||
8
agent-common/utils/MapUtils.go
Normal file
8
agent-common/utils/MapUtils.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package utils
|
||||
|
||||
func MergeMap(originMap map[string]string, mergeInMap map[string]string) map[string]string {
|
||||
for k, v := range mergeInMap {
|
||||
originMap[k] = v
|
||||
}
|
||||
return originMap
|
||||
}
|
||||
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
|
||||
}
|
||||
57
agent-common/utils/PrintUtils.go
Normal file
57
agent-common/utils/PrintUtils.go
Normal file
@@ -0,0 +1,57 @@
|
||||
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 BeautifulPrintWithTitle(contend any, title string) {
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println(fmt.Sprintf("content tile is => %s", title))
|
||||
bytes, _ := json.MarshalIndent(contend, "", " ")
|
||||
fmt.Println(string(bytes))
|
||||
fmt.Println("---------- end -----------")
|
||||
}
|
||||
|
||||
func BeautifulPrintListWithTitle(contend []string, title string) {
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println(fmt.Sprintf("content tile is => %s", title))
|
||||
for _, line := range contend {
|
||||
fmt.Println(line)
|
||||
}
|
||||
fmt.Println("---------- end -----------")
|
||||
}
|
||||
|
||||
func SplitLinePrint() {
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
fmt.Println()
|
||||
}
|
||||
21
agent-common/utils/ReflectUtils.go
Normal file
21
agent-common/utils/ReflectUtils.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import "reflect"
|
||||
|
||||
// CopySameFields 利用反射,将a中的所有同名字段的值 复制到b中的对应字段
|
||||
func CopySameFields(source, target interface{}) {
|
||||
va := reflect.ValueOf(source).Elem()
|
||||
vb := reflect.ValueOf(target).Elem()
|
||||
|
||||
for i := 0; i < va.NumField(); i++ {
|
||||
// 忽略source中 空值的部分
|
||||
if va.Field(i).IsZero() {
|
||||
continue
|
||||
}
|
||||
|
||||
fieldName := va.Type().Field(i).Name
|
||||
if vb.FieldByName(fieldName).IsValid() {
|
||||
vb.FieldByName(fieldName).Set(va.Field(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
81
agent-common/utils/StringUtils.go
Normal file
81
agent-common/utils/StringUtils.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GenerateRandomString(length int, includeSpecialChar bool) string {
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
letters := "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
specialChars := ".!@_#%^&*()-+"
|
||||
|
||||
b := make([]rune, length)
|
||||
|
||||
letterProbability := len(letters) * 100 / (len(letters) + len(specialChars))
|
||||
|
||||
if includeSpecialChar {
|
||||
for i := range b {
|
||||
if rand.Intn(100) < letterProbability { // 概率选择字母
|
||||
b[i] = rune(letters[rand.Intn(len(letters))])
|
||||
} else { // 概率选择特殊字符
|
||||
b[i] = rune(specialChars[rand.Intn(len(specialChars))])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i := range b {
|
||||
b[i] = rune(letters[rand.Intn(len(letters))])
|
||||
}
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func ByteSizeToString(size uint64) string {
|
||||
const (
|
||||
B = 1
|
||||
KB = 1024 * B
|
||||
MB = 1024 * KB
|
||||
GB = 1024 * MB
|
||||
TB = 1024 * GB
|
||||
)
|
||||
|
||||
var unit string
|
||||
var value float64
|
||||
|
||||
switch {
|
||||
case size >= TB:
|
||||
value = float64(size) / TB
|
||||
unit = "TB"
|
||||
case size >= GB:
|
||||
value = float64(size) / GB
|
||||
unit = "GB"
|
||||
case size >= MB:
|
||||
value = float64(size) / MB
|
||||
unit = "MB"
|
||||
case size >= KB:
|
||||
value = float64(size) / KB
|
||||
unit = "KB"
|
||||
default:
|
||||
value = float64(size)
|
||||
unit = "B"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%.2f %s", value, unit)
|
||||
}
|
||||
|
||||
func Base64Encode(content string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(content))
|
||||
}
|
||||
|
||||
func Base64Decode(content string) string {
|
||||
decodeString, err := base64.StdEncoding.DecodeString(content)
|
||||
if err != nil {
|
||||
log.ErrorF("Base64Decode error: %s", err.Error())
|
||||
return ""
|
||||
}
|
||||
return string(decodeString)
|
||||
}
|
||||
37
agent-common/utils/StringUtils_test.go
Normal file
37
agent-common/utils/StringUtils_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBase64Decode(t *testing.T) {
|
||||
decode := Base64Decode("blljUk45MXIuX2hq")
|
||||
|
||||
fmt.Println(decode)
|
||||
}
|
||||
func TestBase64Encode(t *testing.T) {
|
||||
encode := Base64Encode("RB6Vfzs7XdC2")
|
||||
|
||||
fmt.Println(encode)
|
||||
}
|
||||
|
||||
func TestGetRandomString(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
randomString := GenerateRandomString(10, true)
|
||||
fmt.Printf("id: %d randomString: %s\n", i, randomString)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRandomMySQLPassword(t *testing.T) {
|
||||
|
||||
rootPassword := GenerateRandomString(12, false)
|
||||
rootPasswordBase64 := Base64Encode(rootPassword)
|
||||
|
||||
k8sAdminPass := GenerateRandomString(12, true)
|
||||
|
||||
fmt.Println(rootPassword)
|
||||
fmt.Println(rootPasswordBase64)
|
||||
fmt.Println()
|
||||
fmt.Println(k8sAdminPass)
|
||||
}
|
||||
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