From 8284d77d7e4fa00aa1f9415cb4c61f08b06faa08 Mon Sep 17 00:00:00 2001 From: MrLetsplay2003 Date: Thu, 13 Jul 2023 18:15:42 +0000 Subject: [PATCH] Don't overwrite files unless forced --- main.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 877bde6..2b406cc 100644 --- a/main.go +++ b/main.go @@ -14,9 +14,10 @@ type file struct { } type config struct { - Files []file - VariableRegex string - RegexGroup int + Files []file + VariableRegex string + RegexGroup int + ForceOverwrite bool } var defaultVariableRegex = `\$\{([a-zA-Z0-9_-]+)\}` @@ -62,6 +63,19 @@ func filterFile(file file, regex regexp.Regexp, regexGroup int) error { return nil } +func fileExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + + if os.IsNotExist(err) { + return false, nil + } + + return false, err +} + func filterFiles(config config) error { var regexString string if len(config.VariableRegex) == 0 { @@ -85,7 +99,19 @@ func filterFiles(config config) error { } for _, file := range config.Files { - log.Println("Filtering file:", file) + if !config.ForceOverwrite { + exists, err := fileExists(file.TargetPath) + if err != nil { + return err + } + + if exists { + log.Println("Skipping file:", file.Path, "->", file.TargetPath, "(already exists)") + continue + } + } + + log.Println("Filtering file:", file.Path, "->", file.TargetPath) err := filterFile(file, *regex, regexGroup) if err != nil { return err @@ -97,6 +123,7 @@ func filterFiles(config config) error { func main() { configPath := flag.String("config", "config.json", "Path to config file") + force := flag.Bool("force", false, "Forcibly overwrite existing files (equivalent to setting forceOverwrite to true in the config)") flag.Parse() config, err := loadConfig(*configPath) @@ -104,6 +131,8 @@ func main() { log.Fatalln("Failed to load config:", err) } + config.ForceOverwrite = config.ForceOverwrite || *force + err = filterFiles(*config) if err != nil { log.Fatalln(err)