Skip to content

Commit

Permalink
Persistent config, -cleanAll flag, better ux
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmit0920 committed Jul 16, 2024
1 parent a0adf9d commit 1ea1035
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
Binary file modified fidy.exe
Binary file not shown.
62 changes: 33 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func main() {
exclude := flag.String("exclude", "", "Comma-separated list of extensions to exclude")
verbose := flag.Bool("verbose", false, "Enable verbose output")
dryrun := flag.Bool("dryrun", false, "Simulate the file organization without doing any actual changes")
cleanAll := flag.Bool("cleanAll", false, "Delete all the empty folders and sub-folders in the specified directory after organizing files.")

flag.Parse()

Expand Down Expand Up @@ -114,6 +115,7 @@ func main() {
fmt.Println(" -exclude <exts> : Comma-separated list of extensions to exclude.")
fmt.Println(" -verbose : Enable verbose output.")
fmt.Println(" -dryrun : Simulate the file organization without doing any actual changes.")
fmt.Println(" -cleanAll : Delete all the empty folders and sub-folders in the specified directory after organizing files.")
fmt.Println("")
return
}
Expand Down Expand Up @@ -189,36 +191,38 @@ func main() {
}
}

fmt.Println("\nFiles organized by extension in", *dir)
fmt.Printf("\nFiles organized by extension in %s \n", *dir)
}

// if *cleanAll {
// cleanEmptyDirs(".")
// }
if *cleanAll {
cleanEmptyDirs(*dir, *dryrun)
}
}

// func cleanEmptyDirs(dir string) {
// files, err := os.ReadDir(dir)
// if err != nil {
// fmt.Println("Error reading directory:", err)
// return
// }

// for _, file := range files {
// if file.IsDir() {
// subDir := filepath.Join(dir, file.Name())
// cleanEmptyDirs(subDir)
// subFiles, err := os.ReadDir(subDir)
// if err != nil {
// fmt.Println("Error reading subdirectory:", err)
// continue
// }
// if len(subFiles) == 0 {
// fmt.Printf("Deleting empty directory: %s\n", subDir)
// if err := os.Remove(subDir); err != nil {
// fmt.Println("Error deleting directory:", err)
// }
// }
// }
// }
// }
func cleanEmptyDirs(dir string, dryrun bool) {
files, err := os.ReadDir(dir)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}

for _, file := range files {
if file.IsDir() {
subDir := filepath.Join(dir, file.Name())
cleanEmptyDirs(subDir, dryrun)
subFiles, err := os.ReadDir(subDir)
if err != nil {
fmt.Println("Error reading subdirectory:", err)
continue
}
if len(subFiles) == 0 {
fmt.Printf("Deleting empty directory: %s\n", subDir)
if !dryrun {
if err := os.Remove(subDir); err != nil {
fmt.Println("Error deleting directory:", err)
}
}
}
}
}
}

0 comments on commit 1ea1035

Please sign in to comment.