From 334d8ce724690b20c923a86cda90e1d5342daba3 Mon Sep 17 00:00:00 2001 From: Quentin Lemaire Date: Mon, 28 Dec 2020 16:16:10 +0100 Subject: [PATCH 1/4] feat(tags): Create Flatten() method --- internal/restic/tag_list.go | 14 ++++++++++ internal/restic/tag_list_test.go | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 internal/restic/tag_list_test.go diff --git a/internal/restic/tag_list.go b/internal/restic/tag_list.go index 553ea332c..4d57cb14b 100644 --- a/internal/restic/tag_list.go +++ b/internal/restic/tag_list.go @@ -40,6 +40,20 @@ func (l TagLists) String() string { return fmt.Sprintf("%v", []TagList(l)) } +// Flatten returns the list of all tags provided in TagLists +func (l TagLists) Flatten() (tags TagList) { + tags = make([]string, 0) + for _, list := range l { + for _, tag := range list { + if tag != "" { + tags = append(tags, tag) + } + } + } + + return tags +} + // Set updates the TagList's value. func (l *TagLists) Set(s string) error { *l = append(*l, splitTagList(s)) diff --git a/internal/restic/tag_list_test.go b/internal/restic/tag_list_test.go new file mode 100644 index 000000000..d7a93474d --- /dev/null +++ b/internal/restic/tag_list_test.go @@ -0,0 +1,45 @@ +package restic + +import ( + rtest "github.com/restic/restic/internal/test" + "testing" +) + +func TestTagLists_Flatten(t *testing.T) { + tests := []struct { + name string + l TagLists + want TagList + }{ + { + name: "4 tags", + l: TagLists{ + TagList{ + "tag1", + "tag2", + }, + TagList{ + "tag3", + "tag4", + }, + }, + want: TagList{"tag1", "tag2", "tag3", "tag4"}, + }, + { + name: "No tags", + l: nil, + want: TagList{}, + }, + { + name: "Empty tags", + l: TagLists{[]string{""}}, + want: TagList{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := tt.l.Flatten() + rtest.Equals(t, got, tt.want) + }) + } +} From e88f3fb80ceaf3a7b3483ff65cce8f158ead1c92 Mon Sep 17 00:00:00 2001 From: Quentin Lemaire Date: Mon, 28 Dec 2020 16:33:10 +0100 Subject: [PATCH 2/4] fix(cmd_backup): Use restic.TagLists --- cmd/restic/cmd_backup.go | 4 ++-- cmd/restic/integration_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index 0f4dabc84..237f15c09 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -83,7 +83,7 @@ type BackupOptions struct { ExcludeLargerThan string Stdin bool StdinFilename string - Tags restic.TagList + Tags restic.TagLists Host string FilesFrom []string FilesFromVerbatim []string @@ -682,7 +682,7 @@ func runBackup(opts BackupOptions, gopts GlobalOptions, term *termstatus.Termina snapshotOpts := archiver.SnapshotOptions{ Excludes: opts.Excludes, - Tags: opts.Tags, + Tags: opts.Tags.Flatten(), Time: timeStamp, Hostname: opts.Host, ParentSnapshot: *parentSnapshotID, diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index be8ec2bd9..3d6175318 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -662,7 +662,7 @@ func TestBackupTags(t *testing.T) { "expected no tags, got %v", newest.Tags) parent := newest - opts.Tags = []string{"NL"} + opts.Tags = restic.TagLists{[]string{"NL"}} testRunBackup(t, "", []string{env.testdata}, opts, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) From 25ecf9eafbeb0fcd68b5643fe21b5fba871e8b05 Mon Sep 17 00:00:00 2001 From: Quentin Lemaire Date: Tue, 29 Dec 2020 10:59:09 +0100 Subject: [PATCH 3/4] fix(cmd_tag): Use restic.TagLists --- cmd/restic/cmd_tag.go | 8 ++++---- cmd/restic/integration_test.go | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmd/restic/cmd_tag.go b/cmd/restic/cmd_tag.go index 4ff94b01d..13842c077 100644 --- a/cmd/restic/cmd_tag.go +++ b/cmd/restic/cmd_tag.go @@ -38,9 +38,9 @@ type TagOptions struct { Hosts []string Paths []string Tags restic.TagLists - SetTags restic.TagList - AddTags restic.TagList - RemoveTags restic.TagList + SetTags restic.TagLists + AddTags restic.TagLists + RemoveTags restic.TagLists } var tagOptions TagOptions @@ -130,7 +130,7 @@ func runTag(opts TagOptions, gopts GlobalOptions, args []string) error { ctx, cancel := context.WithCancel(gopts.ctx) defer cancel() for sn := range FindFilteredSnapshots(ctx, repo, opts.Hosts, opts.Tags, opts.Paths, args) { - changed, err := changeTags(ctx, repo, sn, opts.SetTags, opts.AddTags, opts.RemoveTags) + changed, err := changeTags(ctx, repo, sn, opts.SetTags.Flatten(), opts.AddTags.Flatten(), opts.RemoveTags.Flatten()) if err != nil { Warnf("unable to modify the tags for snapshot ID %q, ignoring: %v\n", sn.ID(), err) continue diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 3d6175318..67dad2ecd 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -840,7 +840,7 @@ func TestTag(t *testing.T) { "expected original ID to be nil, got %v", newest.Original) originalID := *newest.ID - testRunTag(t, TagOptions{SetTags: []string{"NL"}}, env.gopts) + testRunTag(t, TagOptions{SetTags: restic.TagLists{[]string{"NL"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) rtest.Assert(t, newest != nil, "expected a new backup, got nil") @@ -850,7 +850,7 @@ func TestTag(t *testing.T) { rtest.Assert(t, *newest.Original == originalID, "expected original ID to be set to the first snapshot id") - testRunTag(t, TagOptions{AddTags: []string{"CH"}}, env.gopts) + testRunTag(t, TagOptions{AddTags: restic.TagLists{[]string{"CH"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) rtest.Assert(t, newest != nil, "expected a new backup, got nil") @@ -860,7 +860,7 @@ func TestTag(t *testing.T) { rtest.Assert(t, *newest.Original == originalID, "expected original ID to be set to the first snapshot id") - testRunTag(t, TagOptions{RemoveTags: []string{"NL"}}, env.gopts) + testRunTag(t, TagOptions{RemoveTags: restic.TagLists{[]string{"NL"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) rtest.Assert(t, newest != nil, "expected a new backup, got nil") @@ -870,8 +870,8 @@ func TestTag(t *testing.T) { rtest.Assert(t, *newest.Original == originalID, "expected original ID to be set to the first snapshot id") - testRunTag(t, TagOptions{AddTags: []string{"US", "RU"}}, env.gopts) - testRunTag(t, TagOptions{RemoveTags: []string{"CH", "US", "RU"}}, env.gopts) + testRunTag(t, TagOptions{AddTags: restic.TagLists{[]string{"US", "RU"}}}, env.gopts) + testRunTag(t, TagOptions{RemoveTags: restic.TagLists{[]string{"CH", "US", "RU"}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) rtest.Assert(t, newest != nil, "expected a new backup, got nil") @@ -882,7 +882,7 @@ func TestTag(t *testing.T) { "expected original ID to be set to the first snapshot id") // Check special case of removing all tags. - testRunTag(t, TagOptions{SetTags: []string{""}}, env.gopts) + testRunTag(t, TagOptions{SetTags: restic.TagLists{[]string{""}}}, env.gopts) testRunCheck(t, env.gopts) newest, _ = testRunSnapshots(t, env.gopts) rtest.Assert(t, newest != nil, "expected a new backup, got nil") From 873505ed3bfed5bfa249f881b8ba33a41d11afa2 Mon Sep 17 00:00:00 2001 From: Quentin Lemaire Date: Mon, 28 Dec 2020 16:48:44 +0100 Subject: [PATCH 4/4] Update related changelog --- changelog/unreleased/issue-2688 | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog/unreleased/issue-2688 b/changelog/unreleased/issue-2688 index 2f6532f57..bbaddb15b 100644 --- a/changelog/unreleased/issue-2688 +++ b/changelog/unreleased/issue-2688 @@ -24,3 +24,4 @@ as well as the list of snapshots to process. https://github.com/restic/restic/issues/2688 https://github.com/restic/restic/pull/2690 +https://github.com/restic/restic/pull/3197