Fix 'forget' command with tags

This commit is contained in:
Alexander Neumann 2016-09-13 20:56:18 +02:00
parent cfdf4c92f7
commit a77c615909
4 changed files with 31 additions and 65 deletions

View file

@ -49,20 +49,38 @@ func (cmd CmdForget) Usage() string {
func printSnapshots(w io.Writer, snapshots restic.Snapshots) { func printSnapshots(w io.Writer, snapshots restic.Snapshots) {
tab := NewTable() tab := NewTable()
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %s", "ID", "Date", "Host", "Directory") tab.Header = fmt.Sprintf("%-8s %-19s %-10s %-10s %s", "ID", "Date", "Host", "Tags", "Directory")
tab.RowFormat = "%-8s %-19s %-10s %s" tab.RowFormat = "%-8s %-19s %-10s %-10s %s"
for _, sn := range snapshots { for _, sn := range snapshots {
if len(sn.Paths) == 0 { if len(sn.Paths) == 0 {
continue continue
} }
id := sn.ID()
tab.Rows = append(tab.Rows, []interface{}{id.Str(), sn.Time.Format(TimeFormat), sn.Hostname, sn.Paths[0]})
if len(sn.Paths) > 1 { firstTag := ""
for _, path := range sn.Paths[1:] { if len(sn.Tags) > 0 {
tab.Rows = append(tab.Rows, []interface{}{"", "", "", path}) firstTag = sn.Tags[0]
}
tab.Rows = append(tab.Rows, []interface{}{sn.ID().Str(), sn.Time.Format(TimeFormat), sn.Hostname, firstTag, sn.Paths[0]})
rows := len(sn.Paths)
if len(sn.Tags) > rows {
rows = len(sn.Tags)
}
for i := 1; i < rows; i++ {
path := ""
if len(sn.Paths) > i {
path = sn.Paths[i]
} }
tag := ""
if len(sn.Tags) > i {
tag = sn.Tags[i]
}
tab.Rows = append(tab.Rows, []interface{}{"", "", "", tag, path})
} }
} }

View file

@ -177,11 +177,15 @@ func (f *filter) keepTags(tags []string) {
return return
} }
unprocessed := f.Unprocessed[:0]
for _, sn := range f.Unprocessed { for _, sn := range f.Unprocessed {
if sn.HasTags(tags) { if sn.HasTags(tags) {
f.Keep = append(f.Keep, sn) f.Keep = append(f.Keep, sn)
continue
} }
unprocessed = append(unprocessed, sn)
} }
f.Unprocessed = unprocessed
} }
// keepLast marks the last n snapshots as to be kept. // keepLast marks the last n snapshots as to be kept.

View file

@ -233,10 +233,10 @@ func TestApplyPolicy(t *testing.T) {
} }
for _, sn := range keep { for _, sn := range keep {
t.Logf("test %d: keep snapshot at %v\n", i, sn.Time) t.Logf("test %d: keep snapshot at %v %s\n", i, sn.Time, sn.Tags)
} }
for _, sn := range remove { for _, sn := range remove {
t.Logf("test %d: forget snapshot at %v\n", i, sn.Time) t.Logf("test %d: forget snapshot at %v %s\n", i, sn.Time, sn.Tags)
} }
goldenFilename := filepath.Join("testdata", fmt.Sprintf("expired_snapshots_%d", i)) goldenFilename := filepath.Join("testdata", fmt.Sprintf("expired_snapshots_%d", i))

View file

@ -7,14 +7,6 @@
"foo" "foo"
] ]
}, },
{
"time": "2014-11-12T10:20:30Z",
"tree": null,
"paths": null,
"tags": [
"foo"
]
},
{ {
"time": "2014-11-10T10:20:30Z", "time": "2014-11-10T10:20:30Z",
"tree": null, "tree": null,
@ -23,14 +15,6 @@
"foo" "foo"
] ]
}, },
{
"time": "2014-11-08T10:20:30Z",
"tree": null,
"paths": null,
"tags": [
"foo"
]
},
{ {
"time": "2014-10-22T10:20:30Z", "time": "2014-10-22T10:20:30Z",
"tree": null, "tree": null,
@ -39,14 +23,6 @@
"foo" "foo"
] ]
}, },
{
"time": "2014-10-20T10:20:30Z",
"tree": null,
"paths": null,
"tags": [
"foo"
]
},
{ {
"time": "2014-10-11T10:20:30Z", "time": "2014-10-11T10:20:30Z",
"tree": null, "tree": null,
@ -55,14 +31,6 @@
"foo" "foo"
] ]
}, },
{
"time": "2014-10-10T10:20:30Z",
"tree": null,
"paths": null,
"tags": [
"foo"
]
},
{ {
"time": "2014-10-09T10:20:30Z", "time": "2014-10-09T10:20:30Z",
"tree": null, "tree": null,
@ -71,14 +39,6 @@
"foo" "foo"
] ]
}, },
{
"time": "2014-10-08T10:20:30Z",
"tree": null,
"paths": null,
"tags": [
"foo"
]
},
{ {
"time": "2014-10-06T10:20:30Z", "time": "2014-10-06T10:20:30Z",
"tree": null, "tree": null,
@ -87,14 +47,6 @@
"foo" "foo"
] ]
}, },
{
"time": "2014-10-05T10:20:30Z",
"tree": null,
"paths": null,
"tags": [
"foo"
]
},
{ {
"time": "2014-10-02T10:20:30Z", "time": "2014-10-02T10:20:30Z",
"tree": null, "tree": null,
@ -102,13 +54,5 @@
"tags": [ "tags": [
"foo" "foo"
] ]
},
{
"time": "2014-10-01T10:20:30Z",
"tree": null,
"paths": null,
"tags": [
"foo"
]
} }
] ]