From e1f752217401fbbc4d609c177b0379b4440dbcaa Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Mon, 31 Mar 2025 18:04:54 +0200 Subject: [PATCH] forget: fix ignored RESTIC_HOST environment variable --- changelog/unreleased/issue-5325 | 7 +++++++ cmd/restic/cmd_forget.go | 3 ++- cmd/restic/cmd_forget_test.go | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/issue-5325 diff --git a/changelog/unreleased/issue-5325 b/changelog/unreleased/issue-5325 new file mode 100644 index 000000000..883391a0a --- /dev/null +++ b/changelog/unreleased/issue-5325 @@ -0,0 +1,7 @@ +Bugfix: Correctly handle `RESTIC_HOST` in `forget` command + +The `forget` command did not use the host name from the `RESTIC_HOST` +environment variable. This has been fixed. + +https://github.com/restic/restic/issues/5325 +https://github.com/restic/restic/pull/5327 diff --git a/cmd/restic/cmd_forget.go b/cmd/restic/cmd_forget.go index 2bfacc4de..3a410e223 100644 --- a/cmd/restic/cmd_forget.go +++ b/cmd/restic/cmd_forget.go @@ -137,13 +137,14 @@ func (opts *ForgetOptions) AddFlags(f *pflag.FlagSet) { f.Var(&opts.KeepTags, "keep-tag", "keep snapshots with this `taglist` (can be specified multiple times)") f.BoolVar(&opts.UnsafeAllowRemoveAll, "unsafe-allow-remove-all", false, "allow deleting all snapshots of a snapshot group") - initMultiSnapshotFilter(f, &opts.SnapshotFilter, false) f.StringArrayVar(&opts.Hosts, "hostname", nil, "only consider snapshots with the given `hostname` (can be specified multiple times)") err := f.MarkDeprecated("hostname", "use --host") if err != nil { // MarkDeprecated only returns an error when the flag is not found panic(err) } + // must be defined after `--hostname` to not override the default value from the environment + initMultiSnapshotFilter(f, &opts.SnapshotFilter, false) f.BoolVarP(&opts.Compact, "compact", "c", false, "use compact output format") opts.GroupBy = restic.SnapshotGroupByOptions{Host: true, Path: true} diff --git a/cmd/restic/cmd_forget_test.go b/cmd/restic/cmd_forget_test.go index ddeef028a..0d60f7445 100644 --- a/cmd/restic/cmd_forget_test.go +++ b/cmd/restic/cmd_forget_test.go @@ -5,6 +5,7 @@ import ( "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" + "github.com/spf13/pflag" ) func TestForgetPolicyValues(t *testing.T) { @@ -92,3 +93,10 @@ func TestForgetOptionValues(t *testing.T) { } } } + +func TestForgetHostnameDefaulting(t *testing.T) { + t.Setenv("RESTIC_HOST", "testhost") + opts := ForgetOptions{} + opts.AddFlags(pflag.NewFlagSet("test", pflag.ContinueOnError)) + rtest.Equals(t, []string{"testhost"}, opts.Hosts) +}