From ea8096f4630fa53f289342fb8476fde0acaa7c52 Mon Sep 17 00:00:00 2001 From: Dain Cilke Date: Mon, 8 Aug 2022 10:21:08 +0200 Subject: [PATCH] handle stream eof --- go.mod | 4 +++- go.sum | 6 ++---- main.go | 32 +++++++++++++++++++------------- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 593b370..1706473 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/dcilke/hz go 1.18 require ( - github.com/dcilke/gojay v1.2.15 + github.com/dcilke/gojay v1.2.16 github.com/jessevdk/go-flags v1.5.0 github.com/mattn/go-colorable v0.1.12 ) @@ -12,3 +12,5 @@ require ( github.com/mattn/go-isatty v0.0.14 // indirect golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect ) + +replace github.com/dcilke/gojay v1.2.15 => ../gojay diff --git a/go.sum b/go.sum index d90839e..e1b649c 100644 --- a/go.sum +++ b/go.sum @@ -16,10 +16,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dcilke/gojay v1.2.14 h1:FAqFDPZKgg3FVRJ5IfPnFVRPF8uIpJv2OxFkY7jw8HY= -github.com/dcilke/gojay v1.2.14/go.mod h1:ehT5mTG4ua4581f1++1WLG0vPdaA9HaiDsoyrBGkyDY= -github.com/dcilke/gojay v1.2.15 h1:sb45ro1bZ9QgzgxYYrA4QGjNIkxo9R0QTpuMI9boag4= -github.com/dcilke/gojay v1.2.15/go.mod h1:/glM9szaBVVnuxmiSqzrbWfFDEyncarHqADznC3PGTg= +github.com/dcilke/gojay v1.2.16 h1:2UTs3/RN8nSntOJtFzKAnCC/a6exNnoMSQJCZKuSVkA= +github.com/dcilke/gojay v1.2.16/go.mod h1:/glM9szaBVVnuxmiSqzrbWfFDEyncarHqADznC3PGTg= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= diff --git a/main.go b/main.go index 7c9b2da..8aa0955 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "fmt" + "io" "os" "os/signal" "syscall" @@ -15,8 +16,7 @@ import ( var writer = NewConsoleWriter() const ( - bufSize = 1024 - bufDump = bufSize / 2 + bufDump = 512 ) type cmd struct{} @@ -48,15 +48,14 @@ func process(file *os.File) { reader := bufio.NewReader(file) decoder := gojay.Stream.NewDecoder(reader) decoder.UseNumber() - _buf := make([]byte, 0, bufSize) + _buf := make([]byte, 0, bufDump) buf := bytes.NewBuffer(_buf) c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c - writer.Printf("%s", buf.String()) - buf.Reset() + flush(buf) os.Exit(0) }() @@ -64,18 +63,18 @@ func process(file *os.File) { var msg any err := decoder.Decode(&msg) if err != nil { - b, _ := decoder.ReadByte() + b, err := decoder.ReadByte() + if err == io.EOF { + flush(buf) + return + } buf.WriteByte(b) - if buf.Len() > bufDump || b == 10 { - writer.Printf("%s", buf.String()) - buf.Reset() + if buf.Len() >= bufDump || b == 10 { + flush(buf) } continue } - if buf.Len() > 0 { - writer.Printf("%s", buf.String()) - buf.Reset() - } + flush(buf) err = writer.WriteAny(msg) if err != nil { writer.Println(err) @@ -83,6 +82,13 @@ func process(file *os.File) { } } +func flush(buf *bytes.Buffer) { + if buf.Len() > 0 { + writer.Printf("%s", buf.String()) + buf.Reset() + } +} + func check(err error, hint string) { if err != nil { writer.Println(hint)