2015-12-28 14:57:20 +00:00
|
|
|
// Package location implements parsing the restic repository location from a string.
|
|
|
|
package location
|
2015-12-28 14:51:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2017-05-28 08:19:01 +00:00
|
|
|
"restic/backend/b2"
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/backend/local"
|
2016-02-21 14:24:37 +00:00
|
|
|
"restic/backend/rest"
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/backend/s3"
|
|
|
|
"restic/backend/sftp"
|
2017-03-29 21:58:25 +00:00
|
|
|
"restic/backend/swift"
|
2017-06-30 18:40:27 +00:00
|
|
|
"restic/errors"
|
2015-12-28 14:51:24 +00:00
|
|
|
)
|
|
|
|
|
2015-12-28 14:57:20 +00: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 14:51:24 +00: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{
|
2017-05-28 08:19:01 +00:00
|
|
|
{"b2", b2.ParseConfig},
|
2015-12-28 14:51:24 +00:00
|
|
|
{"local", local.ParseConfig},
|
|
|
|
{"sftp", sftp.ParseConfig},
|
|
|
|
{"s3", s3.ParseConfig},
|
2017-03-29 21:58:25 +00:00
|
|
|
{"swift", swift.ParseConfig},
|
2016-02-21 14:24:37 +00:00
|
|
|
{"rest", rest.ParseConfig},
|
2015-12-28 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 18:40:27 +00:00
|
|
|
func isPath(s string) bool {
|
|
|
|
if strings.HasPrefix(s, "../") || strings.HasPrefix(s, `..\`) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(s, "/") || strings.HasPrefix(s, `\`) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s) < 3 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for drive paths
|
|
|
|
drive := s[0]
|
|
|
|
if !(drive >= 'a' && drive <= 'z') && !(drive >= 'A' && drive <= 'Z') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s[1] != ':' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s[2] != '\\' && s[2] != '/' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-12-28 15:42:44 +00: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 14:51:24 +00: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 14:57:20 +00:00
|
|
|
return Location{}, err
|
2015-12-28 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:40:27 +00:00
|
|
|
// if s is not a path or contains ":", it's ambiguous
|
|
|
|
if !isPath(s) && strings.ContainsRune(s, ':') {
|
|
|
|
return Location{}, errors.New("invalid backend\nIf the repo is in a local directory, you need to add a `local:` prefix")
|
|
|
|
}
|
|
|
|
|
2015-12-28 14:51:24 +00:00
|
|
|
u.Scheme = "local"
|
|
|
|
u.Config, err = local.ParseConfig("local:" + s)
|
|
|
|
if err != nil {
|
2015-12-28 14:57:20 +00:00
|
|
|
return Location{}, err
|
2015-12-28 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractScheme(s string) string {
|
|
|
|
data := strings.SplitN(s, ":", 2)
|
|
|
|
return data[0]
|
|
|
|
}
|