Skip to content

Commit

Permalink
add watcher for serving
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Sep 2, 2023
1 parent 72816e9 commit 4b9be26
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
28 changes: 27 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package builder

import (
"fmt"
"log/slog"
"path/filepath"

"github.com/bmatcuk/doublestar/v4"
"github.com/fsnotify/fsnotify"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
Expand All @@ -28,11 +31,34 @@ func (c *CLI) Run() error {
}

if c.Serve {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("could not create file watcher: %w", err)
}
defer watcher.Close()

go func() {
for event := range watcher.Events {
matched, _ := doublestar.Match(filepath.Join(c.SourcePath, "**", "*.md"), event.Name)

if matched {
slog.Info("rebuilding markdown files")

_ = renderer.Execute()
}
}
}()

err = watcher.Add(c.SourcePath)
if err != nil {
return fmt.Errorf("could add watching path: %w", err)
}

e := echo.New()
e.Use(middleware.Logger())
e.Static("/", c.BuildPath)

err := e.Start(":8080")
err = e.Start(":8080")
if err != nil {
return fmt.Errorf("could not start serving: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package main

import (
"log/slog"
"os"

"github.com/alecthomas/kong"
"github.com/jtarchie/builder"
)

func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))

cli := &builder.CLI{}
ctx := kong.Parse(cli)
// Call the Run() method of the selected parsed command.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
Expand Down Expand Up @@ -90,6 +92,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down

0 comments on commit 4b9be26

Please sign in to comment.