From 11738db8f9f8a1387d296c98a3095149c736d9ba Mon Sep 17 00:00:00 2001 From: ibib Date: Sun, 12 Feb 2017 12:44:47 +0100 Subject: [PATCH] follows fd0 suggestion --- src/cmds/restic/cmd_snapshots.go | 52 ++++++-------------------------- src/restic/id.go | 9 ------ 2 files changed, 10 insertions(+), 51 deletions(-) diff --git a/src/cmds/restic/cmd_snapshots.go b/src/cmds/restic/cmd_snapshots.go index d5ef8ffe3..5671b92eb 100644 --- a/src/cmds/restic/cmd_snapshots.go +++ b/src/cmds/restic/cmd_snapshots.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "fmt" "os" "restic/errors" @@ -9,8 +8,8 @@ import ( "github.com/spf13/cobra" + "encoding/json" "restic" - "time" ) var cmdSnapshots = &cobra.Command{ @@ -154,56 +153,25 @@ func printSnapshotsReadable(list []*restic.Snapshot) { // Snapshot helps to print Snaphots as JSON type Snapshot struct { - 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"` + *restic.Snapshot + + 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 - fmt.Fprint(os.Stdout, "[") + var snapshots []Snapshot - for i, sn := range list { - - // set JSON delimiter - if i != 0 { - fmt.Fprint(os.Stdout, ",") - } + for _, sn := range list { k := Snapshot{ - 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 { - return err + Snapshot: sn, + ID: sn.ID().String(), } - + snapshots = append(snapshots, k) } - // set JSON postfix - fmt.Fprint(os.Stdout, "]") - - return nil + return json.NewEncoder(os.Stdout).Encode(snapshots) } diff --git a/src/restic/id.go b/src/restic/id.go index efb37b708..c64508a4e 100644 --- a/src/restic/id.go +++ b/src/restic/id.go @@ -69,15 +69,6 @@ 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