restic/src/restic/backend/location/location.go

69 lines
1.6 KiB
Go
Raw Normal View History

2015-12-28 15:57:20 +01:00
// Package location implements parsing the restic repository location from a string.
package location
2015-12-28 15:51:24 +01:00
import (
"strings"
"restic/backend/local"
2016-02-21 15:24:37 +01:00
"restic/backend/rest"
"restic/backend/s3"
"restic/backend/sftp"
2015-12-28 15:51:24 +01:00
)
2015-12-28 15:57:20 +01:00
// Location specifies the location of a repository, including the method of
// access and (possibly) credentials needed for access.
type Location struct {
2015-12-28 15:51:24 +01:00
Scheme string
Config interface{}
}
type parser struct {
scheme string
parse func(string) (interface{}, error)
}
// parsers is a list of valid config parsers for the backends. The first parser
// is the fallback and should always be set to the local backend.
var parsers = []parser{
{"local", local.ParseConfig},
{"sftp", sftp.ParseConfig},
{"s3", s3.ParseConfig},
2016-02-21 15:24:37 +01:00
{"rest", rest.ParseConfig},
2015-12-28 15:51:24 +01:00
}
2015-12-28 16:42:44 +01:00
// Parse extracts repository location information from the string s. If s
// starts with a backend name followed by a colon, that backend's Parse()
// function is called. Otherwise, the local backend is used which interprets s
// as the name of a directory.
func Parse(s string) (u Location, err error) {
2015-12-28 15:51:24 +01:00
scheme := extractScheme(s)
u.Scheme = scheme
for _, parser := range parsers {
if parser.scheme != scheme {
continue
}
u.Config, err = parser.parse(s)
if err != nil {
2015-12-28 15:57:20 +01:00
return Location{}, err
2015-12-28 15:51:24 +01:00
}
return u, nil
}
// try again, with the local parser and the prefix "local:"
u.Scheme = "local"
u.Config, err = local.ParseConfig("local:" + s)
if err != nil {
2015-12-28 15:57:20 +01:00
return Location{}, err
2015-12-28 15:51:24 +01:00
}
return u, nil
}
func extractScheme(s string) string {
data := strings.SplitN(s, ":", 2)
return data[0]
}