Skip to content

Commit

Permalink
added -from-file flag
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak committed Oct 6, 2024
1 parent f29c1bd commit 312c877
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ cat test.json | vgt
Usage of vgt:
-debug
enable debug mode
-dont-pass-output
don't print output received to stdin
-duration-cutoff string
threshold for test duration cutoff, under which tests are not shown in the chart (default "100µs")
-from-file string
read input from file instead of stdin
-keep-running
keep browser running after page was opened
-pass-output
pass output received to stdout (default true)
-print-html
print html to stdout instead of opening browser
```
Expand Down
38 changes: 29 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "embed"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
Expand All @@ -26,6 +27,7 @@ var testDurationCutoff string
var testDurationCutoffDuration time.Duration
var printHTML bool
var keepRunning bool
var fromFile string

func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
Expand All @@ -35,6 +37,7 @@ func main() {
flag.BoolVar(&dontPassOutput, "dont-pass-output", false, "don't print output received to stdin")
flag.BoolVar(&keepRunning, "keep-running", false, "keep browser running after page was opened")
flag.BoolVar(&printHTML, "print-html", false, "print html to stdout instead of opening browser")
flag.StringVar(&fromFile, "from-file", "", "read input from file instead of stdin")

flag.StringVar(
&testDurationCutoff,
Expand Down Expand Up @@ -62,16 +65,33 @@ func main() {
}),
))

r, err := cancelreader.NewReader(os.Stdin)
if err != nil {
slog.Error("Error creating cancel reader", "err", err)
return
}
var r io.Reader

if fromFile == "" {
sr, err := cancelreader.NewReader(os.Stdin)
if err != nil {
slog.Error("Error creating cancel reader", "err", err)
return
}

go func() {
<-ctx.Done()
r.Cancel()
}()
go func() {
<-ctx.Done()
sr.Cancel()
}()

r = sr
} else {
f, err := os.Open(fromFile)
if err != nil {
slog.Error("Error opening file", "err", err)
return
}
defer func() {
_ = f.Close()
}()

r = f
}

scanner := bufio.NewScanner(r)

Expand Down

0 comments on commit 312c877

Please sign in to comment.