From f3f140484993c0cf78c2e516722982caf3a2551a Mon Sep 17 00:00:00 2001 From: Christian Kemper Date: Sun, 14 Feb 2016 11:26:46 -0800 Subject: [PATCH] always use "restic" as the default prefix for parsing. improved test error message to make it easier to find the problematic pattern --- backend/s3/config.go | 11 ++++++----- backend/s3/config_test.go | 14 +++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/backend/s3/config.go b/backend/s3/config.go index e2bccd801..5cd74ce79 100644 --- a/backend/s3/config.go +++ b/backend/s3/config.go @@ -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 } diff --git a/backend/s3/config_test.go b/backend/s3/config_test.go index 63bc2c77e..3a04d59a2 100644 --- a/backend/s3/config_test.go +++ b/backend/s3/config_test.go @@ -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 } }