wip
This commit is contained in:
parent
6bad560324
commit
ffd7bc1021
6 changed files with 173 additions and 25 deletions
|
@ -19,14 +19,14 @@ type Config struct {
|
||||||
Password string `hcl:"password" env:"RESTIC_PASSWORD"`
|
Password string `hcl:"password" env:"RESTIC_PASSWORD"`
|
||||||
PasswordFile string `hcl:"password_file" flag:"password-file" env:"RESTIC_PASSWORD_FILE"`
|
PasswordFile string `hcl:"password_file" flag:"password-file" env:"RESTIC_PASSWORD_FILE"`
|
||||||
|
|
||||||
Backends map[string]Backend `hcl:"backend"`
|
Backends map[string]interface{} `hcl:"backend"`
|
||||||
Backup Backup `hcl:"backup"`
|
Backup Backup `hcl:"backup"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backend is a configured backend to store a repository.
|
// BackendLocal is a backend in a local directory.
|
||||||
type Backend struct {
|
type BackendLocal struct {
|
||||||
Backend string
|
Type string `hcl:"type"`
|
||||||
Path string
|
Path string `hcl:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Backup sets the options for the "backup" command.
|
// Backup sets the options for the "backup" command.
|
||||||
|
@ -80,8 +80,8 @@ func Parse(buf []byte) (cfg Config, err error) {
|
||||||
root := parsed.Node.(*ast.ObjectList)
|
root := parsed.Node.(*ast.ObjectList)
|
||||||
|
|
||||||
checks := map[string]map[string]struct{}{
|
checks := map[string]map[string]struct{}{
|
||||||
"": listTags(cfg, "config"),
|
"": listTags(cfg, "hcl"),
|
||||||
"backup": listTags(Backup{}, "config"),
|
"backup": listTags(Backup{}, "hcl"),
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, valid := range checks {
|
for name, valid := range checks {
|
||||||
|
@ -150,6 +150,8 @@ func ApplyFlags(cfg interface{}, fset *pflag.FlagSet) error {
|
||||||
panic("target config is not a pointer")
|
panic("target config is not a pointer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug.Log("apply flags")
|
||||||
|
|
||||||
attr := getFieldsForTag("flag", cfg)
|
attr := getFieldsForTag("flag", cfg)
|
||||||
|
|
||||||
var visitError error
|
var visitError error
|
||||||
|
@ -197,7 +199,13 @@ func ApplyFlags(cfg interface{}, fset *pflag.FlagSet) error {
|
||||||
visitError = err
|
visitError = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
field.SetSlice(v)
|
|
||||||
|
slice := reflect.MakeSlice(reflect.TypeOf(v), len(v), len(v))
|
||||||
|
field.Set(slice)
|
||||||
|
|
||||||
|
for i, s := range v {
|
||||||
|
slice.Index(i).SetString(s)
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
visitError = errors.Errorf("flag %v has unknown type %v", flag.Name, flag.Value.Type())
|
visitError = errors.Errorf("flag %v has unknown type %v", flag.Name, flag.Value.Type())
|
||||||
return
|
return
|
||||||
|
|
|
@ -9,18 +9,11 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var updateGoldenFiles = flag.Bool("update", false, "update golden files in testdata/")
|
var updateGoldenFiles = flag.Bool("update", false, "update golden files in testdata/")
|
||||||
|
|
||||||
func readTestFile(t testing.TB, filename string) []byte {
|
|
||||||
data, err := ioutil.ReadFile(filepath.Join("testdata", filename))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
func saveGoldenFile(t testing.TB, base string, cfg Config) {
|
func saveGoldenFile(t testing.TB, base string, cfg Config) {
|
||||||
buf, err := json.MarshalIndent(cfg, "", " ")
|
buf, err := json.MarshalIndent(cfg, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -48,7 +41,7 @@ func loadGoldenFile(t testing.TB, base string) Config {
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRead(t *testing.T) {
|
func TestConfigLoad(t *testing.T) {
|
||||||
entries, err := ioutil.ReadDir("testdata")
|
entries, err := ioutil.ReadDir("testdata")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -62,9 +55,7 @@ func TestRead(t *testing.T) {
|
||||||
|
|
||||||
base := strings.TrimSuffix(filename, ".conf")
|
base := strings.TrimSuffix(filename, ".conf")
|
||||||
t.Run(base, func(t *testing.T) {
|
t.Run(base, func(t *testing.T) {
|
||||||
buf := readTestFile(t, filename)
|
cfg, err := Load(filepath.Join("testdata", filename))
|
||||||
|
|
||||||
cfg, err := Parse(buf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -81,3 +72,117 @@ func TestRead(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigApplyFlags(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
filename string
|
||||||
|
applyFlags func(cfg *Config) error
|
||||||
|
want Config
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
filename: "backup.conf",
|
||||||
|
applyFlags: func(cfg *Config) error {
|
||||||
|
args := []string{"--exclude", "foo/*.go"}
|
||||||
|
|
||||||
|
s := pflag.NewFlagSet("", pflag.ContinueOnError)
|
||||||
|
s.StringArrayP("exclude", "e", nil, "exclude files")
|
||||||
|
|
||||||
|
err := s.Parse(args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ApplyFlags(&cfg.Backup, s)
|
||||||
|
},
|
||||||
|
want: Config{
|
||||||
|
Backup: Backup{
|
||||||
|
Target: []string{"foo", "/home/user"},
|
||||||
|
Excludes: []string{"foo/*.go"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
filename: "backup.conf",
|
||||||
|
applyFlags: func(cfg *Config) error {
|
||||||
|
args := []string{"--repo", "sftp:user@server:/srv/backup/repo"}
|
||||||
|
|
||||||
|
s := pflag.NewFlagSet("", pflag.ContinueOnError)
|
||||||
|
s.StringP("repo", "r", "", "repository to backup to or restore from")
|
||||||
|
|
||||||
|
err := s.Parse(args)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ApplyFlags(cfg, s)
|
||||||
|
},
|
||||||
|
want: Config{
|
||||||
|
Backup: Backup{
|
||||||
|
Target: []string{"foo", "/home/user"},
|
||||||
|
},
|
||||||
|
Repo: "sftp:user@server:/srv/backup/repo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
cfg, err := Load(filepath.Join("testdata", test.filename))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = test.applyFlags(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cmp.Equal(test.want, cfg) {
|
||||||
|
t.Error(cmp.Diff(test.want, cfg))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConfigApplyEnv(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
filename string
|
||||||
|
env []string
|
||||||
|
want Config
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
filename: "backup.conf",
|
||||||
|
env: []string{
|
||||||
|
"RESTIC_REPOSITORY=/tmp/repo",
|
||||||
|
"RESTIC_PASSWORD=foobar",
|
||||||
|
"RESTIC_PASSWORD_FILE=/root/secret.txt",
|
||||||
|
},
|
||||||
|
want: Config{
|
||||||
|
Password: "foobar",
|
||||||
|
PasswordFile: "/root/secret.txt",
|
||||||
|
Repo: "/tmp/repo",
|
||||||
|
Backup: Backup{
|
||||||
|
Target: []string{"foo", "/home/user"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
cfg, err := Load(filepath.Join("testdata", test.filename))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ApplyEnv(&cfg, test.env)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !cmp.Equal(test.want, cfg) {
|
||||||
|
t.Error(cmp.Diff(test.want, cfg))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
12
internal/ui/config/testdata/all.conf
vendored
Normal file
12
internal/ui/config/testdata/all.conf
vendored
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
repo = "sftp:user@server:/srv/repo"
|
||||||
|
password = "secret"
|
||||||
|
password_file = "/root/secret.txt"
|
||||||
|
|
||||||
|
backup {
|
||||||
|
target = [
|
||||||
|
"/home/user/",
|
||||||
|
"/home/otheruser",
|
||||||
|
]
|
||||||
|
|
||||||
|
exclude = ["*.c"]
|
||||||
|
}
|
15
internal/ui/config/testdata/all.golden
vendored
Normal file
15
internal/ui/config/testdata/all.golden
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"Repo": "sftp:user@server:/srv/repo",
|
||||||
|
"Password": "secret",
|
||||||
|
"PasswordFile": "/root/secret.txt",
|
||||||
|
"Backends": null,
|
||||||
|
"Backup": {
|
||||||
|
"Target": [
|
||||||
|
"/home/user/",
|
||||||
|
"/home/otheruser"
|
||||||
|
],
|
||||||
|
"Excludes": [
|
||||||
|
"*.c"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
7
internal/ui/config/testdata/backup.golden
vendored
7
internal/ui/config/testdata/backup.golden
vendored
|
@ -1,10 +1,13 @@
|
||||||
{
|
{
|
||||||
"Backends": null,
|
|
||||||
"Repo": "",
|
"Repo": "",
|
||||||
|
"Password": "",
|
||||||
|
"PasswordFile": "",
|
||||||
|
"Backends": null,
|
||||||
"Backup": {
|
"Backup": {
|
||||||
"Target": [
|
"Target": [
|
||||||
"foo",
|
"foo",
|
||||||
"/home/user"
|
"/home/user"
|
||||||
]
|
],
|
||||||
|
"Excludes": null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
{
|
{
|
||||||
|
"Repo": "test",
|
||||||
|
"Password": "",
|
||||||
|
"PasswordFile": "",
|
||||||
"Backends": {
|
"Backends": {
|
||||||
"test": {
|
"test": {
|
||||||
"Backend": "local",
|
"Backend": "local",
|
||||||
"Path": "/foo/bar/baz"
|
"Path": "/foo/bar/baz"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Repo": "test",
|
"Backup": {
|
||||||
"Backup": null
|
"Target": null,
|
||||||
|
"Excludes": null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue