feat(pkg/policy): improve TopKeys function handling (#1730)

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero 2024-10-09 20:57:03 +08:00 committed by GitHub
parent 349c471035
commit f3c49ae53a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"regexp"
"slices"
"strings"
"unicode"
"github.com/helmfile/helmfile/pkg/runtime"
)
@ -83,13 +84,15 @@ func TopKeys(helmfileContent []byte, hasSeparator bool) []string {
clines := bytes.Split(helmfileContent, []byte("\n"))
for _, line := range clines {
lineStr := strings.TrimSpace(string(line))
lineStr := strings.TrimRightFunc(string(line), unicode.IsSpace)
if lineStr == "" {
continue // Skip empty lines
}
if hasSeparator && separatorRegex.MatchString(lineStr) {
topKeys = append(topKeys, lineStr)
} else if topConfigKeysRegex.MatchString(lineStr) {
}
if topConfigKeysRegex.MatchString(lineStr) {
topKey := strings.SplitN(lineStr, ":", 2)[0]
topKeys = append(topKeys, topKey)
}

View File

@ -230,6 +230,11 @@ func TestTopKeys(t *testing.T) {
helmfileContent: []byte(""),
want: nil,
},
{
name: "sub level contains top level key",
helmfileContent: []byte("bases:\n releases:\n - name: test\n namespace: test\n"),
want: []string{"bases"},
},
}
for _, tt := range tests {