Adds JSON support for the snapshots command

This commit is contained in:
ibib 2017-02-12 21:43:39 +01:00
parent c83e608cce
commit 3ed4127297
3 changed files with 57 additions and 5 deletions

View File

@ -77,6 +77,7 @@ Available Commands:
version Print version information
Flags:
--json set output mode to JSON for commands that support it
--no-lock do not lock the repo, this allows some operations on read-only repos
-p, --password-file string read the repository password from a file
-q, --quiet do not output comprehensive progress report
@ -110,6 +111,7 @@ Flags:
--tag tag add a tag for the new snapshot (can be specified multiple times)
Global Flags:
--json set output mode to JSON for commands that support it
--no-lock do not lock the repo, this allows some operations on read-only repos
-p, --password-file string read the repository password from a file
-q, --quiet do not output comprehensive progress report
@ -739,3 +741,10 @@ enter password for repository:
"gid": 20
}
```
# Scripting restic
Restic supports the output of some commands in JSON format. The JSON flag ```--json``` is currently supported only by ```restic snapshots```.
```console
$ restic -r /tmp/backup snapshots --json```

View File

@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"
"encoding/json"
"restic"
)
@ -56,10 +57,6 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
}
}
tab := NewTable()
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %-3s %s", "ID", "Date", "Host", "Tags", "", "Directory")
tab.RowFormat = "%-8s %-19s %-10s %-10s %-3s %s"
done := make(chan struct{})
defer close(done)
@ -87,6 +84,25 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
}
if gopts.JSON {
err := printSnapshotsJSON(list)
if err != nil {
fmt.Fprintf(os.Stderr, "error printing snapshot: %v\n", err)
}
return nil
}
printSnapshotsReadable(list)
return nil
}
// printSnapshotsReadable prints a text table of the snapshots in list to stdout.
func printSnapshotsReadable(list []*restic.Snapshot) {
tab := NewTable()
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %-3s %s", "ID", "Date", "Host", "Tags", "", "Directory")
tab.RowFormat = "%-8s %-19s %-10s %-10s %-3s %s"
for _, sn := range list {
if len(sn.Paths) == 0 {
continue
@ -132,5 +148,30 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
tab.Write(os.Stdout)
return nil
return
}
// Snapshot helps to print Snaphots as JSON
type Snapshot struct {
*restic.Snapshot
ID string `json:"id"`
}
// printSnapshotsJSON writes the JSON representation of list to stdout.
func printSnapshotsJSON(list []*restic.Snapshot) error {
var snapshots []Snapshot
for _, sn := range list {
k := Snapshot{
Snapshot: sn,
ID: sn.ID().String(),
}
snapshots = append(snapshots, k)
}
return json.NewEncoder(os.Stdout).Encode(snapshots)
}

View File

@ -31,6 +31,7 @@ type GlobalOptions struct {
PasswordFile string
Quiet bool
NoLock bool
JSON bool
password string
stdout io.Writer
@ -53,6 +54,7 @@ func init() {
f.StringVarP(&globalOptions.PasswordFile, "password-file", "p", "", "read the repository password from a file")
f.BoolVarP(&globalOptions.Quiet, "quiet", "q", false, "do not output comprehensive progress report")
f.BoolVar(&globalOptions.NoLock, "no-lock", false, "do not lock the repo, this allows some operations on read-only repos")
f.BoolVarP(&globalOptions.JSON, "json", "", false, "set output mode to JSON for commands that support it")
restoreTerminal()
}