2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

gs: Rework path initialization

This commit is contained in:
Alexander Neumann 2017-08-05 17:15:59 +02:00
parent 8ca6a9a240
commit d9a5b9178e
2 changed files with 18 additions and 15 deletions

View File

@ -1,10 +1,10 @@
package gs package gs
import ( import (
"errors"
"path" "path"
"strings" "strings"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/options" "github.com/restic/restic/internal/options"
) )
@ -33,25 +33,28 @@ func init() {
// ParseConfig parses the string s and extracts the gcs config. The // ParseConfig parses the string s and extracts the gcs config. The
// supported configuration format is gs:bucketName:/[prefix]. // supported configuration format is gs:bucketName:/[prefix].
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (interface{}, error) {
if strings.HasPrefix(s, "gs:") { if !strings.HasPrefix(s, "gs:") {
s = s[3:]
} else {
return nil, errors.New("gs: invalid format") 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 // use the first entry of the path as the bucket name and the
// remainder as prefix // remainder as prefix
path := strings.SplitN(s, ":/", 2) data := strings.SplitN(s, ":", 2)
return createConfig(path) if len(data) < 2 {
} return nil, errors.New("gs: invalid format: bucket name or path not found")
}
func createConfig(p []string) (interface{}, error) { bucket, path := data[0], path.Clean(data[1])
if len(p) < 2 {
return nil, errors.New("gs: invalid format, bucket name not found") if strings.HasPrefix(path, "/") {
path = path[1:]
} }
cfg := NewConfig() cfg := NewConfig()
cfg.Bucket = p[0] cfg.Bucket = bucket
if p[1] != "" { cfg.Prefix = path
cfg.Prefix = path.Clean(p[1])
}
return cfg, nil return cfg, nil
} }

View File

@ -2,7 +2,6 @@ package gs_test
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"os" "os"
"testing" "testing"
@ -10,6 +9,7 @@ import (
"github.com/restic/restic/internal/backend/gs" "github.com/restic/restic/internal/backend/gs"
"github.com/restic/restic/internal/backend/test" "github.com/restic/restic/internal/backend/test"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
. "github.com/restic/restic/internal/test" . "github.com/restic/restic/internal/test"
) )