From 161a7361dc83539263143086ca4cdd7eee7522e1 Mon Sep 17 00:00:00 2001 From: Junjie Gao Date: Mon, 2 Dec 2024 02:13:35 +0000 Subject: [PATCH] fix: resolve comments for Shiwei Signed-off-by: Junjie Gao --- internal/io/{limitwriter.go => limitedwriter.go} | 14 +++++++------- .../{limitWriter_test.go => limitedwriter_test.go} | 2 +- plugin/plugin.go | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) rename internal/io/{limitwriter.go => limitedwriter.go} (75%) rename internal/io/{limitWriter_test.go => limitedwriter_test.go} (97%) diff --git a/internal/io/limitwriter.go b/internal/io/limitedwriter.go similarity index 75% rename from internal/io/limitwriter.go rename to internal/io/limitedwriter.go index c48b8586..b074f634 100644 --- a/internal/io/limitwriter.go +++ b/internal/io/limitedwriter.go @@ -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] diff --git a/internal/io/limitWriter_test.go b/internal/io/limitedwriter_test.go similarity index 97% rename from internal/io/limitWriter_test.go rename to internal/io/limitedwriter_test.go index 4a190614..59608c93 100644 --- a/internal/io/limitWriter_test.go +++ b/internal/io/limitedwriter_test.go @@ -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) diff --git a/plugin/plugin.go b/plugin/plugin.go index b894c888..1fb34e36 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -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) {