This commit is contained in:
Alexander Neumann 2018-05-13 22:42:42 +02:00
parent 0758c92afc
commit ea565df3e8
5 changed files with 20 additions and 34 deletions

View File

@ -20,26 +20,16 @@ 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]interface{} Backends map[string]Backend
Backup Backup `hcl:"backup"` Backup Backup `hcl:"backup"`
} }
// Backend configures a backend. // Backend configures a backend.
type Backend struct { type Backend struct {
Type string `hcl:"type"` 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"` User string `hcl:"user"`
Host string `hcl:"host"` Host string `hcl:"host"`
Path string `hcl:"path"`
} }
// Backup sets the options for the "backup" command. // 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. // parseBackends parses the backend configuration sections.
func parseBackends(root *ast.ObjectList) (map[string]interface{}, error) { func parseBackends(root *ast.ObjectList) (map[string]Backend, error) {
backends := make(map[string]interface{}) backends := make(map[string]Backend)
// find top-level backend objects // find top-level backend objects
for _, item := range root.Items { for _, item := range root.Items {
@ -172,21 +162,8 @@ func parseBackends(root *ast.ObjectList) (map[string]interface{}, error) {
return nil, err return nil, err
} }
// then decode it into the right type if be.Type == "" {
var target interface{} be.Type = "local"
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 _, ok := backends[name]; ok { if _, ok := backends[name]; ok {
@ -202,12 +179,12 @@ func parseBackends(root *ast.ObjectList) (map[string]interface{}, error) {
} }
// check allowed types // check allowed types
err = validateObjects(innerBlock.List, listTags(target, "hcl")) err = validateObjects(innerBlock.List, listTags(be, "hcl"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
backends[name] = target backends[name] = be
} }
return backends, nil return backends, nil

View File

@ -99,6 +99,7 @@ func TestConfigApplyFlags(t *testing.T) {
Target: []string{"foo", "/home/user"}, Target: []string{"foo", "/home/user"},
Excludes: []string{"foo/*.go"}, Excludes: []string{"foo/*.go"},
}, },
Backends: map[string]Backend{},
}, },
}, },
{ {
@ -120,7 +121,8 @@ func TestConfigApplyFlags(t *testing.T) {
Backup: Backup{ Backup: Backup{
Target: []string{"foo", "/home/user"}, 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{ Backup: Backup{
Target: []string{"foo", "/home/user"}, Target: []string{"foo", "/home/user"},
}, },
Backends: map[string]Backend{},
}, },
}, },
} }

View File

@ -5,10 +5,14 @@
"Backends": { "Backends": {
"bar": { "bar": {
"Type": "local", "Type": "local",
"User": "",
"Host": "",
"Path": "/srv/data/repo" "Path": "/srv/data/repo"
}, },
"foo": { "foo": {
"Type": "local", "Type": "local",
"User": "",
"Host": "",
"Path": "/srv/data/repo" "Path": "/srv/data/repo"
} }
}, },

View File

@ -1,5 +1,5 @@
backend "test" { backend "test" {
backend = "local" type = "local"
path = "/foo/bar/baz" path = "/foo/bar/baz"
} }

View File

@ -4,7 +4,9 @@
"PasswordFile": "", "PasswordFile": "",
"Backends": { "Backends": {
"test": { "test": {
"Type": "", "Type": "local",
"User": "",
"Host": "",
"Path": "/foo/bar/baz" "Path": "/foo/bar/baz"
} }
}, },