From e3545cc3bb5b03d964a6bd0db8c07794fc70ad77 Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Sun, 10 Mar 2024 15:53:25 +0000 Subject: [PATCH] feat(aa-log): improve the regex helper type. --- pkg/integration/suite.go | 4 +--- pkg/logs/loggers.go | 4 +--- pkg/prebuild/directives.go | 4 +--- pkg/util/tools.go | 9 +++++++++ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/integration/suite.go b/pkg/integration/suite.go index f0e50416..2cba683c 100644 --- a/pkg/integration/suite.go +++ b/pkg/integration/suite.go @@ -71,9 +71,7 @@ func (t *TestSuite) Write(path *paths.Path) error { `{{`, `{{ `, `}}`, ` }}`, }) - for _, aa := range regClean { - res = aa.Regex.ReplaceAllLiteralString(res, aa.Repl) - } + res = regClean.Replace(res) _, err = file.WriteString("---\n" + res) return err } diff --git a/pkg/logs/loggers.go b/pkg/logs/loggers.go index 39001a7b..0848944c 100644 --- a/pkg/logs/loggers.go +++ b/pkg/logs/loggers.go @@ -51,9 +51,7 @@ func GetApparmorLogs(file io.Reader, profile string) []string { // Clean & remove doublon in logs res = util.DecodeHexInString(res) - for _, aa := range regCleanLogs { - res = aa.Regex.ReplaceAllLiteralString(res, aa.Repl) - } + res = regCleanLogs.Replace(res) logs := strings.Split(res, "\n") return util.RemoveDuplicate(logs) } diff --git a/pkg/prebuild/directives.go b/pkg/prebuild/directives.go index 1b49c08a..fd45bc4c 100644 --- a/pkg/prebuild/directives.go +++ b/pkg/prebuild/directives.go @@ -184,9 +184,7 @@ func DirectiveStack(file *paths.Path, profile string) string { panic(fmt.Sprintf("No profile found in %s", name)) } stackedRules := m[1] - for _, aa := range regCleanStakedRules { - stackedRules = aa.Regex.ReplaceAllLiteralString(stackedRules, aa.Repl) - } + stackedRules = regCleanStakedRules.Replace(stackedRules) res += " # Stacked profile: " + name + "\n" + stackedRules + "\n" } diff --git a/pkg/util/tools.go b/pkg/util/tools.go index 68392084..d7c3072e 100644 --- a/pkg/util/tools.go +++ b/pkg/util/tools.go @@ -9,6 +9,8 @@ import ( "regexp" ) +type RegexReplList []RegexRepl + type RegexRepl struct { Regex *regexp.Regexp Repl string @@ -58,3 +60,10 @@ func ToRegexRepl(in []string) []RegexRepl { } return out } + +func (rr RegexReplList) Replace(str string) string { + for _, aa := range rr { + str = aa.Regex.ReplaceAllLiteralString(str, aa.Repl) + } + return str +}