diff --git a/README.md b/README.md index 30e1efb..a7ef996 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,11 @@ A dead simple recursive file watcher that works by attaching itself to processes ## Installation +1. If you have Go installed, then ```bash $ go install github.com/mnafees/liver@latest ``` +2. Or else, directly download from the [latest release](https://github.com/mnafees/liver/releases/latest) for your respective OS and architecture and place the binary in a location such as `/usr/local/bin` which is part of your `PATH` ## Getting started diff --git a/internal/process/manager.go b/internal/process/manager.go index de04d16..a3cfd26 100644 --- a/internal/process/manager.go +++ b/internal/process/manager.go @@ -18,8 +18,8 @@ func NewProcessManager() *ProcessManager { } } -func (pm *ProcessManager) Add(path, command string) { - p := newProcess(command) +func (pm *ProcessManager) Add(idx uint, path, command string) { + p := newProcess(idx, command) if _, ok := pm.procs[path]; !ok { pm.procs[path] = make([]*process, 0) diff --git a/internal/process/process.go b/internal/process/process.go index 6816aea..e2249ba 100644 --- a/internal/process/process.go +++ b/internal/process/process.go @@ -8,18 +8,36 @@ import ( "sync" ) +type errWriter struct { + idx uint +} + +func (ew *errWriter) Write(p []byte) (int, error) { + return fmt.Fprintf(os.Stderr, "[%d]: %s", ew.idx, string(p)) +} + +type outWriter struct { + idx uint +} + +func (ow *outWriter) Write(p []byte) (int, error) { + return fmt.Fprintf(os.Stdout, "[%d]: %s", ow.idx, string(p)) +} + type process struct { internalProcessCmd *exec.Cmd mu *sync.Mutex args []string + idx uint } -func newProcess(command string) *process { +func newProcess(idx uint, command string) *process { args := strings.Split(command, " ") return &process{ mu: &sync.Mutex{}, args: args, + idx: idx, } } @@ -32,8 +50,8 @@ func (p *process) Start() error { defer p.mu.Unlock() cmd := exec.Command(p.args[0], p.args[1:]...) - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout + cmd.Stderr = &errWriter{idx: p.idx} + cmd.Stdout = &outWriter{idx: p.idx} cmd.Stdin = os.Stdin p.internalProcessCmd = cmd diff --git a/main.go b/main.go index b6b55ba..f66baf2 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "log" "math" "os" @@ -54,12 +55,21 @@ func main() { } } + idx := uint(0) + + fmt.Println() + for path, commands := range c.Procs { for _, c := range commands { - pm.Add(path, c) + fmt.Printf("setting process %d for: %s\n", idx, c) + + pm.Add(idx, path, c) + idx++ } } + fmt.Println() + sig := make(chan os.Signal, 1) signal.Notify(sig, os.Interrupt) @@ -81,7 +91,7 @@ func main() { return } - log.Println("started all processes") + log.Printf("started all processes\n\n") for { select { @@ -102,7 +112,8 @@ func main() { if !ok { t = time.AfterFunc(math.MaxInt64, func() { - log.Println("stopping processes") + fmt.Println() + log.Printf("stopping processes") for _, p := range procs { err := p.Kill() @@ -120,7 +131,8 @@ func main() { } } - log.Println("restarted processes") + log.Printf("restarted processes") + fmt.Println() mu.Lock() delete(timers, event.Name) @@ -147,7 +159,8 @@ func main() { <-sig - log.Println("stopping all processes") + fmt.Println() + log.Printf("stopping all processes") err = pm.StopAll() if err != nil {