make cmd dirs; start real-ifying

trunk
vilmibm 2020-06-12 22:25:29 +00:00
parent 551871c158
commit 27c0060195
4 changed files with 78 additions and 53 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
cmd/bustled/bustled
cmd/bustle/bustle
*.swp
# ---> Go
# Binaries for programs and plugins

View File

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println("TODO bustle client")
}

View File

@ -0,0 +1,69 @@
package main
import (
"fmt"
"log"
"os"
"github.com/fsnotify/fsnotify"
)
func initHomeWatcher() (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// TODO filepath.Walk over /home, adding to watcher
paths := []string{"/home/vilmibm", "/home/wren"}
for _, path := range paths {
err = watcher.Add(path)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to watch path %s: %w", path, err)
continue
}
}
return watcher, nil
}
func watchHome(watcher *fsnotify.Watcher) {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}
func cli(args []string) int {
watcher, err := initHomeWatcher()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create watcher: %w", err)
return 1
}
defer watcher.Close()
// TODO have an event bus to write to
// TODO what is this done for do i need this done
done := make(chan bool)
go watchHome(watcher)
<-done
return 0
}
func main() {
os.Exit(cli(os.Args))
}

53
main.go
View File

@ -1,53 +0,0 @@
package main
import (
"fmt"
"log"
"os"
"github.com/fsnotify/fsnotify"
)
func main() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
done := make(chan bool)
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
// will need to recursively watch for myself, including adding watchers for new directories D:
// check out filepath.Walk
for _, path := range os.Args[1:] {
err = watcher.Add(path)
if err != nil {
log.Fatal(err)
}
}
<-done
fmt.Println("hmm")
}