mirror of
https://github.com/roddhjav/apparmor.d.git
synced 2024-11-14 23:43:56 +01:00
build: improve internal directive tool.
This commit is contained in:
parent
6f5604d59d
commit
05a489e021
@ -61,11 +61,29 @@ func NewOption(file *paths.Path, match []string) *Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Clean the selected directive from profile.
|
||||
// Clean removes selected directive line from input string.
|
||||
// Useful to remove directive text applied on some condition only
|
||||
func (o *Option) Clean(profile string) string {
|
||||
reg := regexp.MustCompile(`\s*` + Keyword + o.Name + ` .*$`)
|
||||
return strings.Replace(profile, o.Raw, reg.ReplaceAllString(o.Raw, ""), 1)
|
||||
func (o *Option) Clean(input string) string {
|
||||
return strings.Replace(input, o.Raw, o.cleanKeyword(o.Raw), 1)
|
||||
}
|
||||
|
||||
// cleanKeyword removes the dirextive keywork (#aa:...) from the input string
|
||||
func (o *Option) cleanKeyword(input string) string {
|
||||
reg := regexp.MustCompile(`\s*` + Keyword + o.Name + `( .*)?$`)
|
||||
return reg.ReplaceAllString(input, "")
|
||||
}
|
||||
|
||||
// Check if the directive is inline or if it is a paragraph
|
||||
func (o *Option) IsInline() bool {
|
||||
inline := true
|
||||
tmp := strings.Split(o.Raw, Keyword)
|
||||
if len(tmp) >= 1 {
|
||||
left := strings.TrimSpace(tmp[0])
|
||||
if len(left) == 0 {
|
||||
inline = false
|
||||
}
|
||||
}
|
||||
return inline
|
||||
}
|
||||
|
||||
func RegisterDirective(d Directive) {
|
||||
|
@ -49,16 +49,7 @@ func filter(only bool, opt *Option, profile string) (string, error) {
|
||||
return opt.Clean(profile), nil
|
||||
}
|
||||
|
||||
inline := true
|
||||
tmp := strings.Split(opt.Raw, Keyword)
|
||||
if len(tmp) >= 1 {
|
||||
left := strings.TrimSpace(tmp[0])
|
||||
if len(left) == 0 {
|
||||
inline = false
|
||||
}
|
||||
}
|
||||
|
||||
if inline {
|
||||
if opt.IsInline() {
|
||||
profile = strings.Replace(profile, opt.Raw, "", -1)
|
||||
} else {
|
||||
regRemoveParagraph := regexp.MustCompile(`(?s)` + opt.Raw + `\n.*?\n\n`)
|
||||
|
Loading…
Reference in New Issue
Block a user