From ee167dac5f7f47b29495c03e2fb1d7160910ef25 Mon Sep 17 00:00:00 2001 From: ibib Date: Sun, 12 Feb 2017 01:27:32 +0100 Subject: [PATCH] Expands the JSON output based on the Original Snaphot declaration --- src/cmds/restic/cmd_snapshots.go | 41 +++++++++++++++++++++----------- src/restic/id.go | 9 +++++++ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/cmds/restic/cmd_snapshots.go b/src/cmds/restic/cmd_snapshots.go index 03243cb70..d5ef8ffe3 100644 --- a/src/cmds/restic/cmd_snapshots.go +++ b/src/cmds/restic/cmd_snapshots.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" "restic" + "time" ) var cmdSnapshots = &cobra.Command{ @@ -151,35 +152,47 @@ func printSnapshotsReadable(list []*restic.Snapshot) { return } -//Snapshot provides struct to store snapshots +// Snapshot helps to print Snaphots as JSON type Snapshot struct { - ID string `json:"id"` - Date string `json:"date"` - Host string `json:"host"` - Tags []string `json:"tags"` - Directories []string `json:"directories"` + Time time.Time `json:"time"` + Parent string `json:"parent,omitempty"` + Tree string `json:"tree,omitempty"` + Paths []string `json:"paths"` + Hostname string `json:"hostname,omitempty"` + Username string `json:"username,omitempty"` + UID uint32 `json:"uid,omitempty"` + GID uint32 `json:"gid,omitempty"` + Excludes []string `json:"excludes,omitempty"` + Tags []string `json:"tags,omitempty"` + ID string `json:"id"` } // printSnapshotsJSON writes the JSON representation of list to stdout. func printSnapshotsJSON(list []*restic.Snapshot) error { enc := json.NewEncoder(os.Stdout) - //set JSON prefix + // set JSON prefix fmt.Fprint(os.Stdout, "[") for i, sn := range list { - //set JSON delimiter + // set JSON delimiter if i != 0 { fmt.Fprint(os.Stdout, ",") } k := Snapshot{ - ID: sn.ID().Str(), - Date: sn.Time.Format(TimeFormat), - Host: sn.Hostname, - Tags: sn.Tags, - Directories: sn.Paths} + Time: sn.Time, + Parent: sn.Parent.PrintStr(), + Tree: sn.Tree.PrintStr(), + Paths: sn.Paths, + Hostname: sn.Hostname, + Username: sn.Username, + UID: sn.UID, + GID: sn.GID, + Excludes: sn.Excludes, + Tags: sn.Tags, + ID: sn.ID().String()} err := enc.Encode(k) if err != nil { @@ -188,7 +201,7 @@ func printSnapshotsJSON(list []*restic.Snapshot) error { } - //set JSON postfix + // set JSON postfix fmt.Fprint(os.Stdout, "]") return nil diff --git a/src/restic/id.go b/src/restic/id.go index c64508a4e..efb37b708 100644 --- a/src/restic/id.go +++ b/src/restic/id.go @@ -69,6 +69,15 @@ func (id *ID) Str() string { return hex.EncodeToString(id[:shortStr]) } +// PrintStr returns the string version of id if available. +func (id *ID) PrintStr() string { + if id == nil || id.IsNull() { + return "" + } + + return hex.EncodeToString(id[:]) +} + // IsNull returns true iff id only consists of null bytes. func (id ID) IsNull() bool { var nullID ID