Implement filtering

This commit is contained in:
MrLetsplay 2023-07-12 18:54:17 +00:00
parent 040d3bbc10
commit d75e330a26
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg

22
main.go
View File

@ -8,8 +8,13 @@ import (
"regexp" "regexp"
) )
type file struct {
Path string
TargetPath string
}
type config struct { type config struct {
Files []string Files []file
VariableRegex string VariableRegex string
RegexGroup int RegexGroup int
} }
@ -31,8 +36,13 @@ func loadConfig(path string) (*config, error) {
return &config, nil return &config, nil
} }
func filterFile(path string, regex regexp.Regexp, regexGroup int) error { func filterFile(file file, regex regexp.Regexp, regexGroup int) error {
data, err := os.ReadFile(path) data, err := os.ReadFile(file.Path)
if err != nil {
return err
}
stat, err := os.Stat(file.Path)
if err != nil { if err != nil {
return err return err
} }
@ -41,11 +51,13 @@ func filterFile(path string, regex regexp.Regexp, regexGroup int) error {
text = regex.ReplaceAllStringFunc(text, func(s string) string { text = regex.ReplaceAllStringFunc(text, func(s string) string {
matches := regex.FindStringSubmatch(text) matches := regex.FindStringSubmatch(text)
log.Println(matches)
return os.Getenv(matches[regexGroup]) return os.Getenv(matches[regexGroup])
}) })
log.Println(text) err = os.WriteFile(file.TargetPath, []byte(text), stat.Mode())
if err != nil {
return err
}
return nil return nil
} }