Skip to content

Commit

Permalink
handle stream eof
Browse files Browse the repository at this point in the history
  • Loading branch information
dcilke committed Aug 8, 2022
1 parent cf27de1 commit ea8096f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
32 changes: 19 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/signal"
"syscall"
Expand All @@ -15,8 +16,7 @@ import (
var writer = NewConsoleWriter()

const (
bufSize = 1024
bufDump = bufSize / 2
bufDump = 512
)

type cmd struct{}
Expand Down Expand Up @@ -48,41 +48,47 @@ 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)
}()

for {
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)
}
}
}

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)
Expand Down

0 comments on commit ea8096f

Please sign in to comment.