Skip to content

Commit 1ea1035

Browse files
committed
Persistent config, -cleanAll flag, better ux
1 parent a0adf9d commit 1ea1035

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

fidy.exe

9 KB
Binary file not shown.

main.go

+33-29
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func main() {
6666
exclude := flag.String("exclude", "", "Comma-separated list of extensions to exclude")
6767
verbose := flag.Bool("verbose", false, "Enable verbose output")
6868
dryrun := flag.Bool("dryrun", false, "Simulate the file organization without doing any actual changes")
69+
cleanAll := flag.Bool("cleanAll", false, "Delete all the empty folders and sub-folders in the specified directory after organizing files.")
6970

7071
flag.Parse()
7172

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

192-
fmt.Println("\nFiles organized by extension in", *dir)
194+
fmt.Printf("\nFiles organized by extension in %s \n", *dir)
193195
}
194196

195-
// if *cleanAll {
196-
// cleanEmptyDirs(".")
197-
// }
197+
if *cleanAll {
198+
cleanEmptyDirs(*dir, *dryrun)
199+
}
198200
}
199201

200-
// func cleanEmptyDirs(dir string) {
201-
// files, err := os.ReadDir(dir)
202-
// if err != nil {
203-
// fmt.Println("Error reading directory:", err)
204-
// return
205-
// }
206-
207-
// for _, file := range files {
208-
// if file.IsDir() {
209-
// subDir := filepath.Join(dir, file.Name())
210-
// cleanEmptyDirs(subDir)
211-
// subFiles, err := os.ReadDir(subDir)
212-
// if err != nil {
213-
// fmt.Println("Error reading subdirectory:", err)
214-
// continue
215-
// }
216-
// if len(subFiles) == 0 {
217-
// fmt.Printf("Deleting empty directory: %s\n", subDir)
218-
// if err := os.Remove(subDir); err != nil {
219-
// fmt.Println("Error deleting directory:", err)
220-
// }
221-
// }
222-
// }
223-
// }
224-
// }
202+
func cleanEmptyDirs(dir string, dryrun bool) {
203+
files, err := os.ReadDir(dir)
204+
if err != nil {
205+
fmt.Println("Error reading directory:", err)
206+
return
207+
}
208+
209+
for _, file := range files {
210+
if file.IsDir() {
211+
subDir := filepath.Join(dir, file.Name())
212+
cleanEmptyDirs(subDir, dryrun)
213+
subFiles, err := os.ReadDir(subDir)
214+
if err != nil {
215+
fmt.Println("Error reading subdirectory:", err)
216+
continue
217+
}
218+
if len(subFiles) == 0 {
219+
fmt.Printf("Deleting empty directory: %s\n", subDir)
220+
if !dryrun {
221+
if err := os.Remove(subDir); err != nil {
222+
fmt.Println("Error deleting directory:", err)
223+
}
224+
}
225+
}
226+
}
227+
}
228+
}

0 commit comments

Comments
 (0)