From 3ed412729754e5987d0dd7ef5fbaf0295eacbcfc Mon Sep 17 00:00:00 2001 From: ibib Date: Sun, 12 Feb 2017 21:43:39 +0100 Subject: [PATCH] Adds JSON support for the snapshots command --- doc/Manual.md | 9 ++++++ src/cmds/restic/cmd_snapshots.go | 51 ++++++++++++++++++++++++++++---- src/cmds/restic/global.go | 2 ++ 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/doc/Manual.md b/doc/Manual.md index 7398c23da..0fc37143a 100644 --- a/doc/Manual.md +++ b/doc/Manual.md @@ -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``` \ No newline at end of file diff --git a/src/cmds/restic/cmd_snapshots.go b/src/cmds/restic/cmd_snapshots.go index e4cedcaf4..7d293b728 100644 --- a/src/cmds/restic/cmd_snapshots.go +++ b/src/cmds/restic/cmd_snapshots.go @@ -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) + } diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index 8757130ea..b73d9de47 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -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() }