-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
391 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cdmistman/watchman" | ||
"github.com/cdmistman/watchman/protocol" | ||
"github.com/cdmistman/watchman/protocol/query" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type watchmanState int | ||
|
||
const ( | ||
uninit watchmanState = iota | ||
initing | ||
errored | ||
ready | ||
) | ||
|
||
type Watchman struct { | ||
subscriptions []Subscription | ||
} | ||
|
||
type Subscription struct { | ||
updates chan []string | ||
glob string | ||
} | ||
|
||
func (w *Watchman) MaybeStart() error { | ||
// TODO: proper error handling | ||
if len(w.subscriptions) == 0 { | ||
return nil | ||
} | ||
|
||
globs := []string{} | ||
for _, sub := range w.subscriptions { | ||
globs = append(globs, sub.glob) | ||
} | ||
|
||
q := query.Query{ | ||
Generators: query.Generators{query.GGlob: globs}, | ||
Fields: query.Fields{query.FName}, | ||
} | ||
|
||
client, err := watchman.Connect() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !client.HasCapability("glob_generator") { | ||
return fmt.Errorf("watchman does not have the glob_generator capability") | ||
} | ||
|
||
cwd, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
watch, err := client.AddWatch(cwd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if watch.RelativePath() != "" { | ||
if !client.HasCapability("relative_root") { | ||
return fmt.Errorf("watchman does not have the relative_root capability but gave a relative root") | ||
} | ||
|
||
q.RelativeRoot = watch.RelativePath() | ||
} | ||
|
||
if _, err = watch.Subscribe("process-compose", &q); err != nil { | ||
return err | ||
} | ||
|
||
go loop(client, w.subscriptions, watch.Root(), watch.RelativePath()) | ||
return nil | ||
} | ||
|
||
// assumes that all subscriptions have already been created | ||
func loop(client *watchman.Client, subs []Subscription, root, rel string) { | ||
for { | ||
rawNotif, ok := <-client.Notifications() | ||
if !ok { | ||
log.Warn().Msg("watchman client shut down, exiting watch loop") | ||
return | ||
} | ||
|
||
notif, ok := rawNotif.(*protocol.Subscription) | ||
if !ok { | ||
log.Debug().Msg("got a non-subscription notification, ignoring") | ||
} | ||
|
||
if notif.IsFreshInstance() { | ||
log.Debug().Msg("fresh instance notification, ignoring") | ||
} | ||
|
||
files := []string{} | ||
for _, rawFile := range notif.Files() { | ||
// if the query fields change, this will need to be a map[string]any | ||
file := rawFile.(string) | ||
files = append(files, file) | ||
} | ||
|
||
for _, sub := range subs { | ||
sub.updates <- files | ||
} | ||
} | ||
} | ||
|
||
func (w *Watchman) Subscribe(glob string) <-chan []string { | ||
ch := make(chan []string) | ||
w.subscriptions = append(w.subscriptions, Subscription{ | ||
updates: ch, | ||
glob: glob, | ||
}) | ||
|
||
return ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.