Skip to content

Commit

Permalink
give index numbers to process outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
mnafees committed Jun 20, 2023
1 parent b82e0b3 commit 2e224bf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions internal/process/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 21 additions & 3 deletions internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
Expand Down
23 changes: 18 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"log"
"math"
"os"
Expand Down Expand Up @@ -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)

Expand All @@ -81,7 +91,7 @@ func main() {
return
}

log.Println("started all processes")
log.Printf("started all processes\n\n")

for {
select {
Expand All @@ -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()
Expand All @@ -120,7 +131,8 @@ func main() {
}
}

log.Println("restarted processes")
log.Printf("restarted processes")
fmt.Println()

mu.Lock()
delete(timers, event.Name)
Expand All @@ -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 {
Expand Down

0 comments on commit 2e224bf

Please sign in to comment.