From d9a5b9178e4a5b303eaf033914f2d59c5e173a63 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 5 Aug 2017 17:15:59 +0200 Subject: [PATCH] gs: Rework path initialization --- internal/backend/gs/config.go | 31 +++++++++++++++++-------------- internal/backend/gs/gs_test.go | 2 +- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/internal/backend/gs/config.go b/internal/backend/gs/config.go index 29ebc1c0c..cc1b7d805 100644 --- a/internal/backend/gs/config.go +++ b/internal/backend/gs/config.go @@ -1,10 +1,10 @@ package gs import ( - "errors" "path" "strings" + "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/options" ) @@ -33,25 +33,28 @@ func init() { // ParseConfig parses the string s and extracts the gcs config. The // supported configuration format is gs:bucketName:/[prefix]. func ParseConfig(s string) (interface{}, error) { - if strings.HasPrefix(s, "gs:") { - s = s[3:] - } else { + if !strings.HasPrefix(s, "gs:") { return nil, errors.New("gs: invalid format") } + + // strip prefix "gs:" + s = s[3:] + // use the first entry of the path as the bucket name and the // remainder as prefix - path := strings.SplitN(s, ":/", 2) - return createConfig(path) -} + data := strings.SplitN(s, ":", 2) + if len(data) < 2 { + return nil, errors.New("gs: invalid format: bucket name or path not found") + } -func createConfig(p []string) (interface{}, error) { - if len(p) < 2 { - return nil, errors.New("gs: invalid format, bucket name not found") + bucket, path := data[0], path.Clean(data[1]) + + if strings.HasPrefix(path, "/") { + path = path[1:] } + cfg := NewConfig() - cfg.Bucket = p[0] - if p[1] != "" { - cfg.Prefix = path.Clean(p[1]) - } + cfg.Bucket = bucket + cfg.Prefix = path return cfg, nil } diff --git a/internal/backend/gs/gs_test.go b/internal/backend/gs/gs_test.go index b93e698e9..f7b412e5d 100644 --- a/internal/backend/gs/gs_test.go +++ b/internal/backend/gs/gs_test.go @@ -2,7 +2,6 @@ package gs_test import ( "context" - "errors" "fmt" "os" "testing" @@ -10,6 +9,7 @@ import ( "github.com/restic/restic/internal/backend/gs" "github.com/restic/restic/internal/backend/test" + "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/restic" . "github.com/restic/restic/internal/test" )