From 527541aaae3ee89a03f1064217bfe2c4699ee395 Mon Sep 17 00:00:00 2001 From: Vickey Brown Date: Thu, 30 Apr 2026 14:25:35 -0500 Subject: [PATCH] added urlencode and unescapenewlines functions --- pkg/utils/parse_arf_result.go | 15 +++++++++++++-- pkg/utils/parse_arf_result_test.go | 31 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pkg/utils/parse_arf_result.go b/pkg/utils/parse_arf_result.go index 985a58b739..ce5494176a 100644 --- a/pkg/utils/parse_arf_result.go +++ b/pkg/utils/parse_arf_result.go @@ -952,6 +952,14 @@ func toArrayByComma(format string) []string { return strings.Split(format, ",") } +func urlEncode(s string) string { + return url.PathEscape(s) +} + +func unescapeNewlines(s string) string { + return strings.ReplaceAll(s, "\\n", "\n") +} + // This function will take original remediation content, and a list of all values found in the configMap // It will processed and substitue the value in remediation content, and return processed Remediation content // The return will be Processed-Remdiation Content, Value-Used List, Un-Set List, and err if possible @@ -1013,8 +1021,11 @@ func processContent(preProcessedContent string, resultValues map[string]string) var valuesUsedList []string var valuesMissingList []string var valuesParsedList []string - t, err := template.New("").Option("missingkey=zero").Funcs(template.FuncMap{"toArrayByComma": toArrayByComma}). - Parse(preProcessedContent) + t, err := template.New("").Option("missingkey=zero").Funcs(template.FuncMap{ + "toArrayByComma": toArrayByComma, + "urlencode": urlEncode, + "unescapeNewlines": unescapeNewlines, + }).Parse(preProcessedContent) if err != nil { return preProcessedContent, valuesUsedList, valuesMissingList, errors.Wrap(err, "wrongly formatted remediation context: ") //Error creating template // Wrongly formatted remediation context } diff --git a/pkg/utils/parse_arf_result_test.go b/pkg/utils/parse_arf_result_test.go index bac5f85d92..ac133379fd 100644 --- a/pkg/utils/parse_arf_result_test.go +++ b/pkg/utils/parse_arf_result_test.go @@ -991,6 +991,37 @@ Server 3.fedora.pool.ntp.org` }) }) + Describe("Testing unescapeNewlines function", func() { + Context("When given strings with literal newline escape sequences", func() { + It("Should convert single newline", func() { + input := "line1\\nline2" + expected := "line1\nline2" + result := unescapeNewlines(input) + Expect(result).To(Equal(expected)) + }) + + It("Should convert multiple newlines", func() { + input := "line1\\nline2\\n\\nline3" + expected := "line1\nline2\n\nline3" + result := unescapeNewlines(input) + Expect(result).To(Equal(expected)) + }) + + It("Should handle strings without newlines", func() { + input := "single line" + expected := "single line" + result := unescapeNewlines(input) + Expect(result).To(Equal(expected)) + }) + + It("Should handle empty string", func() { + input := "" + expected := "" + result := unescapeNewlines(input) + Expect(result).To(Equal(expected)) + }) + }) + }) }) // printUniquePaths prints all unique paths within an XML document, starting from a given node.