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

@ -7,10 +7,20 @@ package util
import (
"encoding/hex"
"regexp"
"slices"
"strings"
"github.com/arduino/go-paths-helper"
)
var (
Comment = `#`
regFilter = ToRegexRepl([]string{
`\s*` + Comment + `.*`, ``,
`(?m)^(?:[\t\s]*(?:\r?\n|\r))+`, ``,
})
)
type RegexReplList []RegexRepl
type RegexRepl struct {
@ -40,7 +50,7 @@ func (rr RegexReplList) Replace(str string) string {
return str
}
// DecodeHexInString decode and replace all hex value in a given string constitued of "key=value".
// DecodeHexInString decode and replace all hex value in a given string of "key=value" format.
func DecodeHexInString(str string) string {
toDecode := []string{"name", "comm", "profile"}
for _, name := range toDecode {
@ -94,3 +104,37 @@ func CopyTo(src *paths.Path, dst *paths.Path) error {
}
return nil
}
// Filter out comments and empty line from a string
func Filter(src string) string {
return regFilter.Replace(src)
}
// ReadFile read a file and return its content as a string.
func ReadFile(path *paths.Path) (string, error) {
content, err := path.ReadFile()
if err != nil {
return "", err
}
return string(content), nil
}
// MustReadFile read a file and return its content as a string. Panic if an error occurs.
func MustReadFile(path *paths.Path) string {
content, err := path.ReadFile()
if err != nil {
panic(err)
}
return string(content)
}
// MustReadFileAsLines read a file and return its content as a slice of string.
// It panics if an error occurs and filter out comments and empty lines.
func MustReadFileAsLines(path *paths.Path) []string {
res := strings.Split(Filter(MustReadFile(path)), "\n")
if slices.Contains(res, "") {
idx := slices.Index(res, "")
res = slices.Delete(res, idx, idx+1)
}
return res
}

View file

@ -151,3 +151,66 @@ func TestCopyTo(t *testing.T) {
})
}
}
func Test_Filter(t *testing.T) {
tests := []struct {
name string
src string
want string
}{
{
name: "comment",
src: "# comment",
want: "",
},
{
name: "comment with space",
src: " # comment",
want: "",
},
{
name: "no comment",
src: "no comment",
want: "no comment",
},
{
name: "no comment # comment",
src: "no comment # comment",
want: "no comment",
},
{
name: "empty",
src: `
`,
want: ``,
},
{
name: "main",
src: `
# Common profile flags definition for all distributions
# File format: one profile by line using the format: '<profile> <flags>'
bwrap attach_disconnected,mediate_deleted,complain
bwrap-app attach_disconnected,complain
akonadi_akonotes_resource complain # Dev
gnome-disks complain
`,
want: `bwrap attach_disconnected,mediate_deleted,complain
bwrap-app attach_disconnected,complain
akonadi_akonotes_resource complain
gnome-disks complain
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotLine := Filter(tt.src)
if gotLine != tt.want {
t.Errorf("FilterComment() got = |%v|, want |%v|", gotLine, tt.want)
}
})
}
}