feat(build): simplify some internal tooling.

This commit is contained in:
Alexandre Pujol 2024-04-02 17:48:03 +01:00
parent 791459e39a
commit 1915fa5175
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
14 changed files with 140 additions and 121 deletions

View file

@ -6,22 +6,9 @@ package cfg
import (
"strings"
)
// Filter out comments from a text configuration file
func filterComment(line string) (string, bool) {
if strings.HasPrefix(line, "#") || line == "" {
return "", true
}
if strings.Contains(line, "#") {
line = strings.Split(line, "#")[0]
line = strings.TrimSpace(line)
if line == "" {
return "", true
}
}
return line, false
}
"github.com/roddhjav/apparmor.d/pkg/util"
)
type Flagger struct{}
@ -32,12 +19,8 @@ func (f Flagger) Read(name string) map[string][]string {
return res
}
lines, _ := path.ReadFileAsLines()
lines := util.MustReadFileAsLines(path)
for _, line := range lines {
line, next := filterComment(line)
if next {
continue
}
manifest := strings.Split(line, " ")
profile := manifest[0]
flags := []string{}
@ -52,21 +35,11 @@ func (f Flagger) Read(name string) map[string][]string {
type Ignorer struct{}
func (i Ignorer) Read(name string) []string {
res := []string{}
path := IgnoreDir.Join(name + ".ignore")
if !path.Exist() {
return res
return []string{}
}
lines, _ := path.ReadFileAsLines()
for _, line := range lines {
line, next := filterComment(line)
if next {
continue
}
res = append(res, line)
}
return res
return util.MustReadFileAsLines(path)
}
type Overwriter struct {
@ -75,19 +48,11 @@ type Overwriter struct {
// Get the list of upstream profiles to overwrite from dist/overwrite
func (o Overwriter) Get() []string {
res := []string{}
lines, err := DistDir.Join("overwrite").ReadFileAsLines()
if err != nil {
panic(err)
path := DistDir.Join("overwrite")
if !path.Exist() {
return []string{}
}
for _, line := range lines {
line, next := filterComment(line)
if next {
continue
}
res = append(res, line)
}
return res
return util.MustReadFileAsLines(path)
}
// Overwrite upstream profile for APT: rename our profile & hide upstream

View file

@ -11,51 +11,6 @@ import (
"github.com/arduino/go-paths-helper"
)
func Test_filterComment(t *testing.T) {
tests := []struct {
name string
line string
wantLine string
wantNext bool
}{
{
name: "comment",
line: "# comment",
wantLine: "",
wantNext: true,
},
{
name: "comment with space",
line: " # comment",
wantLine: "",
wantNext: true,
},
{
name: "no comment",
line: "no comment",
wantLine: "no comment",
wantNext: false,
},
{
name: "no comment # comment",
line: "no comment # comment",
wantLine: "no comment",
wantNext: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotLine, gotNext := filterComment(tt.line)
if gotLine != tt.wantLine {
t.Errorf("filterComment() got = %v, want %v", gotLine, tt.wantLine)
}
if gotNext != tt.wantNext {
t.Errorf("filterComment() got1 = %v, want %v", gotNext, tt.wantNext)
}
})
}
}
func TestFlagger_Read(t *testing.T) {
tests := []struct {
name string