From 7689d6c67965d4ea7ea8f278282ee63a6cec7312 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 3 Sep 2022 00:06:38 +0200 Subject: [PATCH 1/3] normalize help text for host, tag and path options --- cmd/restic/cmd_copy.go | 6 +++--- cmd/restic/cmd_dump.go | 6 +++--- cmd/restic/cmd_find.go | 6 +++--- cmd/restic/cmd_mount.go | 2 +- cmd/restic/cmd_restore.go | 4 ++-- cmd/restic/cmd_snapshots.go | 2 +- cmd/restic/cmd_tag.go | 6 +++--- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/cmd/restic/cmd_copy.go b/cmd/restic/cmd_copy.go index 98007c8d2..de064580a 100644 --- a/cmd/restic/cmd_copy.go +++ b/cmd/restic/cmd_copy.go @@ -51,9 +51,9 @@ func init() { f := cmdCopy.Flags() initSecondaryRepoOptions(f, ©Options.secondaryRepoOptions, "destination", "to copy snapshots from") - f.StringArrayVarP(©Options.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when no snapshot ID is given (can be specified multiple times)") - f.Var(©Options.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot ID is given") - f.StringArrayVar(©Options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot ID is given") + f.StringArrayVarP(©Options.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") + f.Var(©Options.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") + f.StringArrayVar(©Options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") } func runCopy(opts CopyOptions, gopts GlobalOptions, args []string) error { diff --git a/cmd/restic/cmd_dump.go b/cmd/restic/cmd_dump.go index 993072f9c..40d62ef0f 100644 --- a/cmd/restic/cmd_dump.go +++ b/cmd/restic/cmd_dump.go @@ -52,9 +52,9 @@ func init() { cmdRoot.AddCommand(cmdDump) flags := cmdDump.Flags() - flags.StringArrayVarP(&dumpOptions.Hosts, "host", "H", nil, `only consider snapshots for this host when the snapshot ID is "latest" (can be specified multiple times)`) - flags.Var(&dumpOptions.Tags, "tag", "only consider snapshots which include this `taglist` for snapshot ID \"latest\"") - flags.StringArrayVar(&dumpOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` for snapshot ID \"latest\"") + flags.StringArrayVarP(&dumpOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when snapshot ID \"latest\" is given (can be specified multiple times)") + flags.Var(&dumpOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when snapshot ID \"latest\" is given (can be specified multiple times)") + flags.StringArrayVar(&dumpOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") flags.StringVarP(&dumpOptions.Archive, "archive", "a", "tar", "set archive `format` as \"tar\" or \"zip\"") } diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index 7171314e2..7f2aa2d7f 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -72,9 +72,9 @@ func init() { f.BoolVarP(&findOptions.CaseInsensitive, "ignore-case", "i", false, "ignore case for pattern") f.BoolVarP(&findOptions.ListLong, "long", "l", false, "use a long listing format showing size and mode") - f.StringArrayVarP(&findOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when no snapshot ID is given (can be specified multiple times)") - f.Var(&findOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot-ID is given") - f.StringArrayVar(&findOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot-ID is given") + f.StringArrayVarP(&findOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") + f.Var(&findOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") + f.StringArrayVar(&findOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") } type findPattern struct { diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index 747316f9f..f1747054a 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -94,7 +94,7 @@ func init() { mountFlags.BoolVar(&mountOptions.NoDefaultPermissions, "no-default-permissions", false, "for 'allow-other', ignore Unix permissions and allow users to read all snapshot files") mountFlags.StringArrayVarP(&mountOptions.Hosts, "host", "H", nil, `only consider snapshots for this host (can be specified multiple times)`) - mountFlags.Var(&mountOptions.Tags, "tag", "only consider snapshots which include this `taglist`") + mountFlags.Var(&mountOptions.Tags, "tag", "only consider snapshots which include this `taglist` (can be specified multiple times)") mountFlags.StringArrayVar(&mountOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`") mountFlags.StringArrayVar(&mountOptions.PathTemplates, "path-template", nil, "set `template` for path names (can be specified multiple times)") diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index addd36661..1146cdca9 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -60,8 +60,8 @@ func init() { flags.StringVarP(&restoreOptions.Target, "target", "t", "", "directory to extract data to") flags.StringArrayVarP(&restoreOptions.Hosts, "host", "H", nil, `only consider snapshots for this host when the snapshot ID is "latest" (can be specified multiple times)`) - flags.Var(&restoreOptions.Tags, "tag", "only consider snapshots which include this `taglist` for snapshot ID \"latest\"") - flags.StringArrayVar(&restoreOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` for snapshot ID \"latest\"") + flags.Var(&restoreOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when snapshot ID \"latest\" is given (can be specified multiple times)") + flags.StringArrayVar(&restoreOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") flags.BoolVar(&restoreOptions.Verify, "verify", false, "verify restored files content") } diff --git a/cmd/restic/cmd_snapshots.go b/cmd/restic/cmd_snapshots.go index ed201bf65..44e9a090b 100644 --- a/cmd/restic/cmd_snapshots.go +++ b/cmd/restic/cmd_snapshots.go @@ -49,7 +49,7 @@ func init() { f := cmdSnapshots.Flags() f.StringArrayVarP(&snapshotOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") f.Var(&snapshotOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - f.StringArrayVar(&snapshotOptions.Paths, "path", nil, "only consider snapshots for this `path` (can be specified multiple times)") + f.StringArrayVar(&snapshotOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") f.BoolVarP(&snapshotOptions.Compact, "compact", "c", false, "use compact output format") f.BoolVar(&snapshotOptions.Last, "last", false, "only show the last snapshot for each host and path") err := f.MarkDeprecated("last", "use --latest 1") diff --git a/cmd/restic/cmd_tag.go b/cmd/restic/cmd_tag.go index 1b99a4d56..f2e29679b 100644 --- a/cmd/restic/cmd_tag.go +++ b/cmd/restic/cmd_tag.go @@ -53,9 +53,9 @@ func init() { tagFlags.Var(&tagOptions.AddTags, "add", "`tags` which will be added to the existing tags in the format `tag[,tag,...]` (can be given multiple times)") tagFlags.Var(&tagOptions.RemoveTags, "remove", "`tags` which will be removed from the existing tags in the format `tag[,tag,...]` (can be given multiple times)") - tagFlags.StringArrayVarP(&tagOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when no snapshot ID is given (can be specified multiple times)") - tagFlags.Var(&tagOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot-ID is given") - tagFlags.StringArrayVar(&tagOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot-ID is given") + tagFlags.StringArrayVarP(&tagOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") + tagFlags.Var(&tagOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") + tagFlags.StringArrayVar(&tagOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") } func changeTags(ctx context.Context, repo *repository.Repository, sn *restic.Snapshot, setTags, addTags, removeTags []string) (bool, error) { From d6309961c5f52f16c79ce53de02d0aa5bf385610 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 3 Sep 2022 00:19:19 +0200 Subject: [PATCH 2/3] deduplicate the snapshot filter cli option setup --- cmd/restic/cmd_copy.go | 8 ++------ cmd/restic/cmd_dump.go | 8 ++------ cmd/restic/cmd_find.go | 8 ++------ cmd/restic/cmd_forget.go | 11 +++-------- cmd/restic/cmd_ls.go | 10 +++------- cmd/restic/cmd_mount.go | 13 ++++--------- cmd/restic/cmd_restore.go | 10 +++------- cmd/restic/cmd_snapshots.go | 8 ++------ cmd/restic/cmd_stats.go | 9 ++------- cmd/restic/cmd_tag.go | 9 ++------- cmd/restic/find.go | 25 +++++++++++++++++++++++++ cmd/restic/integration_test.go | 6 ++++-- 12 files changed, 54 insertions(+), 71 deletions(-) diff --git a/cmd/restic/cmd_copy.go b/cmd/restic/cmd_copy.go index de064580a..bd56d1182 100644 --- a/cmd/restic/cmd_copy.go +++ b/cmd/restic/cmd_copy.go @@ -39,9 +39,7 @@ new destination repository using the "init" command. // CopyOptions bundles all options for the copy command. type CopyOptions struct { secondaryRepoOptions - Hosts []string - Tags restic.TagLists - Paths []string + snapshotFilterOptions } var copyOptions CopyOptions @@ -51,9 +49,7 @@ func init() { f := cmdCopy.Flags() initSecondaryRepoOptions(f, ©Options.secondaryRepoOptions, "destination", "to copy snapshots from") - f.StringArrayVarP(©Options.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") - f.Var(©Options.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - f.StringArrayVar(©Options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") + initMultiSnapshotFilterOptions(f, ©Options.snapshotFilterOptions, true) } func runCopy(opts CopyOptions, gopts GlobalOptions, args []string) error { diff --git a/cmd/restic/cmd_dump.go b/cmd/restic/cmd_dump.go index 40d62ef0f..44dff2783 100644 --- a/cmd/restic/cmd_dump.go +++ b/cmd/restic/cmd_dump.go @@ -40,9 +40,7 @@ Exit status is 0 if the command was successful, and non-zero if there was any er // DumpOptions collects all options for the dump command. type DumpOptions struct { - Hosts []string - Paths []string - Tags restic.TagLists + snapshotFilterOptions Archive string } @@ -52,9 +50,7 @@ func init() { cmdRoot.AddCommand(cmdDump) flags := cmdDump.Flags() - flags.StringArrayVarP(&dumpOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when snapshot ID \"latest\" is given (can be specified multiple times)") - flags.Var(&dumpOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when snapshot ID \"latest\" is given (can be specified multiple times)") - flags.StringArrayVar(&dumpOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") + initMultiSnapshotFilterOptions(flags, &dumpOptions.snapshotFilterOptions, true) flags.StringVarP(&dumpOptions.Archive, "archive", "a", "tar", "set archive `format` as \"tar\" or \"zip\"") } diff --git a/cmd/restic/cmd_find.go b/cmd/restic/cmd_find.go index 7f2aa2d7f..0daee7fca 100644 --- a/cmd/restic/cmd_find.go +++ b/cmd/restic/cmd_find.go @@ -51,9 +51,7 @@ type FindOptions struct { PackID, ShowPackID bool CaseInsensitive bool ListLong bool - Hosts []string - Paths []string - Tags restic.TagLists + snapshotFilterOptions } var findOptions FindOptions @@ -72,9 +70,7 @@ func init() { f.BoolVarP(&findOptions.CaseInsensitive, "ignore-case", "i", false, "ignore case for pattern") f.BoolVarP(&findOptions.ListLong, "long", "l", false, "use a long listing format showing size and mode") - f.StringArrayVarP(&findOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") - f.Var(&findOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - f.StringArrayVar(&findOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") + initMultiSnapshotFilterOptions(f, &findOptions.snapshotFilterOptions, true) } type findPattern struct { diff --git a/cmd/restic/cmd_forget.go b/cmd/restic/cmd_forget.go index 46f3fee0f..29d3c81ff 100644 --- a/cmd/restic/cmd_forget.go +++ b/cmd/restic/cmd_forget.go @@ -52,9 +52,7 @@ type ForgetOptions struct { WithinYearly restic.Duration KeepTags restic.TagLists - Hosts []string - Tags restic.TagLists - Paths []string + snapshotFilterOptions Compact bool // Grouping @@ -81,9 +79,9 @@ func init() { f.VarP(&forgetOptions.WithinWeekly, "keep-within-weekly", "", "keep weekly snapshots that are newer than `duration` (eg. 1y5m7d2h) relative to the latest snapshot") f.VarP(&forgetOptions.WithinMonthly, "keep-within-monthly", "", "keep monthly snapshots that are newer than `duration` (eg. 1y5m7d2h) relative to the latest snapshot") f.VarP(&forgetOptions.WithinYearly, "keep-within-yearly", "", "keep yearly snapshots that are newer than `duration` (eg. 1y5m7d2h) relative to the latest snapshot") - f.Var(&forgetOptions.KeepTags, "keep-tag", "keep snapshots with this `taglist` (can be specified multiple times)") - f.StringArrayVar(&forgetOptions.Hosts, "host", nil, "only consider snapshots with the given `host` (can be specified multiple times)") + + initMultiSnapshotFilterOptions(f, &forgetOptions.snapshotFilterOptions, false) f.StringArrayVar(&forgetOptions.Hosts, "hostname", nil, "only consider snapshots with the given `hostname` (can be specified multiple times)") err := f.MarkDeprecated("hostname", "use --host") if err != nil { @@ -91,9 +89,6 @@ func init() { panic(err) } - f.Var(&forgetOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - - f.StringArrayVar(&forgetOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") f.BoolVarP(&forgetOptions.Compact, "compact", "c", false, "use compact output format") f.StringVarP(&forgetOptions.GroupBy, "group-by", "g", "host,paths", "`group` snapshots by host, paths and/or tags, separated by comma (disable grouping with '')") diff --git a/cmd/restic/cmd_ls.go b/cmd/restic/cmd_ls.go index ec6695714..df7fd7b18 100644 --- a/cmd/restic/cmd_ls.go +++ b/cmd/restic/cmd_ls.go @@ -48,10 +48,8 @@ Exit status is 0 if the command was successful, and non-zero if there was any er // LsOptions collects all options for the ls command. type LsOptions struct { - ListLong bool - Hosts []string - Tags restic.TagLists - Paths []string + ListLong bool + snapshotFilterOptions Recursive bool } @@ -61,10 +59,8 @@ func init() { cmdRoot.AddCommand(cmdLs) flags := cmdLs.Flags() + initSingleSnapshotFilterOptions(flags, &lsOptions.snapshotFilterOptions) flags.BoolVarP(&lsOptions.ListLong, "long", "l", false, "use a long listing format showing size and mode") - flags.StringArrayVarP(&lsOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when snapshot ID \"latest\" is given (can be specified multiple times)") - flags.Var(&lsOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when snapshot ID \"latest\" is given (can be specified multiple times)") - flags.StringArrayVar(&lsOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") flags.BoolVar(&lsOptions.Recursive, "recursive", false, "include files in subfolders of the listed directories") } diff --git a/cmd/restic/cmd_mount.go b/cmd/restic/cmd_mount.go index f1747054a..97aa05ef4 100644 --- a/cmd/restic/cmd_mount.go +++ b/cmd/restic/cmd_mount.go @@ -12,7 +12,6 @@ import ( "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" - "github.com/restic/restic/internal/restic" resticfs "github.com/restic/restic/internal/fs" "github.com/restic/restic/internal/fuse" @@ -76,11 +75,9 @@ type MountOptions struct { OwnerRoot bool AllowOther bool NoDefaultPermissions bool - Hosts []string - Tags restic.TagLists - Paths []string - TimeTemplate string - PathTemplates []string + snapshotFilterOptions + TimeTemplate string + PathTemplates []string } var mountOptions MountOptions @@ -93,9 +90,7 @@ func init() { mountFlags.BoolVar(&mountOptions.AllowOther, "allow-other", false, "allow other users to access the data in the mounted directory") mountFlags.BoolVar(&mountOptions.NoDefaultPermissions, "no-default-permissions", false, "for 'allow-other', ignore Unix permissions and allow users to read all snapshot files") - mountFlags.StringArrayVarP(&mountOptions.Hosts, "host", "H", nil, `only consider snapshots for this host (can be specified multiple times)`) - mountFlags.Var(&mountOptions.Tags, "tag", "only consider snapshots which include this `taglist` (can be specified multiple times)") - mountFlags.StringArrayVar(&mountOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`") + initMultiSnapshotFilterOptions(mountFlags, &mountOptions.snapshotFilterOptions, true) mountFlags.StringArrayVar(&mountOptions.PathTemplates, "path-template", nil, "set `template` for path names (can be specified multiple times)") mountFlags.StringVar(&mountOptions.TimeTemplate, "snapshot-template", time.RFC3339, "set `template` to use for snapshot dirs") diff --git a/cmd/restic/cmd_restore.go b/cmd/restic/cmd_restore.go index 1146cdca9..7561bb22f 100644 --- a/cmd/restic/cmd_restore.go +++ b/cmd/restic/cmd_restore.go @@ -41,10 +41,8 @@ type RestoreOptions struct { Include []string InsensitiveInclude []string Target string - Hosts []string - Paths []string - Tags restic.TagLists - Verify bool + snapshotFilterOptions + Verify bool } var restoreOptions RestoreOptions @@ -59,9 +57,7 @@ func init() { flags.StringArrayVar(&restoreOptions.InsensitiveInclude, "iinclude", nil, "same as `--include` but ignores the casing of filenames") flags.StringVarP(&restoreOptions.Target, "target", "t", "", "directory to extract data to") - flags.StringArrayVarP(&restoreOptions.Hosts, "host", "H", nil, `only consider snapshots for this host when the snapshot ID is "latest" (can be specified multiple times)`) - flags.Var(&restoreOptions.Tags, "tag", "only consider snapshots which include this `taglist`, when snapshot ID \"latest\" is given (can be specified multiple times)") - flags.StringArrayVar(&restoreOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") + initSingleSnapshotFilterOptions(flags, &restoreOptions.snapshotFilterOptions) flags.BoolVar(&restoreOptions.Verify, "verify", false, "verify restored files content") } diff --git a/cmd/restic/cmd_snapshots.go b/cmd/restic/cmd_snapshots.go index 44e9a090b..80a205dcf 100644 --- a/cmd/restic/cmd_snapshots.go +++ b/cmd/restic/cmd_snapshots.go @@ -32,9 +32,7 @@ Exit status is 0 if the command was successful, and non-zero if there was any er // SnapshotOptions bundles all options for the snapshots command. type SnapshotOptions struct { - Hosts []string - Tags restic.TagLists - Paths []string + snapshotFilterOptions Compact bool Last bool // This option should be removed in favour of Latest. Latest int @@ -47,9 +45,7 @@ func init() { cmdRoot.AddCommand(cmdSnapshots) f := cmdSnapshots.Flags() - f.StringArrayVarP(&snapshotOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") - f.Var(&snapshotOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - f.StringArrayVar(&snapshotOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") + initMultiSnapshotFilterOptions(f, &snapshotOptions.snapshotFilterOptions, true) f.BoolVarP(&snapshotOptions.Compact, "compact", "c", false, "use compact output format") f.BoolVar(&snapshotOptions.Last, "last", false, "only show the last snapshot for each host and path") err := f.MarkDeprecated("last", "use --latest 1") diff --git a/cmd/restic/cmd_stats.go b/cmd/restic/cmd_stats.go index a8bcb2b85..d845cb223 100644 --- a/cmd/restic/cmd_stats.go +++ b/cmd/restic/cmd_stats.go @@ -56,10 +56,7 @@ type StatsOptions struct { // the mode of counting to perform (see consts for available modes) countMode string - // filter snapshots by, if given by user - Hosts []string - Tags restic.TagLists - Paths []string + snapshotFilterOptions } var statsOptions StatsOptions @@ -68,9 +65,7 @@ func init() { cmdRoot.AddCommand(cmdStats) f := cmdStats.Flags() f.StringVar(&statsOptions.countMode, "mode", countModeRestoreSize, "counting mode: restore-size (default), files-by-contents, blobs-per-file or raw-data") - f.StringArrayVarP(&statsOptions.Hosts, "host", "H", nil, "only consider snapshots with the given `host` (can be specified multiple times)") - f.Var(&statsOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - f.StringArrayVar(&statsOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") + initMultiSnapshotFilterOptions(f, &statsOptions.snapshotFilterOptions, true) } func runStats(gopts GlobalOptions, args []string) error { diff --git a/cmd/restic/cmd_tag.go b/cmd/restic/cmd_tag.go index f2e29679b..7a2561235 100644 --- a/cmd/restic/cmd_tag.go +++ b/cmd/restic/cmd_tag.go @@ -35,9 +35,7 @@ Exit status is 0 if the command was successful, and non-zero if there was any er // TagOptions bundles all options for the 'tag' command. type TagOptions struct { - Hosts []string - Paths []string - Tags restic.TagLists + snapshotFilterOptions SetTags restic.TagLists AddTags restic.TagLists RemoveTags restic.TagLists @@ -52,10 +50,7 @@ func init() { tagFlags.Var(&tagOptions.SetTags, "set", "`tags` which will replace the existing tags in the format `tag[,tag,...]` (can be given multiple times)") tagFlags.Var(&tagOptions.AddTags, "add", "`tags` which will be added to the existing tags in the format `tag[,tag,...]` (can be given multiple times)") tagFlags.Var(&tagOptions.RemoveTags, "remove", "`tags` which will be removed from the existing tags in the format `tag[,tag,...]` (can be given multiple times)") - - tagFlags.StringArrayVarP(&tagOptions.Hosts, "host", "H", nil, "only consider snapshots for this `host` (can be specified multiple times)") - tagFlags.Var(&tagOptions.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - tagFlags.StringArrayVar(&tagOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") + initMultiSnapshotFilterOptions(tagFlags, &tagOptions.snapshotFilterOptions, true) } func changeTags(ctx context.Context, repo *repository.Repository, sn *restic.Snapshot, setTags, addTags, removeTags []string) (bool, error) { diff --git a/cmd/restic/find.go b/cmd/restic/find.go index 5107ef599..4331856ed 100644 --- a/cmd/restic/find.go +++ b/cmd/restic/find.go @@ -5,8 +5,33 @@ import ( "github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/restic" + "github.com/spf13/pflag" ) +type snapshotFilterOptions struct { + Hosts []string + Tags restic.TagLists + Paths []string +} + +// initMultiSnapshotFilterOptions is used for commands that work on multiple snapshots +func initMultiSnapshotFilterOptions(flags *pflag.FlagSet, options *snapshotFilterOptions, addHostShorthand bool) { + hostShorthand := "H" + if !addHostShorthand { + hostShorthand = "" + } + flags.StringArrayVarP(&options.Hosts, "host", hostShorthand, nil, "only consider snapshots for this `host` (can be specified multiple times)") + flags.Var(&options.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") + flags.StringArrayVar(&options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") +} + +// initSingleSnapshotFilterOptions is used for commands that work on a single snapshot +func initSingleSnapshotFilterOptions(flags *pflag.FlagSet, options *snapshotFilterOptions) { + flags.StringArrayVarP(&options.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when snapshot ID \"latest\" is given (can be specified multiple times)") + flags.Var(&options.Tags, "tag", "only consider snapshots which include this `taglist`, when snapshot ID \"latest\" is given (can be specified multiple times)") + flags.StringArrayVar(&options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") +} + // FindFilteredSnapshots yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots. func FindFilteredSnapshots(ctx context.Context, be restic.Lister, loader restic.LoaderUnpacked, hosts []string, tags []restic.TagList, paths []string, snapshotIDs []string) <-chan *restic.Snapshot { out := make(chan *restic.Snapshot) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index c04a5a2fb..3f1df2544 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -106,8 +106,10 @@ func testRunRestore(t testing.TB, opts GlobalOptions, dir string, snapshotID res func testRunRestoreLatest(t testing.TB, gopts GlobalOptions, dir string, paths []string, hosts []string) { opts := RestoreOptions{ Target: dir, - Hosts: hosts, - Paths: paths, + snapshotFilterOptions: snapshotFilterOptions{ + Hosts: hosts, + Paths: paths, + }, } rtest.OK(t, runRestore(opts, gopts, []string{"latest"})) From 381da0443a47e6b8e1dc19bfe19c1f7fe2ea287e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 10 Sep 2022 23:34:46 +0200 Subject: [PATCH 3/3] tweak snapshot filter descriptions --- cmd/restic/find.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/restic/find.go b/cmd/restic/find.go index 4331856ed..4b429447e 100644 --- a/cmd/restic/find.go +++ b/cmd/restic/find.go @@ -21,15 +21,15 @@ func initMultiSnapshotFilterOptions(flags *pflag.FlagSet, options *snapshotFilte hostShorthand = "" } flags.StringArrayVarP(&options.Hosts, "host", hostShorthand, nil, "only consider snapshots for this `host` (can be specified multiple times)") - flags.Var(&options.Tags, "tag", "only consider snapshots which include this `taglist` in the format `tag[,tag,...]` (can be specified multiple times)") - flags.StringArrayVar(&options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` (can be specified multiple times)") + flags.Var(&options.Tags, "tag", "only consider snapshots including `tag[,tag,...]` (can be specified multiple times)") + flags.StringArrayVar(&options.Paths, "path", nil, "only consider snapshots including this (absolute) `path` (can be specified multiple times)") } // initSingleSnapshotFilterOptions is used for commands that work on a single snapshot func initSingleSnapshotFilterOptions(flags *pflag.FlagSet, options *snapshotFilterOptions) { flags.StringArrayVarP(&options.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when snapshot ID \"latest\" is given (can be specified multiple times)") - flags.Var(&options.Tags, "tag", "only consider snapshots which include this `taglist`, when snapshot ID \"latest\" is given (can be specified multiple times)") - flags.StringArrayVar(&options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") + flags.Var(&options.Tags, "tag", "only consider snapshots including `tag[,tag,...]`, when snapshot ID \"latest\" is given (can be specified multiple times)") + flags.StringArrayVar(&options.Paths, "path", nil, "only consider snapshots including this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)") } // FindFilteredSnapshots yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.