Skip to content

Commit

Permalink
Use writer.Write instead of fmt.FPrintf
Browse files Browse the repository at this point in the history
  • Loading branch information
banjoh committed Sep 13, 2023
1 parent a53acb4 commit f422726
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 41 deletions.
8 changes: 8 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ const (
// TermUI Display Constants
MESSAGE_TEXT_PADDING = 4
MESSAGE_TEXT_LINES_MARGIN_TO_BOTTOM = 4

// This is the initial size of the buffer allocated.
// Under the hood, an array of size N is allocated in memory
BUF_INIT_SIZE = 4096 // 4KB

// This is the muximum size the buffer can grow to
// Its not what the buffer will be allocated to initially
SCANNER_MAX_SIZE = 10 * 1024 * 1024 // 10MB
)
24 changes: 12 additions & 12 deletions pkg/redact/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package redact
import (
"bufio"
"bytes"
"fmt"
"io"

"github.com/replicatedhq/troubleshoot/pkg/constants"
)

type literalRedactor struct {
Expand Down Expand Up @@ -35,22 +36,18 @@ func (r literalRedactor) Redact(input io.Reader, path string) io.Reader {
}
}()

mask := []byte(MASK_TEXT)
buf := make([]byte, constants.BUF_INIT_SIZE)
scanner := bufio.NewScanner(input)
scanner.Buffer(buf, constants.SCANNER_MAX_SIZE)

reader := bufio.NewReader(input)
lineNum := 0
for {
for scanner.Scan() {
lineNum++
var line []byte
line, err = readLine(reader)
if err != nil {
return
}
line := scanner.Bytes()

clean := bytes.ReplaceAll(line, r.match, mask)
clean := bytes.ReplaceAll(line, r.match, maskTextBytes)

// io.WriteString would be nicer, but scanner strips new lines
fmt.Fprintf(writer, "%s\n", clean)
_, err = writer.Write(append(clean, '\n')) // Append newline since scanner strips it
if err != nil {
return
}
Expand All @@ -65,6 +62,9 @@ func (r literalRedactor) Redact(input io.Reader, path string) io.Reader {
})
}
}
if scanErr := scanner.Err(); scanErr != nil {
err = scanErr
}
}()
return out
}
30 changes: 22 additions & 8 deletions pkg/redact/multi_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package redact
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
)
Expand Down Expand Up @@ -54,8 +53,14 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
line1, line2, err := getNextTwoLines(reader, nil)
if err != nil {
// this will print 2 blank lines for empty input...
fmt.Fprintf(writer, "%s\n", line1)
fmt.Fprintf(writer, "%s\n", line2)
_, err = writer.Write(append(line1, '\n'))
if err != nil {
return
}
_, err = writer.Write(append(line2, '\n'))
if err != nil {
return
}
return
}

Expand All @@ -68,7 +73,10 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
if r.scan != nil {
lowerLine1 := bytes.ToLower(line1)
if !r.scan.Match(lowerLine1) {
fmt.Fprintf(writer, "%s\n", line1)
_, err = writer.Write(append(line1, '\n'))
if err != nil {
return
}
line1, line2, err = getNextTwoLines(reader, line2)
flushLastLine = true
continue
Expand All @@ -77,16 +85,19 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {

// If line1 matches re1, then transform line2 using re2
if !r.re1.Match(line1) {
fmt.Fprintf(writer, "%s\n", line1)
_, err = writer.Write(append(line1, '\n'))
if err != nil {
return
}
line1, line2, err = getNextTwoLines(reader, line2)
flushLastLine = true
continue
}
flushLastLine = false
clean := r.re2.ReplaceAll(line2, substStr)

// io.WriteString would be nicer, but reader strips new lines
fmt.Fprintf(writer, "%s\n%s\n", line1, clean)
out := append(append(line1, '\n'), append(clean, '\n')...) // line1 + \n + clean + \n
_, err = writer.Write(out)
if err != nil {
return
}
Expand All @@ -106,7 +117,10 @@ func (r *MultiLineRedactor) Redact(input io.Reader, path string) io.Reader {
}

if flushLastLine {
fmt.Fprintf(writer, "%s\n", line1)
_, err = writer.Write(append(line1, '\n')) // Append newline since scanner strips it
if err != nil {
return
}
}
}()
return out
Expand Down
2 changes: 1 addition & 1 deletion pkg/redact/multi_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
)

func Test_NewMultiLineRedactorr(t *testing.T) {
func Test_NewMultiLineRedactor(t *testing.T) {
tests := []struct {
name string
selector LineRedactor
Expand Down
4 changes: 3 additions & 1 deletion pkg/redact/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
// A regex cache to avoid recompiling the same regexes over and over
regexCache = map[string]*regexp.Regexp{}
regexCacheLock sync.Mutex
maskTextBytes = []byte(MASK_TEXT)
)

func init() {
Expand Down Expand Up @@ -113,6 +114,7 @@ func ResetRedactionList() {

func buildAdditionalRedactors(path string, redacts []*troubleshootv1beta2.Redact) ([]Redactor, error) {
additionalRedactors := []Redactor{}

for i, redact := range redacts {
if redact == nil {
continue
Expand Down Expand Up @@ -488,7 +490,7 @@ func readLine(r *bufio.Reader) ([]byte, error) {
var line []byte
line, isPrefix, err := r.ReadLine()
if err != nil {
return []byte{}, err
return nil, err
}

completeLine = append(completeLine, line...)
Expand Down
30 changes: 13 additions & 17 deletions pkg/redact/single_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package redact
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"

"github.com/replicatedhq/troubleshoot/pkg/constants"
)

type SingleLineRedactor struct {
Expand Down Expand Up @@ -34,15 +35,6 @@ func NewSingleLineRedactor(re LineRedactor, maskText, path, name string, isDefau
return &SingleLineRedactor{scan: scanCompiled, re: compiled, maskText: maskText, filePath: path, redactName: name, isDefault: isDefault}, nil
}

const (
// This is the initial size of the buffer allocated.
// Under the hood, an array of size N is allocated in memory
BUF_INIT_SIZE = 4096 // 4KB

// This is the muximum size the buffer can grow to
SCANNER_MAX_SIZE = 1024 * 1024 // 1MB
)

func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader {
out, writer := io.Pipe()

Expand All @@ -58,9 +50,9 @@ func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader {

substStr := []byte(getReplacementPattern(r.re, r.maskText))

buf := make([]byte, BUF_INIT_SIZE)
buf := make([]byte, constants.BUF_INIT_SIZE)
scanner := bufio.NewScanner(input)
scanner.Buffer(buf, SCANNER_MAX_SIZE)
scanner.Buffer(buf, constants.SCANNER_MAX_SIZE)

lineNum := 0
for scanner.Scan() {
Expand All @@ -71,22 +63,26 @@ func (r *SingleLineRedactor) Redact(input io.Reader, path string) io.Reader {
if r.scan != nil {
lowerLine := bytes.ToLower(line)
if !r.scan.Match(lowerLine) {
fmt.Fprintf(writer, "%s\n", line)
_, err = writer.Write(append(line, '\n')) // Append newline since scanner strips it
if err != nil {
return
}
continue
}
}

// if scan matches, but re does not, do not redact
if !r.re.Match(line) {
fmt.Fprintf(writer, "%s\n", line)
_, err = writer.Write(append(line, '\n')) // Append newline since scanner strips it
if err != nil {
return
}
continue
}

clean := r.re.ReplaceAll(line, substStr)

// io.WriteString would be nicer, but scanner strips new lines
fmt.Fprintf(writer, "%s\n", clean)

_, err = writer.Write(append(clean, '\n')) // Append newline since scanner strips it
if err != nil {
return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/redact/single_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package redact

import (
"bytes"
"io/ioutil"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestNewSingleLineRedactor(t *testing.T) {
req.NoError(err)

outReader := reRunner.Redact(bytes.NewReader([]byte(tt.inputString)), "")
gotBytes, err := ioutil.ReadAll(outReader)
gotBytes, err := io.ReadAll(outReader)
req.NoError(err)
req.Equal(tt.wantString, string(gotBytes))

Expand Down

0 comments on commit f422726

Please sign in to comment.