From 7ddb2faca6a2ff07bf42e586a2671890d2eaf6ce Mon Sep 17 00:00:00 2001 From: ibib Date: Sun, 29 Jan 2017 15:59:08 +0100 Subject: [PATCH] Change JSON output to json.Encode --- src/cmds/restic/cmd_snapshots.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/cmds/restic/cmd_snapshots.go b/src/cmds/restic/cmd_snapshots.go index daabef6ef..cd2210522 100644 --- a/src/cmds/restic/cmd_snapshots.go +++ b/src/cmds/restic/cmd_snapshots.go @@ -152,9 +152,17 @@ type Snapshot struct { //printSnapshotsJSON provides machine redability func printSnapshotsJSON(list []*restic.Snapshot) error { - var response []Snapshot - for _, sn := range list { + enc := json.NewEncoder(os.Stdout) + //set JSON prefix + fmt.Fprint(os.Stdout, "[") + + for i, sn := range list { + + //set JSON delimiter + if i != 0 { + fmt.Fprint(os.Stdout, ",") + } k := Snapshot{ ID: sn.ID().Str(), @@ -163,15 +171,16 @@ func printSnapshotsJSON(list []*restic.Snapshot) error { Tags: sn.Tags, Directories: sn.Paths} - response = append(response, k) + err := enc.Encode(k) + if err != nil { + return err + } + } - output, _ := json.Marshal(response) + //set JSON postfix + fmt.Fprint(os.Stdout, "]") - _, err := fmt.Fprintln(os.Stdout, string(output)) - if err != nil { - return err - } return nil }