Skip to content

Commit

Permalink
eliminating fsnotify
Browse files Browse the repository at this point in the history
  • Loading branch information
donuts-are-good committed Nov 18, 2023
1 parent fc17056 commit e3fabc0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 26 deletions.
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ module github.com/donuts-are-good/bearclaw/v2
go 1.19

require (
github.com/fsnotify/fsnotify v1.7.0
github.com/russross/blackfriday v1.6.0
mpldr.codes/ansi v1.5.0
)

require golang.org/x/sys v0.14.0 // indirect
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
mpldr.codes/ansi v1.5.0 h1:jAAtDMwU/DC7OPxmrVFpmM3RrSQDcMeLhuk6BuEoWZc=
mpldr.codes/ansi v1.5.0/go.mod h1:SYuKX0r6nxvySxMxzQX9frQaKLjEqKapjjg17euonMc=
78 changes: 59 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"path/filepath"
"sort"
"strings"

"github.com/fsnotify/fsnotify"
"time"
)

// init runs before main()
Expand Down Expand Up @@ -65,34 +64,75 @@ func recreateHeaderFooterFiles(templatesFolder string) error {
return nil
}

// watchfolderforchange will watch an individual folder for
// watchFolderForChange will watch an individual folder for
// any type of change, then trigger a rebuild
func watchFolderForChange(folder string) {
// Get the initial modification time of the folder
initialModTime := getFolderModTime(folder)

// make a watcher with fsnotify
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("unable to watch %s : %v", folder, err)
}
defer watcher.Close()
for {
// Sleep for a certain duration before checking for changes
time.Sleep(1 * time.Second)

err = watcher.Add(folder)
if err != nil {
log.Fatalf("couldn't add a watcher: %v", err)
}
// Get the current modification time of the folder
currentModTime := getFolderModTime(folder)

for {
select {
case event := <-watcher.Events:
log.Println("modified:", event.Name, " - rebuilding files..")
// Compare the current modification time with the initial one
if currentModTime.After(initialModTime) {
log.Println("Folder modified - rebuilding files..")
markdownToHTML(inFolder, outFolder, templateFolder)
createPostList(inFolder, outFolder, templateFolder)
case err := <-watcher.Errors:
log.Println("error:", err)

// Update the initial modification time
initialModTime = currentModTime
}
}
}

// getFolderModTime returns the modification time of a folder
func getFolderModTime(folder string) time.Time {
info, err := os.Stat(folder)
if err != nil {
log.Fatalf("unable to get folder info: %v", err)
}
return info.ModTime()
}

// sometimes even though it worked well, i regretted adding fsnotify.
// during the period I was writing bearclaw I was using as much stdlib
// as possible before reaching out to third party solutions.
// Not thinking this one through, or not being simple enough resulted
// in me choosing fsnotify and i always regretted it. not because its
// bad, but because i didnt try harder. we'll see if this new way works.

// // watchfolderforchange will watch an individual folder for
// // any type of change, then trigger a rebuild
// func watchFolderForChange(folder string) {

// // make a watcher with fsnotify
// watcher, err := fsnotify.NewWatcher()
// if err != nil {
// log.Fatalf("unable to watch %s : %v", folder, err)
// }
// defer watcher.Close()

// err = watcher.Add(folder)
// if err != nil {
// log.Fatalf("couldn't add a watcher: %v", err)
// }

// for {
// select {
// case event := <-watcher.Events:
// log.Println("modified:", event.Name, " - rebuilding files..")
// markdownToHTML(inFolder, outFolder, templateFolder)
// createPostList(inFolder, outFolder, templateFolder)
// case err := <-watcher.Errors:
// log.Println("error:", err)
// }
// }
// }

// watchfoldersforchanges loops through a list of folders and
// passes them to watchfolderforchange
func watchFoldersForChanges(folders []string) {
Expand Down

0 comments on commit e3fabc0

Please sign in to comment.