Add JSON output to check command

This commit is contained in:
Dark Dragon 2024-12-30 22:33:01 +01:00
parent 79d435efb1
commit a58a8f2ce0
3 changed files with 68 additions and 1 deletions

View File

@ -0,0 +1,7 @@
Enhancement: Add JSON support to check
Restic `check` now also supports the `--json` option and gives all
statistics in JSON format.
https://github.com/restic/restic/issues/1378
https://github.com/restic/restic/pull/5194

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"fmt"
"math/rand" "math/rand"
"os" "os"
"strconv" "strconv"
@ -215,7 +216,12 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
return errors.Fatal("the check command expects no arguments, only options - please see `restic help check` for usage and flags") return errors.Fatal("the check command expects no arguments, only options - please see `restic help check` for usage and flags")
} }
printer := newTerminalProgressPrinter(gopts.verbosity, term) var printer progress.Printer
if !gopts.JSON {
printer = newTerminalProgressPrinter(gopts.verbosity, term)
} else {
printer = newJSONErrorPrinter(term)
}
cleanup := prepareCheckCache(opts, &gopts, printer) cleanup := prepareCheckCache(opts, &gopts, printer)
defer cleanup() defer cleanup()
@ -431,6 +437,13 @@ func runCheck(ctx context.Context, opts CheckOptions, gopts GlobalOptions, args
return errors.Fatal("repository contains errors") return errors.Fatal("repository contains errors")
} }
printer.P("no errors were found\n") printer.P("no errors were found\n")
if gopts.JSON {
status := checkSuccess{
MessageType: "checked",
Message: "no errors were found",
}
term.Print(ui.ToJSONString(status))
}
return nil return nil
} }
@ -478,3 +491,38 @@ func selectRandomPacksByFileSize(allPacks map[restic.ID]int64, subsetSize int64,
packs := selectRandomPacksByPercentage(allPacks, subsetPercentage) packs := selectRandomPacksByPercentage(allPacks, subsetPercentage)
return packs return packs
} }
type checkSuccess struct {
MessageType string `json:"message_type"` // "checked"
Message string `json:"message"`
}
type checkError struct {
MessageType string `json:"message_type"` // "error"
Message string `json:"message"`
}
type jsonErrorPrinter struct {
term ui.Terminal
}
func newJSONErrorPrinter(term ui.Terminal) *jsonErrorPrinter {
return &jsonErrorPrinter{
term: term,
}
}
func (*jsonErrorPrinter) NewCounter(_ string) *progress.Counter {
return nil
}
func (p *jsonErrorPrinter) E(msg string, args ...interface{}) {
status := checkError{
MessageType: "error",
Message: fmt.Sprintf(msg, args...),
}
p.term.Print(ui.ToJSONString(status))
}
func (*jsonErrorPrinter) P(_ string, _ ...interface{}) {}
func (*jsonErrorPrinter) V(_ string, _ ...interface{}) {}
func (*jsonErrorPrinter) VV(_ string, _ ...interface{}) {}

View File

@ -245,6 +245,18 @@ are stored in JSON form. Specifying ``--json`` or ``--quiet`` will suppress any
non-JSON messages the command generates. non-JSON messages the command generates.
check
-----
The ``check`` command outputs JSON messages with the following format:
+------------------+--------------------------------+
| ``message_type`` | Either "checked" or "error" |
+------------------+--------------------------------+
| ``message`` | Descriptive message |
+------------------+--------------------------------+
diff diff
---- ----