[Agent][Deploy] - cmii update accomplish
This commit is contained in:
@@ -1,19 +1,36 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GenerateRandomString(length int) string {
|
||||
func GenerateRandomString(length int, includeSpecialChar bool) string {
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
b[i] = chars[rand.Intn(len(chars))]
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -49,3 +66,16 @@ func ByteSizeToString(size uint64) string {
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user