refractor: rename some path util function.
This commit is contained in:
parent
ebdeef152c
commit
982c2c66aa
13 changed files with 87 additions and 82 deletions
|
|
@ -7,10 +7,6 @@ package util
|
|||
import (
|
||||
"encoding/hex"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/roddhjav/apparmor.d/pkg/paths"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -67,61 +63,7 @@ func DecodeHexInString(str string) string {
|
|||
return str
|
||||
}
|
||||
|
||||
// CopyTo recursivelly copy all files from a source path to a destination path.
|
||||
func CopyTo(src *paths.Path, dst *paths.Path) error {
|
||||
files, err := src.ReadDirRecursiveFiltered(nil,
|
||||
paths.FilterOutDirectories(),
|
||||
paths.FilterOutNames("README.md"),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, file := range files {
|
||||
destination, err := file.RelFrom(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
destination = dst.JoinPath(destination)
|
||||
if err := destination.Parent().MkdirAll(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := file.CopyTo(destination); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
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