Skip to content

Commit

Permalink
complete RESITC_HOST environment handling & test
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelEischer committed Apr 24, 2024
1 parent f84f1c8 commit ded8f67
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
3 changes: 2 additions & 1 deletion changelog/unreleased/issue-4733
Expand Up @@ -6,4 +6,5 @@ grouoping snapshots. They now permit selecting the hostname via the
environment variable `RESTIC_HOST`. `--host` still takes precedence over the
environment variable.

https://github.com/restic/restic/issues/4733
https://github.com/restic/restic/issues/4733
https://github.com/restic/restic/pull/4734
16 changes: 6 additions & 10 deletions cmd/restic/find.go
Expand Up @@ -19,11 +19,9 @@ func initMultiSnapshotFilter(flags *pflag.FlagSet, filt *restic.SnapshotFilter,
flags.Var(&filt.Tags, "tag", "only consider snapshots including `tag[,tag,...]` (can be specified multiple times)")
flags.StringArrayVar(&filt.Paths, "path", nil, "only consider snapshots including this (absolute) `path` (can be specified multiple times)")

if len(filt.Hosts) == 0 {
// parse host from env, if not exists or empty the default value will be used
if host := os.Getenv("RESTIC_HOST"); host != "" {
filt.Hosts = []string{host}
}
// set default based on env if set
if host := os.Getenv("RESTIC_HOST"); host != "" {
filt.Hosts = []string{host}
}
}

Expand All @@ -34,11 +32,9 @@ func initSingleSnapshotFilter(flags *pflag.FlagSet, filt *restic.SnapshotFilter)
flags.Var(&filt.Tags, "tag", "only consider snapshots including `tag[,tag,...]`, when snapshot ID \"latest\" is given (can be specified multiple times)")
flags.StringArrayVar(&filt.Paths, "path", nil, "only consider snapshots including this (absolute) `path`, when snapshot ID \"latest\" is given (can be specified multiple times)")

if len(filt.Hosts) == 0 {
// parse host from env, if not exists or empty the default value will be used
if host := os.Getenv("RESTIC_HOST"); host != "" {
filt.Hosts = []string{host}
}
// set default based on env if set
if host := os.Getenv("RESTIC_HOST"); host != "" {
filt.Hosts = []string{host}
}
}

Expand Down
55 changes: 55 additions & 0 deletions cmd/restic/find_test.go
@@ -0,0 +1,55 @@
package main

import (
"testing"

"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
"github.com/spf13/pflag"
)

func TestSnapshotFilter(t *testing.T) {
for _, test := range []struct {
name string
args []string
expected []string
env string
}{
{
"no value",
[]string{},
nil,
"",
},
{
"args only",
[]string{"--host", "abc"},
[]string{"abc"},
"",
},
{
"env default",
[]string{},
[]string{"def"},
"def",
},
{
"both",
[]string{"--host", "abc"},
[]string{"abc"},
"def",
},
} {
t.Run(test.name, func(t *testing.T) {
t.Setenv("RESTIC_HOST", test.env)

set := pflag.NewFlagSet("test", pflag.PanicOnError)
flt := &restic.SnapshotFilter{}
initMultiSnapshotFilter(set, flt, false)
err := set.Parse(test.args)
rtest.OK(t, err)

rtest.Equals(t, test.expected, flt.Hosts, "unexpected hosts")
})
}
}

0 comments on commit ded8f67

Please sign in to comment.