refactor
This commit is contained in:
+36
-18
@@ -16,9 +16,9 @@ var secrets [][]byte
|
|||||||
var templateRegex = regexp.MustCompile(`\{\{\s*([^\}]+)\s*\}\}`)
|
var templateRegex = regexp.MustCompile(`\{\{\s*([^\}]+)\s*\}\}`)
|
||||||
|
|
||||||
func ResolveSecrets(template string) string {
|
func ResolveSecrets(template string) string {
|
||||||
if !templateRegex.MatchString(template) {
|
if !templateRegex.MatchString(template) {
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
|
|
||||||
var secretsMap map[string]interface{}
|
var secretsMap map[string]interface{}
|
||||||
LoadSecret(&secretsMap)
|
LoadSecret(&secretsMap)
|
||||||
@@ -28,23 +28,38 @@ func ResolveSecrets(template string) string {
|
|||||||
varName := strings.TrimSpace(templateRegex.FindStringSubmatch(match)[1])
|
varName := strings.TrimSpace(templateRegex.FindStringSubmatch(match)[1])
|
||||||
pathParts := strings.Split(varName, ".")
|
pathParts := strings.Split(varName, ".")
|
||||||
value := getNestedValue(secretsMap, pathParts)
|
value := getNestedValue(secretsMap, pathParts)
|
||||||
|
|
||||||
if value != nil {
|
if value != nil {
|
||||||
return stringify(value)
|
return stringify(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
})
|
})
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadSecret(v any) {
|
func LoadSecret(v any) {
|
||||||
for _, data := range secrets {
|
for _, data := range secrets {
|
||||||
if err := yaml.Unmarshal(data, v); err != nil {
|
var tempData map[string]interface{}
|
||||||
Logger.Warn().Err(err).Send()
|
|
||||||
}
|
if err := yaml.Unmarshal(data, &tempData); err != nil {
|
||||||
}
|
Logger.Warn().Err(err).Send()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if secretData, exists := tempData["secret"]; exists {
|
||||||
|
secretBytes, err := yaml.Encode(secretData, 2)
|
||||||
|
if err != nil {
|
||||||
|
Logger.Warn().Err(err).Send()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := yaml.Unmarshal(secretBytes, v); err != nil {
|
||||||
|
Logger.Warn().Err(err).Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func PatchSecret(path []string, value any) error {
|
func PatchSecret(path []string, value any) error {
|
||||||
@@ -55,7 +70,7 @@ func PatchSecret(path []string, value any) error {
|
|||||||
// empty config is OK
|
// empty config is OK
|
||||||
b, _ := os.ReadFile(SecretPath)
|
b, _ := os.ReadFile(SecretPath)
|
||||||
|
|
||||||
b, err := yaml.Patch(b, path, value)
|
b, err := yaml.Patch(b, append([]string{"secret"}, path...), value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -82,29 +97,32 @@ func initSecret(secret string) {
|
|||||||
}
|
}
|
||||||
Info["secret_path"] = SecretPath
|
Info["secret_path"] = SecretPath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if data, err := os.ReadFile(SecretPath); err == nil {
|
||||||
|
secrets = append(secrets, data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNestedValue(m map[string]interface{}, path []string) interface{} {
|
func getNestedValue(m map[string]interface{}, path []string) interface{} {
|
||||||
if len(path) == 0 || m == nil {
|
if len(path) == 0 || m == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
key := path[0]
|
key := path[0]
|
||||||
value, exists := m[key]
|
value, exists := m[key]
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(path) == 1 {
|
if len(path) == 1 {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Für verschachtelte Maps
|
// Check nested maps
|
||||||
switch nextMap := value.(type) {
|
switch nextMap := value.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
return getNestedValue(nextMap, path[1:])
|
return getNestedValue(nextMap, path[1:])
|
||||||
case map[interface{}]interface{}:
|
case map[interface{}]interface{}:
|
||||||
// Konvertiere map[interface{}]interface{} zu map[string]interface{}
|
|
||||||
stringMap := make(map[string]interface{})
|
stringMap := make(map[string]interface{})
|
||||||
for k, v := range nextMap {
|
for k, v := range nextMap {
|
||||||
if keyStr, ok := k.(string); ok {
|
if keyStr, ok := k.(string); ok {
|
||||||
@@ -126,4 +144,4 @@ func stringify(value interface{}) string {
|
|||||||
default:
|
default:
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user