visualizer: make it optional to dup JSON to file

This commit is contained in:
Simon Ruderich 2024-05-28 08:59:06 +02:00
parent 570c379bc5
commit d5aeb6c0bc

View file

@ -24,9 +24,9 @@ import (
var static embed.FS var static embed.FS
func main() { func main() {
if len(os.Args) != 3 { if len(os.Args) != 2 && len(os.Args) != 3 {
log.SetFlags(0) log.SetFlags(0)
log.Fatalf("usage: %s <fifo> <json-dup-prefix>", os.Args[0]) log.Fatalf("usage: %s <fifo> [<json-dup-prefix>]", os.Args[0])
} }
var mutex sync.Mutex var mutex sync.Mutex
@ -35,16 +35,22 @@ func main() {
go func() { go func() {
for { for {
path := os.Args[1] path := os.Args[1]
dupPath := os.Args[2] + "." + time.Now().Format(time.RFC3339) var dupPath string
if len(os.Args) == 3 {
dupPath = os.Args[2] + "." + time.Now().Format(time.RFC3339)
}
fh, err := os.Open(path) fh, err := os.Open(path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
dupFh, err := os.Create(dupPath) var dupFh *os.File
if dupPath != "" {
dupFh, err = os.Create(dupPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
r := bufio.NewReader(fh) r := bufio.NewReader(fh)
for { for {
@ -56,10 +62,12 @@ func main() {
// Duplicate JSON input to a file so it can be replayed later // Duplicate JSON input to a file so it can be replayed later
// if necessary // if necessary
if dupFh != nil {
_, err = dupFh.WriteString(line) _, err = dupFh.WriteString(line)
if err != nil { if err != nil {
log.Fatalf("write %q: %v", dupPath, err) log.Fatalf("write %q: %v", dupPath, err)
} }
}
// Send to all listeners // Send to all listeners
mutex.Lock() mutex.Lock()
@ -73,11 +81,13 @@ func main() {
if err != nil { if err != nil {
log.Fatalf("close %q: %v", path, err) log.Fatalf("close %q: %v", path, err)
} }
if dupFh != nil {
err = dupFh.Close() err = dupFh.Close()
if err != nil { if err != nil {
log.Fatalf("close %q: %v", dupPath, err) log.Fatalf("close %q: %v", dupPath, err)
} }
} }
}
}() }()
// Use existing "./static/" directory for quick changes // Use existing "./static/" directory for quick changes