From 137d20a06a5491d92b35beefee83af5917934daa Mon Sep 17 00:00:00 2001 From: "Leo R. Lundgren" Date: Mon, 5 Oct 2020 13:04:24 +0200 Subject: [PATCH] snapshots: Make --json output [] instead of null when no snapshots --- changelog/unreleased/issue-2979 | 9 +++++++++ cmd/restic/cmd_snapshots.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/issue-2979 diff --git a/changelog/unreleased/issue-2979 b/changelog/unreleased/issue-2979 new file mode 100644 index 000000000..456e2901c --- /dev/null +++ b/changelog/unreleased/issue-2979 @@ -0,0 +1,9 @@ +Bugfix: Make snapshots --json output [] instead of null when no snapshots + +Restic previously output `null` instead of `[]` for the `--json snapshots` +command, when there were no snapshots in the repository. This caused some +minor problems when parsing the output, but is now fixed such that `[]` is +output when the list of snapshots is empty. + +https://github.com/restic/restic/issues/2979 +https://github.com/restic/restic/pull/2984 diff --git a/cmd/restic/cmd_snapshots.go b/cmd/restic/cmd_snapshots.go index d49e254ec..cc58479c2 100644 --- a/cmd/restic/cmd_snapshots.go +++ b/cmd/restic/cmd_snapshots.go @@ -298,7 +298,7 @@ type SnapshotGroup struct { // printSnapshotsJSON writes the JSON representation of list to stdout. func printSnapshotGroupJSON(stdout io.Writer, snGroups map[string]restic.Snapshots, grouped bool) error { if grouped { - var snapshotGroups []SnapshotGroup + snapshotGroups := []SnapshotGroup{} for k, list := range snGroups { var key restic.SnapshotGroupKey @@ -330,7 +330,7 @@ func printSnapshotGroupJSON(stdout io.Writer, snGroups map[string]restic.Snapsho } // Old behavior - var snapshots []Snapshot + snapshots := []Snapshot{} for _, list := range snGroups { for _, sn := range list {