aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--golings/cmd/watch.go52
1 files changed, 27 insertions, 25 deletions
diff --git a/golings/cmd/watch.go b/golings/cmd/watch.go
index 549770c..9e159dd 100644
--- a/golings/cmd/watch.go
+++ b/golings/cmd/watch.go
@@ -5,9 +5,9 @@ import (
"io/fs"
"log"
"os"
+ "os/exec"
"path/filepath"
- "github.com/fatih/color"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
)
@@ -17,7 +17,6 @@ func WatchCmd() *cobra.Command {
Use: "watch",
Short: "Run a single exercise",
Run: func(cmd *cobra.Command, args []string) {
- color.White("WATCHING ALL")
log.Println("Create watcher")
watcher, err := fsnotify.NewWatcher()
if err != nil {
@@ -25,27 +24,6 @@ func WatchCmd() *cobra.Command {
}
defer watcher.Close()
- // Start listening for events.
- go func() {
- for {
- select {
- case event, ok := <-watcher.Events:
- if !ok {
- return
- }
- log.Println("event:", event)
- if event.Has(fsnotify.Write) {
- log.Println("modified file:", event.Name)
- }
- case err, ok := <-watcher.Errors:
- if !ok {
- return
- }
- log.Println("error:", err)
- }
- }
- }()
-
path, _ := os.Getwd()
file_path := fmt.Sprintf("%s/exercises", path)
@@ -55,8 +33,7 @@ func WatchCmd() *cobra.Command {
return err
}
if d.IsDir() {
- log.Printf("Added %s to watch\n", path_dir)
- err = watcher.Add(file_path)
+ err = watcher.Add(path_dir)
if err != nil {
log.Fatal(err)
@@ -69,6 +46,31 @@ func WatchCmd() *cobra.Command {
log.Fatal("Error in file path:", err.Error())
}
+ // Start listening for events.
+ go func() {
+ for {
+ select {
+ case event, ok := <-watcher.Events:
+ if !ok {
+ return
+ }
+ log.Println("event:", event)
+ if event.Has(fsnotify.Write) {
+ log.Println("modified file:", event.Name)
+ cmd := exec.Command("golings run next")
+ if err := cmd.Run(); err != nil {
+ log.Fatal(err)
+ }
+ }
+ case err, ok := <-watcher.Errors:
+ if !ok {
+ return
+ }
+ log.Println("error:", err)
+ }
+ }
+ }()
+
// Block main goroutine forever.
<-make(chan struct{})
},