feat(prebuild): add builder opt to build tasks.

This commit is contained in:
Alexandre Pujol 2024-05-25 22:32:10 +01:00
parent 02e3334949
commit 2dd6046697
No known key found for this signature in database
GPG Key ID: C5469996F0DF68EC
9 changed files with 38 additions and 13 deletions

View File

@ -30,6 +30,6 @@ func init() {
})
}
func (b ABI3) Apply(profile string) (string, error) {
func (b ABI3) Apply(opt *Option, profile string) (string, error) {
return regAbi4To3.Replace(profile), nil
}

View File

@ -30,7 +30,7 @@ func init() {
})
}
func (b Complain) Apply(profile string) (string, error) {
func (b Complain) Apply(opt *Option, profile string) (string, error) {
flags := []string{}
matches := regFlags.FindStringSubmatch(profile)
if len(matches) != 0 {

View File

@ -7,6 +7,7 @@ package builder
import (
"fmt"
"github.com/arduino/go-paths-helper"
"github.com/roddhjav/apparmor.d/pkg/prebuild/cfg"
)
@ -21,7 +22,20 @@ var (
// Main directive interface
type Builder interface {
cfg.BaseInterface
Apply(profile string) (string, error)
Apply(opt *Option, profile string) (string, error)
}
// Builder options
type Option struct {
Name string
File *paths.Path
}
func NewOption(file *paths.Path) *Option {
return &Option{
Name: file.Base(),
File: file,
}
}
func Register(names ...string) {
@ -37,3 +51,15 @@ func Register(names ...string) {
func RegisterBuilder(d Builder) {
Builders[d.Name()] = d
}
func Run(file *paths.Path, profile string) (string, error) {
var err error
opt := NewOption(file)
for _, b := range Builds {
profile, err = b.Apply(opt, profile)
if err != nil {
return "", err
}
}
return profile, nil
}

View File

@ -238,7 +238,8 @@ func TestBuilder_Apply(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.b.Apply(tt.profile)
opt := &Option{}
got, err := tt.b.Apply(opt, tt.profile)
if (err != nil) != tt.wantErr {
t.Errorf("Builder.Apply() error = %v, wantErr %v", err, tt.wantErr)
return

View File

@ -31,6 +31,6 @@ func init() {
})
}
func (b Dev) Apply(profile string) (string, error) {
func (b Dev) Apply(opt *Option, profile string) (string, error) {
return regDev.Replace(profile), nil
}

View File

@ -24,7 +24,7 @@ func init() {
})
}
func (b Enforce) Apply(profile string) (string, error) {
func (b Enforce) Apply(opt *Option, profile string) (string, error) {
matches := regFlags.FindStringSubmatch(profile)
if len(matches) == 0 {
return profile, nil

View File

@ -28,6 +28,6 @@ func init() {
})
}
func (b FullSystemPolicy) Apply(profile string) (string, error) {
func (b FullSystemPolicy) Apply(opt *Option, profile string) (string, error) {
return regFullSystemPolicy.Replace(profile), nil
}

View File

@ -29,7 +29,7 @@ func init() {
})
}
func (b Userspace) Apply(profile string) (string, error) {
func (b Userspace) Apply(opt *Option, profile string) (string, error) {
p := aa.DefaultTunables()
p.ParseVariables(profile)
p.ResolveAttachments()

View File

@ -83,12 +83,10 @@ func Build() error {
if err != nil {
return err
}
for _, b := range builder.Builds {
profile, err = b.Apply(profile)
profile, err = builder.Run(file, profile)
if err != nil {
return err
}
}
profile, err = directive.Run(file, profile)
if err != nil {
return err