2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00

Merge pull request #803 from ibib/adds-json-support

Adds JSON support for the snapshots command
This commit is contained in:
Alexander Neumann 2017-02-16 11:26:14 +01:00
commit 40685a0e61
3 changed files with 57 additions and 5 deletions

View File

@ -77,6 +77,7 @@ Available Commands:
version Print version information version Print version information
Flags: 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 --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 -p, --password-file string read the repository password from a file
-q, --quiet do not output comprehensive progress report -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) --tag tag add a tag for the new snapshot (can be specified multiple times)
Global Flags: 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 --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 -p, --password-file string read the repository password from a file
-q, --quiet do not output comprehensive progress report -q, --quiet do not output comprehensive progress report
@ -739,3 +741,10 @@ enter password for repository:
"gid": 20 "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" "github.com/spf13/cobra"
"encoding/json"
"restic" "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{}) done := make(chan struct{})
defer close(done) 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 { for _, sn := range list {
if len(sn.Paths) == 0 { if len(sn.Paths) == 0 {
continue continue
@ -132,5 +148,30 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
tab.Write(os.Stdout) 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 PasswordFile string
Quiet bool Quiet bool
NoLock bool NoLock bool
JSON bool
password string password string
stdout io.Writer stdout io.Writer
@ -53,6 +54,7 @@ func init() {
f.StringVarP(&globalOptions.PasswordFile, "password-file", "p", "", "read the repository password from a file") 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.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.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() restoreTerminal()
} }