feat(build): simplify some internal tooling.
This commit is contained in:
parent
791459e39a
commit
1915fa5175
14 changed files with 140 additions and 121 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue