diff --git a/internal/ui/config/config.go b/internal/ui/config/config.go index 042d14452..7473e432e 100644 --- a/internal/ui/config/config.go +++ b/internal/ui/config/config.go @@ -20,26 +20,16 @@ type Config struct { Password string `hcl:"password" env:"RESTIC_PASSWORD"` PasswordFile string `hcl:"password_file" flag:"password-file" env:"RESTIC_PASSWORD_FILE"` - Backends map[string]interface{} + Backends map[string]Backend Backup Backup `hcl:"backup"` } // Backend configures a backend. type Backend struct { Type string `hcl:"type"` -} - -// BackendLocal is a backend in a local directory. -type BackendLocal struct { - Type string `hcl:"type"` - Path string `hcl:"path"` -} - -// BackendSFTP is a backend stored on a server via sftp. -type BackendSFTP struct { - Type string `hcl:"type"` User string `hcl:"user"` Host string `hcl:"host"` + Path string `hcl:"path"` } // Backup sets the options for the "backup" command. @@ -132,8 +122,8 @@ func Parse(buf []byte) (cfg Config, err error) { } // parseBackends parses the backend configuration sections. -func parseBackends(root *ast.ObjectList) (map[string]interface{}, error) { - backends := make(map[string]interface{}) +func parseBackends(root *ast.ObjectList) (map[string]Backend, error) { + backends := make(map[string]Backend) // find top-level backend objects for _, item := range root.Items { @@ -172,21 +162,8 @@ func parseBackends(root *ast.ObjectList) (map[string]interface{}, error) { return nil, err } - // then decode it into the right type - var target interface{} - switch be.Type { - case "local", "": - target = &BackendLocal{Type: "local"} - case "sftp": - target = &BackendSFTP{} - default: - return nil, errors.Errorf("backend type %q is unknown at line %v, column %v", - be.Type, item.Pos().Line, item.Pos().Column) - } - - err = hcl.DecodeObject(target, item) - if err != nil { - return nil, err + if be.Type == "" { + be.Type = "local" } if _, ok := backends[name]; ok { @@ -202,12 +179,12 @@ func parseBackends(root *ast.ObjectList) (map[string]interface{}, error) { } // check allowed types - err = validateObjects(innerBlock.List, listTags(target, "hcl")) + err = validateObjects(innerBlock.List, listTags(be, "hcl")) if err != nil { return nil, err } - backends[name] = target + backends[name] = be } return backends, nil diff --git a/internal/ui/config/config_test.go b/internal/ui/config/config_test.go index 09e517930..fe8734bdf 100644 --- a/internal/ui/config/config_test.go +++ b/internal/ui/config/config_test.go @@ -99,6 +99,7 @@ func TestConfigApplyFlags(t *testing.T) { Target: []string{"foo", "/home/user"}, Excludes: []string{"foo/*.go"}, }, + Backends: map[string]Backend{}, }, }, { @@ -120,7 +121,8 @@ func TestConfigApplyFlags(t *testing.T) { Backup: Backup{ Target: []string{"foo", "/home/user"}, }, - Repo: "sftp:user@server:/srv/backup/repo", + Repo: "sftp:user@server:/srv/backup/repo", + Backends: map[string]Backend{}, }, }, } @@ -164,6 +166,7 @@ func TestConfigApplyEnv(t *testing.T) { Backup: Backup{ Target: []string{"foo", "/home/user"}, }, + Backends: map[string]Backend{}, }, }, } diff --git a/internal/ui/config/testdata/backend-local.golden b/internal/ui/config/testdata/backend-local.golden index 383c03bef..441621397 100644 --- a/internal/ui/config/testdata/backend-local.golden +++ b/internal/ui/config/testdata/backend-local.golden @@ -5,10 +5,14 @@ "Backends": { "bar": { "Type": "local", + "User": "", + "Host": "", "Path": "/srv/data/repo" }, "foo": { "Type": "local", + "User": "", + "Host": "", "Path": "/srv/data/repo" } }, diff --git a/internal/ui/config/testdata/repo_local.conf b/internal/ui/config/testdata/repo_local.conf index 0fecdbad2..a146e7fc0 100644 --- a/internal/ui/config/testdata/repo_local.conf +++ b/internal/ui/config/testdata/repo_local.conf @@ -1,5 +1,5 @@ backend "test" { - backend = "local" + type = "local" path = "/foo/bar/baz" } diff --git a/internal/ui/config/testdata/repo_local.golden b/internal/ui/config/testdata/repo_local.golden index 9adfddec2..c43bbcc7d 100644 --- a/internal/ui/config/testdata/repo_local.golden +++ b/internal/ui/config/testdata/repo_local.golden @@ -4,7 +4,9 @@ "PasswordFile": "", "Backends": { "test": { - "Type": "", + "Type": "local", + "User": "", + "Host": "", "Path": "/foo/bar/baz" } },