From 7793113f2ffc6ba9120872f28f942cd94b7b3543 Mon Sep 17 00:00:00 2001 From: ah8ad3 Date: Mon, 15 Apr 2024 21:28:09 +0330 Subject: [PATCH] Ignore empty documents. Signed-off-by: ah8ad3 Add '---' to IsEmptyYamlDocument function as a condition to ignore empty yaml files. Signed-off-by: ah8ad3 refactor: add gendoc for IsEmptyYamlDocument Signed-off-by: ah8ad3 Update comment Signed-off-by: ah8ad3 Fix: gofmt document Signed-off-by: ah8ad3 --- pkg/utils/yaml.go | 5 ++++- testcases/manifests/empty.yaml | 1 + testcases/manifests/empty_with_document.yaml | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 testcases/manifests/empty.yaml create mode 100644 testcases/manifests/empty_with_document.yaml diff --git a/pkg/utils/yaml.go b/pkg/utils/yaml.go index 0f90d017..d768c997 100644 --- a/pkg/utils/yaml.go +++ b/pkg/utils/yaml.go @@ -27,10 +27,13 @@ func SplitYamlDocuments(fileBytes Document) ([]Document, error) { } // IsEmptyYamlDocument checks if a yaml document is empty (contains only comments) +// +// Returns true for comment-only single documents, and strings with multiple documents +// where all docs are comment-only. func IsEmptyYamlDocument(document Document) bool { for _, line := range strings.Split(string(document), "\n") { line := strings.TrimSpace(line) - if line != "" && !strings.HasPrefix(line, "#") { + if line != "" && !strings.HasPrefix(line, "#") && line != "---" { return false } } diff --git a/testcases/manifests/empty.yaml b/testcases/manifests/empty.yaml new file mode 100644 index 00000000..73b314ff --- /dev/null +++ b/testcases/manifests/empty.yaml @@ -0,0 +1 @@ +--- \ No newline at end of file diff --git a/testcases/manifests/empty_with_document.yaml b/testcases/manifests/empty_with_document.yaml new file mode 100644 index 00000000..1bf4f65c --- /dev/null +++ b/testcases/manifests/empty_with_document.yaml @@ -0,0 +1,5 @@ +# Some document +# Some document +# Some document +# Some document +--- \ No newline at end of file