-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve output detail collection for command failures
- Loading branch information
1 parent
b9bcc7e
commit c492f9b
Showing
11 changed files
with
162 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package restic | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// headWriter keeps the first 'limit' bytes in memory. | ||
type headWriter struct { | ||
buf []byte | ||
limit int | ||
} | ||
|
||
var _ io.Writer = &headWriter{} | ||
|
||
func (w *headWriter) Write(p []byte) (n int, err error) { | ||
if len(w.buf) >= w.limit { | ||
return len(p), nil | ||
} | ||
w.buf = append(w.buf, p...) | ||
if len(w.buf) > w.limit { | ||
w.buf = w.buf[:w.limit] | ||
} | ||
return len(p), nil | ||
} | ||
|
||
func (w *headWriter) Bytes() []byte { | ||
return w.buf | ||
} | ||
|
||
// tailWriter keeps the last 'limit' bytes in memory. | ||
type tailWriter struct { | ||
buf []byte | ||
limit int | ||
} | ||
|
||
var _ io.Writer = &tailWriter{} | ||
|
||
func (w *tailWriter) Write(p []byte) (n int, err error) { | ||
w.buf = append(w.buf, p...) | ||
if len(w.buf) > w.limit { | ||
w.buf = w.buf[len(w.buf)-w.limit:] | ||
} | ||
return len(p), nil | ||
} | ||
|
||
func (w *tailWriter) Bytes() []byte { | ||
return w.buf | ||
} | ||
|
||
type outputCapturer struct { | ||
headWriter | ||
tailWriter | ||
limit int | ||
totalBytes int | ||
} | ||
|
||
var _ io.Writer = &outputCapturer{} | ||
|
||
func newOutputCapturer(limit int) *outputCapturer { | ||
return &outputCapturer{ | ||
headWriter: headWriter{limit: limit}, | ||
tailWriter: tailWriter{limit: limit}, | ||
limit: limit, | ||
} | ||
} | ||
|
||
func (w *outputCapturer) Write(p []byte) (n int, err error) { | ||
w.headWriter.Write(p) | ||
w.tailWriter.Write(p) | ||
w.totalBytes += len(p) | ||
return len(p), nil | ||
} | ||
|
||
func (w *outputCapturer) String() string { | ||
head := w.headWriter.Bytes() | ||
tail := w.tailWriter.Bytes() | ||
if w.totalBytes <= w.limit { | ||
return string(head) | ||
} | ||
|
||
head = head[:w.limit/2] | ||
tail = tail[len(tail)-w.limit/2:] | ||
|
||
return fmt.Sprintf("%s...[%v bytes dropped]...%s", string(head), w.totalBytes-len(head)-len(tail), string(tail)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package restic | ||
|
||
import "testing" | ||
|
||
func TestOutputCapture(t *testing.T) { | ||
c := newOutputCapturer(100) | ||
|
||
c.Write([]byte("hello")) | ||
|
||
if c.String() != "hello" { | ||
t.Errorf("expected 'hello', got '%s'", c.String()) | ||
} | ||
} | ||
|
||
func TestOutputCaptureDrops(t *testing.T) { | ||
c := newOutputCapturer(2) | ||
|
||
c.Write([]byte("hello")) | ||
|
||
want := "h...[3 bytes dropped]...o" | ||
if c.String() != want { | ||
t.Errorf("expected '%s', got '%s'", want, c.String()) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.