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

View file

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