From 25652f0d876c02f0183ff05cea9781b8f7cc8d08 Mon Sep 17 00:00:00 2001 From: Felipe Martin Date: Mon, 5 Aug 2019 19:30:41 +0200 Subject: [PATCH] Check if the path to rename is too broad as safety measure --- main.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/main.go b/main.go index 31e81be..29d3dce 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( const defaultAllowedExtensions = "jpg,jpeg,png,gif,avi,mp4,mov" var extensions = flag.String("extensions", defaultAllowedExtensions, "Comma separated extensions to allow") +var force = flag.Bool("force", false, "Force execution without safety checks") func stringInSlice(a string, list []string) bool { for _, b := range list { @@ -50,6 +51,18 @@ func main() { panic(err) } + if !*force { + // Count the number of paths separators and don't allow executing + // if the path is too close to the root folder without the force + // flag + sublevels := strings.Split(path, string(filepath.Separator))[1:] + if len(sublevels) < 3 { + fmt.Printf("%s seems too broad.\n", path) + fmt.Println("If you're sure use the -force flag.") + os.Exit(1) + } + } + fmt.Printf("Walking by %s...\n", path) err2 := filepath.Walk(path, readDir)