Skip to content

Commit

Permalink
fix: resolve comments for Shiwei
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Dec 2, 2024
1 parent 665e111 commit 161a736
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions internal/io/limitwriter.go → internal/io/limitedwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ package io

import "io"

// LimitWriter is a writer that writes to an underlying writer up to a limit.
type LimitWriter struct {
// LimitedWriter is a writer that writes to an underlying writer up to a limit.
type LimitedWriter struct {
w io.Writer
limit int64
written int64
}

// NewLimitWriter returns a new LimitWriter that writes to w.
// LimitWriter returns a new LimitWriter that writes to w.
//
// parameters:
// w: the writer to write to
// limit: the maximum number of bytes to write
func NewLimitWriter(w io.Writer, limit int64) *LimitWriter {
return &LimitWriter{w: w, limit: limit}
func LimitWriter(w io.Writer, limit int64) *LimitedWriter {
return &LimitedWriter{w: w, limit: limit}
}

// Write writes p to the underlying writer up to the limit.
func (lw *LimitWriter) Write(p []byte) (int, error) {
func (lw *LimitedWriter) Write(p []byte) (int, error) {
remaining := lw.limit - lw.written
if remaining <= 0 {
return 0, nil
return 0, io.ErrShortWrite
}
if int64(len(p)) > remaining {
p = p[:remaining]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestLimitWriter(t *testing.T) {

for _, tt := range tests {
var buf bytes.Buffer
lw := NewLimitWriter(&buf, limit)
lw := LimitWriter(&buf, limit)
n, err := lw.Write([]byte(tt.input))
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ func (c execCommander) Output(ctx context.Context, name string, command plugin.C
var stdout, stderr bytes.Buffer
cmd := exec.CommandContext(ctx, name, string(command))
cmd.Stdin = bytes.NewReader(req)
cmd.Stderr = io.NewLimitWriter(&stderr, maxPluginOutputSize)
cmd.Stdout = io.NewLimitWriter(&stdout, maxPluginOutputSize)
cmd.Stderr = io.LimitWriter(&stderr, maxPluginOutputSize)
cmd.Stdout = io.LimitWriter(&stdout, maxPluginOutputSize)
err := cmd.Run()
if err != nil {
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
Expand Down

0 comments on commit 161a736

Please sign in to comment.