Don't overwrite files unless forced
This commit is contained in:
parent
d75e330a26
commit
8284d77d7e
31
main.go
31
main.go
@ -17,6 +17,7 @@ type config struct {
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user