mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-14 09:35:41 +00:00
feat: support restic check operation (#303)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const outputBufferLimit = 1000
|
||||
@@ -40,13 +41,37 @@ func newCmdError(ctx context.Context, cmd *exec.Cmd, err error) *CmdError {
|
||||
return cerr
|
||||
}
|
||||
|
||||
func newCmdErrorPreformatted(ctx context.Context, cmd *exec.Cmd, err error) *CmdError {
|
||||
cerr := &CmdError{
|
||||
Command: cmd.String(),
|
||||
Err: err,
|
||||
}
|
||||
if logger := LoggerFromContext(ctx); logger != nil {
|
||||
logger.Write([]byte(cerr.Error()))
|
||||
}
|
||||
return cerr
|
||||
type ErrorWithOutput struct {
|
||||
Err error
|
||||
Output string
|
||||
}
|
||||
|
||||
func (e *ErrorWithOutput) Error() string {
|
||||
return fmt.Sprintf("%v\nOutput:\n%s", e.Err, e.Output)
|
||||
}
|
||||
|
||||
func (e *ErrorWithOutput) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
func (e *ErrorWithOutput) Is(target error) bool {
|
||||
_, ok := target.(*ErrorWithOutput)
|
||||
return ok
|
||||
}
|
||||
|
||||
// newErrorWithOutput creates a new error with the given output.
|
||||
func newErrorWithOutput(err error, output string) error {
|
||||
firstNewLine := strings.Index(output, "\n")
|
||||
if firstNewLine > 0 {
|
||||
output = output[:firstNewLine]
|
||||
}
|
||||
|
||||
if len(output) == 0 {
|
||||
return err
|
||||
}
|
||||
|
||||
return &ErrorWithOutput{
|
||||
Err: err,
|
||||
Output: output,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user