Add repository location parsing code
This commit is contained in:
parent
43cf95e3c6
commit
566a15285a
7 changed files with 370 additions and 0 deletions
51
backend/s3/uri.go
Normal file
51
backend/s3/uri.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package s3
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Config contains all configuration necessary to connect to an s3 compatible
|
||||
// server.
|
||||
type Config struct {
|
||||
Host string
|
||||
KeyID, Secret string
|
||||
Bucket string
|
||||
}
|
||||
|
||||
// ParseConfig parses the string s and extracts the s3 config. The two
|
||||
// supported configuration formats are s3://host/bucketname and
|
||||
// s3:host:bucketname. The host can also be a valid s3 region name.
|
||||
func ParseConfig(s string) (interface{}, error) {
|
||||
if strings.HasPrefix(s, "s3://") {
|
||||
s = s[5:]
|
||||
|
||||
data := strings.SplitN(s, "/", 2)
|
||||
if len(data) != 2 {
|
||||
return nil, errors.New("s3: invalid format, host/region or bucket name not found")
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
Host: data[0],
|
||||
Bucket: data[1],
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
data := strings.SplitN(s, ":", 3)
|
||||
if len(data) != 3 {
|
||||
return nil, errors.New("s3: invalid format")
|
||||
}
|
||||
|
||||
if data[0] != "s3" {
|
||||
return nil, errors.New(`s3: config does not start with "s3"`)
|
||||
}
|
||||
|
||||
cfg := Config{
|
||||
Host: data[1],
|
||||
Bucket: data[2],
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
33
backend/s3/uri_test.go
Normal file
33
backend/s3/uri_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
package s3
|
||||
|
||||
import "testing"
|
||||
|
||||
var uriTests = []struct {
|
||||
s string
|
||||
cfg Config
|
||||
}{
|
||||
{"s3://eu-central-1/bucketname", Config{
|
||||
Host: "eu-central-1",
|
||||
Bucket: "bucketname",
|
||||
}},
|
||||
{"s3:hostname:foobar", Config{
|
||||
Host: "hostname",
|
||||
Bucket: "foobar",
|
||||
}},
|
||||
}
|
||||
|
||||
func TestParseConfig(t *testing.T) {
|
||||
for i, test := range uriTests {
|
||||
cfg, err := ParseConfig(test.s)
|
||||
if err != nil {
|
||||
t.Errorf("test %d failed: %v", i, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if cfg != test.cfg {
|
||||
t.Errorf("test %d: wrong config, want:\n %v\ngot:\n %v",
|
||||
i, test.cfg, cfg)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue