about summary refs log tree commit diff stats
path: root/internal/server/dev.go
blob: e83201a3caa7c04f1fdb94fbe5b3482d0f5c712d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package server

import (
	"fmt"
	"io/fs"
	"log/slog"
	"os"
	"path"
	"path/filepath"
	"slices"
	"time"

	"go.alanpearce.eu/website/internal/log"

	"github.com/fsnotify/fsnotify"
	"gitlab.com/tozd/go/errors"
)

type FileWatcher struct {
	*fsnotify.Watcher
}

var (
	ignores = []string{
		"*.templ",
		"*.go",
	}
	checkSettleInterval = 200 * time.Millisecond
)

func matches(name string) func(string) bool {
	return func(pattern string) bool {
		matched, err := path.Match(pattern, name)
		if err != nil {
			log.Warn("error checking watcher ignores", "error", err)
		}

		return matched
	}
}

func ignored(pathname string) bool {
	return slices.ContainsFunc(ignores, matches(path.Base(pathname)))
}

func NewFileWatcher() (*FileWatcher, error) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return nil, errors.WithMessage(err, "could not create watcher")
	}

	return &FileWatcher{watcher}, nil
}

func (watcher FileWatcher) AddRecursive(from string) error {
	log.Debug("walking directory tree", "root", from)
	err := filepath.WalkDir(from, func(path string, entry fs.DirEntry, err error) error {
		if err != nil {
			return errors.WithMessagef(err, "could not walk directory %s", path)
		}
		if entry.IsDir() {
			log.Debug("adding directory to watcher", "path", path)
			if err = watcher.Add(path); err != nil {
				return errors.WithMessagef(err, "could not add directory %s to watcher", path)
			}
		}

		return nil
	})

	return errors.WithMessage(err, "error walking directory tree")
}

func (watcher FileWatcher) Start(callback func(string)) {
	var timer *time.Timer
	for {
		select {
		case event := <-watcher.Events:
			if !ignored(event.Name) {
				log.Debug("watcher event", "name", event.Name, "op", event.Op.String())
				if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) {
					f, err := os.Stat(event.Name)
					if err != nil {
						slog.Error(
							fmt.Sprintf("error handling %s event: %v", event.Op.String(), err),
						)
					} else if f.IsDir() {
						err = watcher.Add(event.Name)
						if err != nil {
							slog.Error(fmt.Sprintf("error adding new folder to watcher: %v", err))
						}
					}
				}
				if event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) ||
					event.Has(fsnotify.Create) || event.Has(fsnotify.Chmod) {
					if timer == nil {
						timer = time.AfterFunc(checkSettleInterval, func() {
							callback(event.Name)
						})
					}
					timer.Reset(checkSettleInterval)
				}
			}
		case err := <-watcher.Errors:
			slog.Error(fmt.Sprintf("error in watcher: %v", err))
		}
	}
}