2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00

Fix json tags for grouped snapshot output

This commit will add json tags to the structs for json output, so all
json variables of the snapshot command output are lowercase and
snake-case.

Furthermore it adds some internal code changes based on the feedback in
the pull request #2087.
This commit is contained in:
Jan Niklas Richter 2018-11-30 14:34:19 +01:00 committed by Alexander Neumann
parent cadcab5a19
commit c9fd9b5275

View File

@ -51,9 +51,9 @@ func init() {
} }
type groupKey struct { type groupKey struct {
Hostname string Hostname string `json:"hostname"`
Paths []string Paths []string `json:"paths"`
Tags []string Tags []string `json:"tags"`
} }
func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) error { func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) error {
@ -295,32 +295,33 @@ func PrintSnapshots(stdout io.Writer, list restic.Snapshots, reasons []restic.Ke
// following snapshots belong to. // following snapshots belong to.
// Prints nothing, if we did not group at all. // Prints nothing, if we did not group at all.
func PrintSnapshotGroupHeader(stdout io.Writer, groupKeyJSON string, GroupByTag bool, GroupByHost bool, GroupByPath bool) error { func PrintSnapshotGroupHeader(stdout io.Writer, groupKeyJSON string, GroupByTag bool, GroupByHost bool, GroupByPath bool) error {
if GroupByTag || GroupByHost || GroupByPath { if !GroupByTag && !GroupByHost && !GroupByPath {
var key groupKey return nil
var err error
err = json.Unmarshal([]byte(groupKeyJSON), &key)
if err != nil {
return err
}
// Info
fmt.Fprintf(stdout, "snapshots")
var infoStrings []string
if GroupByTag {
infoStrings = append(infoStrings, "tags ["+strings.Join(key.Tags, ", ")+"]")
}
if GroupByHost {
infoStrings = append(infoStrings, "host ["+key.Hostname+"]")
}
if GroupByPath {
infoStrings = append(infoStrings, "paths ["+strings.Join(key.Paths, ", ")+"]")
}
if infoStrings != nil {
fmt.Fprintf(stdout, " for (%s)", strings.Join(infoStrings, ", "))
}
fmt.Fprintf(stdout, ":\n")
} }
var key groupKey
var err error
err = json.Unmarshal([]byte(groupKeyJSON), &key)
if err != nil {
return err
}
// Info
fmt.Fprintf(stdout, "snapshots")
var infoStrings []string
if GroupByTag {
infoStrings = append(infoStrings, "tags ["+strings.Join(key.Tags, ", ")+"]")
}
if GroupByHost {
infoStrings = append(infoStrings, "host ["+key.Hostname+"]")
}
if GroupByPath {
infoStrings = append(infoStrings, "paths ["+strings.Join(key.Paths, ", ")+"]")
}
if infoStrings != nil {
fmt.Fprintf(stdout, " for (%s)", strings.Join(infoStrings, ", "))
}
fmt.Fprintf(stdout, ":\n")
return nil return nil
} }
@ -335,8 +336,8 @@ type Snapshot struct {
// SnapshotGroup helps to print SnaphotGroups as JSON with their GroupReasons included. // SnapshotGroup helps to print SnaphotGroups as JSON with their GroupReasons included.
type SnapshotGroup struct { type SnapshotGroup struct {
GroupKey groupKey GroupKey groupKey `json:"group_key"`
Snapshots []Snapshot Snapshots []Snapshot `json:"snapshots"`
} }
// printSnapshotsJSON writes the JSON representation of list to stdout. // printSnapshotsJSON writes the JSON representation of list to stdout.
@ -372,21 +373,21 @@ func printSnapshotGroupJSON(stdout io.Writer, snGroups map[string]restic.Snapsho
} }
return json.NewEncoder(stdout).Encode(snapshotGroups) return json.NewEncoder(stdout).Encode(snapshotGroups)
} else {
// Old behavior
var snapshots []Snapshot
for _, list := range snGroups {
for _, sn := range list {
k := Snapshot{
Snapshot: sn,
ID: sn.ID(),
ShortID: sn.ID().Str(),
}
snapshots = append(snapshots, k)
}
}
return json.NewEncoder(stdout).Encode(snapshots)
} }
// Old behavior
var snapshots []Snapshot
for _, list := range snGroups {
for _, sn := range list {
k := Snapshot{
Snapshot: sn,
ID: sn.ID(),
ShortID: sn.ID().Str(),
}
snapshots = append(snapshots, k)
}
}
return json.NewEncoder(stdout).Encode(snapshots)
} }