Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
Attempts to fix line interleaving issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Moran committed Feb 10, 2020
1 parent ea50173 commit 4602302
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions cred-filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import (

func main() {
source := os.Stdin
destination := os.Stdout

destination := NewLineWriter(os.Stdout)

if len(os.Args) > 1 && os.Args[1] == "-stderr" {
destination = os.Stderr
destination = NewLineWriter(os.Stderr)
}

defer destination.Close()

redacted, maxSize := RedactedList()

err := Stream(source, destination, redacted, maxSize)
Expand All @@ -26,6 +30,43 @@ func main() {
}
}

type LineWriter struct {
buffer []byte
writer io.Writer
}

func NewLineWriter(writer io.Writer) *LineWriter {
return &LineWriter{
buffer: []byte{},
writer: writer,
}
}

func (lw *LineWriter) Write(p []byte) (int, error) {
lw.buffer = append(lw.buffer, p...)

index := bytes.IndexRune(lw.buffer, '\n')
if index != -1 {
_, err := lw.writer.Write(lw.buffer[:index])
if err != nil {
return 0, err
}

lw.buffer = lw.buffer[index:]
}

return len(p), nil
}

func (lw *LineWriter) Close() error {
_, err := lw.writer.Write(lw.buffer)
if err != nil {
return err
}

return nil
}

type RedactedVariable struct {
Name string
Value []byte
Expand Down Expand Up @@ -68,7 +109,7 @@ func RedactedList() ([]RedactedVariable, int) {
return redacted, maxSize
}

func Stream(source io.Reader, destination io.Writer, redacted []RedactedVariable, maxSize int) error {
func Stream(source io.Reader, destination io.WriteCloser, redacted []RedactedVariable, maxSize int) error {
if maxSize == 0 {
_, err := io.Copy(destination, source)
if err != nil {
Expand Down

0 comments on commit 4602302

Please sign in to comment.