@@ -26,6 +26,9 @@ func showResults(ch chan string, limit *int) {
2626 }
2727}
2828
29+ // dirChecker check how many directories are being searched
30+ // once the number of directories hits zero, then all files & dirs have been
31+ // checked and the program can be closed
2932func dirChecker (in chan int , work chan string ) {
3033 n := 1
3134 for i := range in {
@@ -37,6 +40,7 @@ func dirChecker(in chan int, work chan string) {
3740 }
3841}
3942
43+ // createWorkerPool create all workers and close our results channel after they stop searching
4044func createWorkerPool (p string , in chan string , failover chan string , results chan string , cnt chan int , w int ) {
4145 var wg sync.WaitGroup
4246 for i := 0 ; i < w ; i ++ {
@@ -46,16 +50,17 @@ func createWorkerPool(p string, in chan string, failover chan string, results ch
4650 search (p , in , failover , results , cnt )
4751 }()
4852 }
53+
4954 wg .Wait ()
5055 close (results )
5156}
5257
58+ // search looks through files and directories for a given substring
5359func search (pattern string , in chan string , failover chan string , out chan string , cnt chan int ) {
5460 for path := range in {
5561 items , err := os .ReadDir (path )
5662 if err != nil {
57- fmt .Println ("Error reading the directory" , path )
58- fmt .Println (err )
63+ slog .Error ("failed to reading the directory" , "path" , path , "err" , err )
5964 cnt <- - 1
6065 }
6166 ItemSearch:
@@ -74,6 +79,7 @@ func search(pattern string, in chan string, failover chan string, out chan strin
7479 case failover <- subPath :
7580 }
7681 }
82+
7783 //Always check if the name of the thing matches pattern, including directory names
7884 if strings .Contains (item .Name (), pattern ) {
7985 //subPath is repeated but no point in creating an allocation if not required
0 commit comments