From f3c49ae53a48ce2d314cce2aff9d59064351349f Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Wed, 9 Oct 2024 20:57:03 +0800 Subject: [PATCH] feat(pkg/policy): improve TopKeys function handling (#1730) Signed-off-by: yxxhero --- pkg/policy/checker.go | 7 +++++-- pkg/policy/checker_test.go | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/policy/checker.go b/pkg/policy/checker.go index eea0a0cd..b493d513 100644 --- a/pkg/policy/checker.go +++ b/pkg/policy/checker.go @@ -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) } diff --git a/pkg/policy/checker_test.go b/pkg/policy/checker_test.go index b867b4ad..7c28555c 100644 --- a/pkg/policy/checker_test.go +++ b/pkg/policy/checker_test.go @@ -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 {