2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-02 11:46:36 +00:00

Changed cmd_snapshots to be testable (no more using os.Stdout)

This commit is contained in:
Pauline Middelink 2017-03-05 05:24:11 +01:00
parent bbcab800c9
commit 7238a3ee89

View File

@ -2,7 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "io"
"restic/errors" "restic/errors"
"sort" "sort"
@ -64,7 +64,7 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
for id := range repo.List(restic.SnapshotFile, done) { for id := range repo.List(restic.SnapshotFile, done) {
sn, err := restic.LoadSnapshot(repo, id) sn, err := restic.LoadSnapshot(repo, id)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error loading snapshot %s: %v\n", id, err) Warnf("error loading snapshot %s: %v\n", id, err)
continue continue
} }
@ -85,19 +85,19 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
} }
if gopts.JSON { if gopts.JSON {
err := printSnapshotsJSON(list) err := printSnapshotsJSON(gopts.stdout, list)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "error printing snapshot: %v\n", err) Warnf("error printing snapshot: %v\n", err)
} }
return nil return nil
} }
printSnapshotsReadable(list) printSnapshotsReadable(gopts.stdout, list)
return nil return nil
} }
// printSnapshotsReadable prints a text table of the snapshots in list to stdout. // printSnapshotsReadable prints a text table of the snapshots in list to stdout.
func printSnapshotsReadable(list []*restic.Snapshot) { func printSnapshotsReadable(stdout io.Writer, list []*restic.Snapshot) {
tab := NewTable() tab := NewTable()
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %-3s %s", "ID", "Date", "Host", "Tags", "", "Directory") tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %-3s %s", "ID", "Date", "Host", "Tags", "", "Directory")
@ -146,9 +146,7 @@ func printSnapshotsReadable(list []*restic.Snapshot) {
} }
} }
tab.Write(os.Stdout) tab.Write(stdout)
return
} }
// Snapshot helps to print Snaphots as JSON // Snapshot helps to print Snaphots as JSON
@ -159,7 +157,7 @@ type Snapshot struct {
} }
// printSnapshotsJSON writes the JSON representation of list to stdout. // printSnapshotsJSON writes the JSON representation of list to stdout.
func printSnapshotsJSON(list []*restic.Snapshot) error { func printSnapshotsJSON(stdout io.Writer, list []*restic.Snapshot) error {
var snapshots []Snapshot var snapshots []Snapshot
@ -172,6 +170,6 @@ func printSnapshotsJSON(list []*restic.Snapshot) error {
snapshots = append(snapshots, k) snapshots = append(snapshots, k)
} }
return json.NewEncoder(os.Stdout).Encode(snapshots) return json.NewEncoder(stdout).Encode(snapshots)
} }