always use "restic" as the default prefix for parsing.

improved test error message to make it easier to find the problematic pattern
This commit is contained in:
Christian Kemper 2016-02-14 11:26:46 -08:00
parent 24b7514fe0
commit f3f1404849
2 changed files with 13 additions and 12 deletions

View file

@ -3,6 +3,7 @@ package s3
import (
"errors"
"net/url"
"path"
"strings"
)
@ -52,20 +53,20 @@ func ParseConfig(s string) (interface{}, error) {
return createConfig(path[0], path[1:], false)
}
func createConfig(endpoint string, path []string, useHTTP bool) (interface{}, error) {
func createConfig(endpoint string, p []string, useHTTP bool) (interface{}, error) {
var prefix string
switch {
case len(path) < 1:
case len(p) < 1:
return nil, errors.New("s3: invalid format, host/region or bucket name not found")
case len(path) == 1:
case len(p) == 1 || p[1] == "":
prefix = defaultPrefix
default:
prefix = strings.TrimRight(path[1], "/")
prefix = path.Clean(p[1])
}
return Config{
Endpoint: endpoint,
UseHTTP: useHTTP,
Bucket: path[0],
Bucket: p[0],
Prefix: prefix,
}, nil
}

View file

@ -14,7 +14,7 @@ var configTests = []struct {
{"s3://eu-central-1/bucketname/", Config{
Endpoint: "eu-central-1",
Bucket: "bucketname",
Prefix: "",
Prefix: "restic",
}},
{"s3://eu-central-1/bucketname/prefix/directory", Config{
Endpoint: "eu-central-1",
@ -34,7 +34,7 @@ var configTests = []struct {
{"s3:eu-central-1/foobar/", Config{
Endpoint: "eu-central-1",
Bucket: "foobar",
Prefix: "",
Prefix: "restic",
}},
{"s3:eu-central-1/foobar/prefix/directory", Config{
Endpoint: "eu-central-1",
@ -54,7 +54,7 @@ var configTests = []struct {
{"s3:https://hostname:9999/foobar/", Config{
Endpoint: "hostname:9999",
Bucket: "foobar",
Prefix: "",
Prefix: "restic",
}},
{"s3:http://hostname:9999/foobar", Config{
Endpoint: "hostname:9999",
@ -65,7 +65,7 @@ var configTests = []struct {
{"s3:http://hostname:9999/foobar/", Config{
Endpoint: "hostname:9999",
Bucket: "foobar",
Prefix: "",
Prefix: "restic",
UseHTTP: true,
}},
{"s3:http://hostname:9999/bucket/prefix/directory", Config{
@ -86,13 +86,13 @@ func TestParseConfig(t *testing.T) {
for i, test := range configTests {
cfg, err := ParseConfig(test.s)
if err != nil {
t.Errorf("test %d failed: %v", i, err)
t.Errorf("test %d:%s failed: %v", i, test.s, err)
continue
}
if cfg != test.cfg {
t.Errorf("test %d: wrong config, want:\n %v\ngot:\n %v",
i, test.cfg, cfg)
t.Errorf("test %d:\ninput:\n %s\n wrong config, want:\n %v\ngot:\n %v",
i, test.s, test.cfg, cfg)
continue
}
}