From 1333ec202502f93bfe67c1416f0975c4301cf3ca Mon Sep 17 00:00:00 2001 From: Alexandre Pujol Date: Tue, 28 May 2024 18:07:32 +0100 Subject: [PATCH] feat(aa): cleanup rules methods. --- pkg/aa/resolve.go | 4 ++-- pkg/aa/rules.go | 34 ++++++++++++---------------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/pkg/aa/resolve.go b/pkg/aa/resolve.go index 85426340..9a246746 100644 --- a/pkg/aa/resolve.go +++ b/pkg/aa/resolve.go @@ -159,7 +159,7 @@ func (f *AppArmorProfileFile) resolveInclude(include *Include) error { } // Insert iFile in the place of include in the current file - index := f.Preamble.IndexOf(include) - f.Preamble = f.Preamble.Insert(index, includeCache[include].Preamble...) + index := f.Preamble.Index(include) + f.Preamble = f.Preamble.Replace(index, includeCache[include].Preamble...) return nil } diff --git a/pkg/aa/rules.go b/pkg/aa/rules.go index 895d5f28..4bd1b61e 100644 --- a/pkg/aa/rules.go +++ b/pkg/aa/rules.go @@ -51,7 +51,8 @@ func (r Rules) String() string { return renderTemplate("rules", r) } -func (r Rules) IndexOf(rule Rule) int { +// Index returns the index of the first occurrence of rule rin r, or -1 if not present. +func (r Rules) Index(rule Rule) int { for idx, rr := range r { if rr.Kind() == rule.Kind() && rr.Equals(rule) { return idx @@ -60,31 +61,20 @@ func (r Rules) IndexOf(rule Rule) int { return -1 } -func (r Rules) Contains(rule Rule) bool { - return r.IndexOf(rule) != -1 +// Replace replaces the elements r[i] by the given rules, and returns the +// modified slice. +func (r Rules) Replace(i int, rules ...Rule) Rules { + return append(r[:i], append(rules, r[i+1:]...)...) } -func (r Rules) Add(rule Rule) Rules { - if r.Contains(rule) { - return r - } - return append(r, rule) +// Insert inserts the rules into r at index i, returning the modified slice. +func (r Rules) Insert(i int, rules ...Rule) Rules { + return append(r[:i], append(rules, r[i:]...)...) } -func (r Rules) Remove(rule Rule) Rules { - idx := r.IndexOf(rule) - if idx == -1 { - return r - } - return append(r[:idx], r[idx+1:]...) -} - -func (r Rules) Insert(idx int, rules ...Rule) Rules { - return append(r[:idx], append(rules, r[idx+1:]...)...) -} - -func (r Rules) Sort() Rules { - return r +// Delete removes the elements r[i] from r, returning the modified slice. +func (r Rules) Delete(i int) Rules { + return append(r[:i], r[i+1:]...) } func (r Rules) DeleteKind(kind string) Rules {