feat(aa): add some rules methods.

This commit is contained in:
Alexandre Pujol 2024-05-26 18:05:15 +01:00
parent 92641e7e28
commit 2e043d4ec8
No known key found for this signature in database
GPG Key ID: C5469996F0DF68EC
4 changed files with 57 additions and 5 deletions

View File

@ -4,6 +4,8 @@
package aa package aa
import "fmt"
const tokCHANGEPROFILE = "change_profile" const tokCHANGEPROFILE = "change_profile"
func init() { func init() {

View File

@ -5,6 +5,7 @@
package aa package aa
import ( import (
"fmt"
"slices" "slices"
) )

View File

@ -4,6 +4,8 @@
package aa package aa
import "fmt"
const ( const (
tokRLIMIT = "rlimit" tokRLIMIT = "rlimit"
tokSET = "set" tokSET = "set"

View File

@ -51,10 +51,56 @@ func (r Rules) String() string {
return renderTemplate("rules", r) return renderTemplate("rules", r)
} }
func (r Rules) Get(filter string) Rules { func (r Rules) IndexOf(rule Rule) int {
for idx, rr := range r {
if rr.Kind() == rule.Kind() && rr.Equals(rule) {
return idx
}
}
return -1
}
func (r Rules) Contains(rule Rule) bool {
return r.IndexOf(rule) != -1
}
func (r Rules) Add(rule Rule) Rules {
if r.Contains(rule) {
return r
}
return append(r, rule)
}
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:]...)...)
}
func (r Rules) Sort() Rules {
return r
}
func (r Rules) DeleteKind(kind string) Rules {
res := make(Rules, 0) res := make(Rules, 0)
for _, rule := range r { for _, rule := range r {
if rule.Kind() == filter { if rule.Kind() != kind {
res = append(res, rule)
}
}
return res
}
func (r Rules) Filter(filter string) Rules {
res := make(Rules, 0)
for _, rule := range r {
if rule.Kind() != filter {
res = append(res, rule) res = append(res, rule)
} }
} }
@ -120,9 +166,10 @@ func toValues(rule string, key string, input string) ([]string, error) {
sep = " " sep = " "
} }
res := strings.Split(input, sep) res := strings.Split(input, sep)
for _, access := range res { for idx := range res {
if !slices.Contains(req, access) { res[idx] = strings.Trim(res[idx], `" `)
return nil, fmt.Errorf("unrecognized %s: %s", key, access) if !slices.Contains(req, res[idx]) {
return nil, fmt.Errorf("unrecognized %s: %s", key, res[idx])
} }
} }
slices.SortFunc(res, func(i, j string) int { slices.SortFunc(res, func(i, j string) int {