From bdbe956c5cf73686cece88003d91fa9581203083 Mon Sep 17 00:00:00 2001 From: Dmitriy Morozov Date: Sat, 9 Sep 2017 08:51:10 -0700 Subject: [PATCH 1/3] Add --compact option to snapshots With --compact, snapshots doesn't list directories and puts all tags on a single line. This way each snapshot takes up exactly one line. --- cmd/restic/cmd_forget.go | 4 ++-- cmd/restic/cmd_snapshots.go | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/cmd/restic/cmd_forget.go b/cmd/restic/cmd_forget.go index d3f42d4d0..6bcd65d26 100644 --- a/cmd/restic/cmd_forget.go +++ b/cmd/restic/cmd_forget.go @@ -204,13 +204,13 @@ func runForget(opts ForgetOptions, gopts GlobalOptions, args []string) error { if len(keep) != 0 && !gopts.Quiet { Printf("keep %d snapshots:\n", len(keep)) - PrintSnapshots(globalOptions.stdout, keep) + PrintSnapshots(globalOptions.stdout, keep, false) Printf("\n") } if len(remove) != 0 && !gopts.Quiet { Printf("remove %d snapshots:\n", len(remove)) - PrintSnapshots(globalOptions.stdout, remove) + PrintSnapshots(globalOptions.stdout, remove, false) Printf("\n") } diff --git a/cmd/restic/cmd_snapshots.go b/cmd/restic/cmd_snapshots.go index 6e14a9bec..0ced91b5c 100644 --- a/cmd/restic/cmd_snapshots.go +++ b/cmd/restic/cmd_snapshots.go @@ -28,6 +28,7 @@ type SnapshotOptions struct { Host string Tags restic.TagLists Paths []string + Compact bool } var snapshotOptions SnapshotOptions @@ -39,6 +40,7 @@ func init() { f.StringVarP(&snapshotOptions.Host, "host", "H", "", "only consider snapshots for this `host`") f.Var(&snapshotOptions.Tags, "tag", "only consider snapshots which include this `taglist` (can be specified multiple times)") f.StringArrayVar(&snapshotOptions.Paths, "path", nil, "only consider snapshots for this `path` (can be specified multiple times)") + f.BoolVarP(&snapshotOptions.Compact, "compact", "c", false, "use compact format") } func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) error { @@ -71,13 +73,13 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro } return nil } - PrintSnapshots(gopts.stdout, list) + PrintSnapshots(gopts.stdout, list, opts.Compact) return nil } // PrintSnapshots prints a text table of the snapshots in list to stdout. -func PrintSnapshots(stdout io.Writer, list restic.Snapshots) { +func PrintSnapshots(stdout io.Writer, list restic.Snapshots, compact bool) { // Determine the max widths for host and tag. maxHost, maxTag := 10, 6 @@ -93,8 +95,13 @@ func PrintSnapshots(stdout io.Writer, list restic.Snapshots) { } tab := NewTable() - tab.Header = fmt.Sprintf("%-8s %-19s %-*s %-*s %-3s %s", "ID", "Date", -maxHost, "Host", -maxTag, "Tags", "", "Directory") - tab.RowFormat = fmt.Sprintf("%%-8s %%-19s %%%ds %%%ds %%-3s %%s", -maxHost, -maxTag) + if !compact { + tab.Header = fmt.Sprintf("%-8s %-19s %-*s %-*s %-3s %s", "ID", "Date", -maxHost, "Host", -maxTag, "Tags", "", "Directory") + tab.RowFormat = fmt.Sprintf("%%-8s %%-19s %%%ds %%%ds %%-3s %%s", -maxHost, -maxTag) + } else { + tab.Header = fmt.Sprintf("%-8s %-19s %-*s %-*s", "ID", "Date", -maxHost, "Host", -maxTag, "Tags") + tab.RowFormat = fmt.Sprintf("%%-8s %%-19s %%%ds %%s", -maxHost) + } for _, sn := range list { if len(sn.Paths) == 0 { @@ -116,7 +123,16 @@ func PrintSnapshots(stdout io.Writer, list restic.Snapshots) { treeElement = "┌──" } - tab.Rows = append(tab.Rows, []interface{}{sn.ID().Str(), sn.Time.Format(TimeFormat), sn.Hostname, firstTag, treeElement, sn.Paths[0]}) + if !compact { + tab.Rows = append(tab.Rows, []interface{}{sn.ID().Str(), sn.Time.Format(TimeFormat), sn.Hostname, firstTag, treeElement, sn.Paths[0]}) + } else { + allTags := "" + for _, tag := range sn.Tags { + allTags += tag + " " + } + tab.Rows = append(tab.Rows, []interface{}{sn.ID().Str(), sn.Time.Format(TimeFormat), sn.Hostname, allTags}) + continue + } if len(sn.Tags) > rows { rows = len(sn.Tags) From 7a221f2473527f70e893722cd7ce6ff54cbfe31a Mon Sep 17 00:00:00 2001 From: Dmitriy Morozov Date: Sun, 10 Sep 2017 15:09:28 -0700 Subject: [PATCH 2/3] Run changes through gofmt --- cmd/restic/cmd_snapshots.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/restic/cmd_snapshots.go b/cmd/restic/cmd_snapshots.go index 0ced91b5c..5987b6de8 100644 --- a/cmd/restic/cmd_snapshots.go +++ b/cmd/restic/cmd_snapshots.go @@ -25,9 +25,9 @@ The "snapshots" command lists all snapshots stored in the repository. // SnapshotOptions bundles all options for the snapshots command. type SnapshotOptions struct { - Host string - Tags restic.TagLists - Paths []string + Host string + Tags restic.TagLists + Paths []string Compact bool } From 7d5b17ac72a38d89296d21edb5d215fc6910032a Mon Sep 17 00:00:00 2001 From: Dmitriy Morozov Date: Sun, 10 Sep 2017 15:28:06 -0700 Subject: [PATCH 3/3] Update man page for snapshots --- doc/man/restic-snapshots.1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doc/man/restic-snapshots.1 b/doc/man/restic-snapshots.1 index 84ade8b78..0a82c0e61 100644 --- a/doc/man/restic-snapshots.1 +++ b/doc/man/restic-snapshots.1 @@ -19,6 +19,10 @@ The "snapshots" command lists all snapshots stored in the repository. .SH OPTIONS +.PP +\fB\-c\fP, \fB\-\-compact\fP[=false] + use compact format + .PP \fB\-h\fP, \fB\-\-help\fP[=false] help for snapshots