[agent-deploy] - basic accomplish

This commit is contained in:
zeaslity
2024-04-25 16:44:40 +08:00
parent 4e5af5856a
commit 08e5a4d422
51 changed files with 8751 additions and 210 deletions

View File

@@ -0,0 +1,68 @@
package assert
import (
"fmt"
"reflect"
"strings"
)
var Asserter = NewAssert()
// Assert utility class
type Assert struct{}
// NewAssert returns a new instance of Assert
func NewAssert() *Assert {
return &Assert{}
}
// NotEmpty checks if the given value is not empty
func (a *Assert) NotEmpty(value interface{}, message string) {
if isEmptyValue(reflect.ValueOf(value)) {
panic(fmt.Sprintf("Assertion failed: %s", message))
}
}
// NotBlank checks if the given string is not blank
func (a *Assert) NotBlank(str string, message string) {
if str == "" || len(strings.TrimSpace(str)) == 0 {
panic(fmt.Sprintf("Assertion failed: %s", message))
}
}
// Equals checks if two values are equal
func (a *Assert) Equals(expected, actual interface{}, message string) {
if !reflect.DeepEqual(expected, actual) {
panic(fmt.Sprintf("Assertion failed: %s. Expected '%v' but got '%v'", message, expected, actual))
}
}
// Nil checks if the given value is nil
func (a *Assert) Nil(value interface{}, message string) {
if value != nil {
panic(fmt.Sprintf("Assertion failed: %s", message))
}
}
// NotNil checks if the given value is not nil
func (a *Assert) NotNil(value interface{}, message string) {
if value == nil {
panic(fmt.Sprintf("Assertion failed: %s", message))
}
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Map:
return v.IsNil() || v.Len() == 0
case reflect.Ptr:
if v.IsNil() {
return true
}
return isEmptyValue(v.Elem())
default:
return false
}
}

View File

@@ -133,6 +133,11 @@ func GzipFileNameToImageFullName(gzipFileName string) (imageFullName string) {
}
gzipFileName = strings.TrimSuffix(gzipFileName, ".tar.gz")
if strings.HasPrefix(gzipFileName, "docker=library") {
// docker=library=busybox=latest.tar.gz
return strings.Split(gzipFileName, "=")[2] + ":" + strings.Split(gzipFileName, "=")[3]
}
if strings.HasPrefix(gzipFileName, "docker") {
return strings.Split(gzipFileName, "=")[1] + "/" + strings.Split(gzipFileName, "=")[2] + ":" + strings.Split(gzipFileName, "=")[3]
}

View File

@@ -68,6 +68,29 @@ func AppendContentToFile(content string, targetFile string) bool {
return true
}
func AppendContentWithSplitLineToFile(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
}
// AppendNullToFile 清空一个文件
func AppendNullToFile(targetFile string) bool {

View File

@@ -0,0 +1,17 @@
package utils
import (
"math/rand"
"time"
)
func GenerateRandomString(length int) string {
rand.Seed(time.Now().UnixNano())
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
b := make([]byte, length)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
}
return string(b)
}