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 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{} flags := []string{}
matches := regFlags.FindStringSubmatch(profile) matches := regFlags.FindStringSubmatch(profile)
if len(matches) != 0 { if len(matches) != 0 {

View file

@ -7,6 +7,7 @@ package builder
import ( import (
"fmt" "fmt"
"github.com/arduino/go-paths-helper"
"github.com/roddhjav/apparmor.d/pkg/prebuild/cfg" "github.com/roddhjav/apparmor.d/pkg/prebuild/cfg"
) )
@ -21,7 +22,20 @@ var (
// Main directive interface // Main directive interface
type Builder interface { type Builder interface {
cfg.BaseInterface 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) { func Register(names ...string) {
@ -37,3 +51,15 @@ func Register(names ...string) {
func RegisterBuilder(d Builder) { func RegisterBuilder(d Builder) {
Builders[d.Name()] = d 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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 { if (err != nil) != tt.wantErr {
t.Errorf("Builder.Apply() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Builder.Apply() error = %v, wantErr %v", err, tt.wantErr)
return 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 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) matches := regFlags.FindStringSubmatch(profile)
if len(matches) == 0 { if len(matches) == 0 {
return profile, nil 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 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 := aa.DefaultTunables()
p.ParseVariables(profile) p.ParseVariables(profile)
p.ResolveAttachments() p.ResolveAttachments()

View file

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