feat(aa): add new formatting methods to the rule interface.

This commit is contained in:
Alexandre Pujol 2024-06-29 22:20:42 +01:00
parent d9bbdb77fa
commit 8b24f3521d
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
2 changed files with 44 additions and 9 deletions

View file

@ -13,10 +13,8 @@ type Base struct {
Comment string Comment string
NoNewPrivs bool NoNewPrivs bool
FileInherit bool FileInherit bool
Prefix string
Padding string
Suffix string
Optional bool Optional bool
Paddings []string
} }
func newBase(rule rule) Base { func newBase(rule rule) Base {
@ -79,13 +77,27 @@ func newBaseFromLog(log map[string]string) Base {
} }
} }
func (r Base) Padding(i int) string {
if i >= len(r.Paddings) {
return ""
}
return r.Paddings[i]
}
func (r *Base) merge(other Base) bool { func (r *Base) merge(other Base) bool {
r.NoNewPrivs = r.NoNewPrivs || other.NoNewPrivs
r.FileInherit = r.FileInherit || other.FileInherit
r.Optional = r.Optional || other.Optional
if other.Comment != "" { if other.Comment != "" {
r.Comment += " " + other.Comment r.Comment += " " + other.Comment
} }
return true return true
} }
func (r Base) addLine(other Rule) bool {
return false
}
type Qualifier struct { type Qualifier struct {
Audit bool Audit bool
AccessType string AccessType string
@ -109,3 +121,22 @@ func (r Qualifier) Compare(o Qualifier) int {
func (r Qualifier) Equal(o Qualifier) bool { func (r Qualifier) Equal(o Qualifier) bool {
return r.Audit == o.Audit && r.AccessType == o.AccessType return r.Audit == o.Audit && r.AccessType == o.AccessType
} }
func (r Qualifier) getLenAudit() int {
return length("audit", r.Audit)
}
func (r Qualifier) getLenAccess() int {
lenAccess := 0
if r.AccessType != "" {
lenAccess = length("", r.AccessType)
}
return lenAccess
}
func (r Qualifier) setPaddings(max []int) []string {
return setPaddings(max,
[]string{"audit", ""},
[]any{r.Audit, r.AccessType},
)
}

View file

@ -43,6 +43,10 @@ type Rule interface {
Validate() error // Validate the rule. Return an error if the rule is invalid Validate() error // Validate the rule. Return an error if the rule is invalid
Compare(other Rule) int // Compare two rules. Return 0 if they are identical Compare(other Rule) int // Compare two rules. Return 0 if they are identical
Merge(other Rule) bool // Merge rules of same kind together. Return true if merged Merge(other Rule) bool // Merge rules of same kind together. Return true if merged
Padding(i int) string // Padding for rule items at index i
Lengths() []int // Length of each item in the rule
setPaddings(max []int) // Set paddings for each item in the rule
addLine(other Rule) bool // Check either a new line should be added before the rule
} }
type Rules []Rule type Rules []Rule