mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 23:06:32 +00:00
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:
parent
24b7514fe0
commit
f3f1404849
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user