2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 21:05:10 +00:00

backend: let ParseConfig return concrete type

This commit is contained in:
Michael Eischer 2023-04-20 22:40:21 +02:00
parent 2f7b4ceae1
commit 5260d38980
17 changed files with 55 additions and 62 deletions

View File

@ -30,12 +30,11 @@ func newAzureTestSuite(t testing.TB) *test.Suite {
// NewConfig returns a config for a new temporary backend that will be used in tests. // NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) { NewConfig: func() (interface{}, error) {
azcfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY")) cfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg := azcfg.(azure.Config)
cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME") cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME")
cfg.AccountKey = options.NewSecretString(os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_KEY")) cfg.AccountKey = options.NewSecretString(os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_KEY"))
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano()) cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
@ -141,12 +140,11 @@ func TestUploadLargeFile(t *testing.T) {
return return
} }
azcfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY")) cfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
cfg := azcfg.(azure.Config)
cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME") cfg.AccountName = os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_NAME")
cfg.AccountKey = options.NewSecretString(os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_KEY")) cfg.AccountKey = options.NewSecretString(os.Getenv("RESTIC_TEST_AZURE_ACCOUNT_KEY"))
cfg.Prefix = fmt.Sprintf("test-upload-large-%d", time.Now().UnixNano()) cfg.Prefix = fmt.Sprintf("test-upload-large-%d", time.Now().UnixNano())

View File

@ -34,9 +34,9 @@ func init() {
// ParseConfig parses the string s and extracts the azure config. The // ParseConfig parses the string s and extracts the azure config. The
// configuration format is azure:containerName:/[prefix]. // configuration format is azure:containerName:/[prefix].
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
if !strings.HasPrefix(s, "azure:") { if !strings.HasPrefix(s, "azure:") {
return nil, errors.New("azure: invalid format") return Config{}, errors.New("azure: invalid format")
} }
// strip prefix "azure:" // strip prefix "azure:"
@ -46,7 +46,7 @@ func ParseConfig(s string) (interface{}, error) {
// remainder as prefix // remainder as prefix
container, prefix, colon := strings.Cut(s, ":") container, prefix, colon := strings.Cut(s, ":")
if !colon { if !colon {
return nil, errors.New("azure: invalid format: bucket name or path not found") return Config{}, errors.New("azure: invalid format: bucket name or path not found")
} }
prefix = strings.TrimPrefix(path.Clean(prefix), "/") prefix = strings.TrimPrefix(path.Clean(prefix), "/")
cfg := NewConfig() cfg := NewConfig()

View File

@ -31,12 +31,11 @@ func newB2TestSuite(t testing.TB) *test.Suite {
// NewConfig returns a config for a new temporary backend that will be used in tests. // NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) { NewConfig: func() (interface{}, error) {
b2cfg, err := b2.ParseConfig(os.Getenv("RESTIC_TEST_B2_REPOSITORY")) cfg, err := b2.ParseConfig(os.Getenv("RESTIC_TEST_B2_REPOSITORY"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg := b2cfg.(b2.Config)
cfg.AccountID = os.Getenv("RESTIC_TEST_B2_ACCOUNT_ID") cfg.AccountID = os.Getenv("RESTIC_TEST_B2_ACCOUNT_ID")
cfg.Key = options.NewSecretString(os.Getenv("RESTIC_TEST_B2_ACCOUNT_KEY")) cfg.Key = options.NewSecretString(os.Getenv("RESTIC_TEST_B2_ACCOUNT_KEY"))
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano()) cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())

View File

@ -59,15 +59,15 @@ func checkBucketName(name string) error {
// ParseConfig parses the string s and extracts the b2 config. The supported // ParseConfig parses the string s and extracts the b2 config. The supported
// configuration format is b2:bucketname/prefix. If no prefix is given the // configuration format is b2:bucketname/prefix. If no prefix is given the
// prefix "restic" will be used. // prefix "restic" will be used.
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
if !strings.HasPrefix(s, "b2:") { if !strings.HasPrefix(s, "b2:") {
return nil, errors.New("invalid format, want: b2:bucket-name[:path]") return Config{}, errors.New("invalid format, want: b2:bucket-name[:path]")
} }
s = s[3:] s = s[3:]
bucket, prefix, _ := strings.Cut(s, ":") bucket, prefix, _ := strings.Cut(s, ":")
if err := checkBucketName(bucket); err != nil { if err := checkBucketName(bucket); err != nil {
return nil, err return Config{}, err
} }
if len(prefix) > 0 { if len(prefix) > 0 {

View File

@ -35,9 +35,9 @@ 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) (Config, error) {
if !strings.HasPrefix(s, "gs:") { if !strings.HasPrefix(s, "gs:") {
return nil, errors.New("gs: invalid format") return Config{}, errors.New("gs: invalid format")
} }
// strip prefix "gs:" // strip prefix "gs:"
@ -47,7 +47,7 @@ func ParseConfig(s string) (interface{}, error) {
// remainder as prefix // remainder as prefix
bucket, prefix, colon := strings.Cut(s, ":") bucket, prefix, colon := strings.Cut(s, ":")
if !colon { if !colon {
return nil, errors.New("gs: invalid format: bucket name or path not found") return Config{}, errors.New("gs: invalid format: bucket name or path not found")
} }
prefix = strings.TrimPrefix(path.Clean(prefix), "/") prefix = strings.TrimPrefix(path.Clean(prefix), "/")

View File

@ -27,12 +27,11 @@ func newGSTestSuite(t testing.TB) *test.Suite {
// NewConfig returns a config for a new temporary backend that will be used in tests. // NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) { NewConfig: func() (interface{}, error) {
gscfg, err := gs.ParseConfig(os.Getenv("RESTIC_TEST_GS_REPOSITORY")) cfg, err := gs.ParseConfig(os.Getenv("RESTIC_TEST_GS_REPOSITORY"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg := gscfg.(gs.Config)
cfg.ProjectID = os.Getenv("RESTIC_TEST_GS_PROJECT_ID") cfg.ProjectID = os.Getenv("RESTIC_TEST_GS_PROJECT_ID")
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano()) cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
return cfg, nil return cfg, nil

View File

@ -27,9 +27,9 @@ func init() {
} }
// ParseConfig parses a local backend config. // ParseConfig parses a local backend config.
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
if !strings.HasPrefix(s, "local:") { if !strings.HasPrefix(s, "local:") {
return nil, errors.New(`invalid format, prefix "local" not found`) return Config{}, errors.New(`invalid format, prefix "local" not found`)
} }
cfg := NewConfig() cfg := NewConfig()

View File

@ -29,18 +29,24 @@ type parser struct {
stripPassword func(string) string stripPassword func(string) string
} }
func configToAny[C any](parser func(string) (C, error)) func(string) (interface{}, error) {
return func(s string) (interface{}, error) {
return parser(s)
}
}
// parsers is a list of valid config parsers for the backends. The first parser // parsers is a list of valid config parsers for the backends. The first parser
// is the fallback and should always be set to the local backend. // is the fallback and should always be set to the local backend.
var parsers = []parser{ var parsers = []parser{
{"b2", b2.ParseConfig, noPassword}, {"b2", configToAny(b2.ParseConfig), noPassword},
{"local", local.ParseConfig, noPassword}, {"local", configToAny(local.ParseConfig), noPassword},
{"sftp", sftp.ParseConfig, noPassword}, {"sftp", configToAny(sftp.ParseConfig), noPassword},
{"s3", s3.ParseConfig, noPassword}, {"s3", configToAny(s3.ParseConfig), noPassword},
{"gs", gs.ParseConfig, noPassword}, {"gs", configToAny(gs.ParseConfig), noPassword},
{"azure", azure.ParseConfig, noPassword}, {"azure", configToAny(azure.ParseConfig), noPassword},
{"swift", swift.ParseConfig, noPassword}, {"swift", configToAny(swift.ParseConfig), noPassword},
{"rest", rest.ParseConfig, rest.StripPassword}, {"rest", configToAny(rest.ParseConfig), rest.StripPassword},
{"rclone", rclone.ParseConfig, noPassword}, {"rclone", configToAny(rclone.ParseConfig), noPassword},
} }
// noPassword returns the repository location unchanged (there's no sensitive information there) // noPassword returns the repository location unchanged (there's no sensitive information there)

View File

@ -34,9 +34,9 @@ func NewConfig() Config {
} }
// ParseConfig parses the string s and extracts the remote server URL. // ParseConfig parses the string s and extracts the remote server URL.
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
if !strings.HasPrefix(s, "rclone:") { if !strings.HasPrefix(s, "rclone:") {
return nil, errors.New("invalid rclone backend specification") return Config{}, errors.New("invalid rclone backend specification")
} }
s = s[7:] s = s[7:]

View File

@ -26,16 +26,16 @@ func NewConfig() Config {
} }
// ParseConfig parses the string s and extracts the REST server URL. // ParseConfig parses the string s and extracts the REST server URL.
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
if !strings.HasPrefix(s, "rest:") { if !strings.HasPrefix(s, "rest:") {
return nil, errors.New("invalid REST backend specification") return Config{}, errors.New("invalid REST backend specification")
} }
s = prepareURL(s) s = prepareURL(s)
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return Config{}, errors.WithStack(err)
} }
cfg := NewConfig() cfg := NewConfig()

View File

@ -130,12 +130,10 @@ func TestBackendRESTExternalServer(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
c := cfg.(rest.Config)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
newTestSuite(ctx, t, c.URL, true).RunTests(t) newTestSuite(ctx, t, cfg.URL, true).RunTests(t)
} }
func BenchmarkBackendREST(t *testing.B) { func BenchmarkBackendREST(t *testing.B) {

View File

@ -45,7 +45,7 @@ func init() {
// supported configuration formats are s3://host/bucketname/prefix and // supported configuration formats are s3://host/bucketname/prefix and
// s3:host/bucketname/prefix. The host can also be a valid s3 region // s3:host/bucketname/prefix. The host can also be a valid s3 region
// name. If no prefix is given the prefix "restic" will be used. // name. If no prefix is given the prefix "restic" will be used.
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
switch { switch {
case strings.HasPrefix(s, "s3:http"): case strings.HasPrefix(s, "s3:http"):
// assume that a URL has been specified, parse it and // assume that a URL has been specified, parse it and
@ -53,11 +53,11 @@ func ParseConfig(s string) (interface{}, error) {
// bucket name and prefix // bucket name and prefix
url, err := url.Parse(s[3:]) url, err := url.Parse(s[3:])
if err != nil { if err != nil {
return nil, errors.WithStack(err) return Config{}, errors.WithStack(err)
} }
if url.Path == "" { if url.Path == "" {
return nil, errors.New("s3: bucket name not found") return Config{}, errors.New("s3: bucket name not found")
} }
bucket, path, _ := strings.Cut(url.Path[1:], "/") bucket, path, _ := strings.Cut(url.Path[1:], "/")
@ -67,7 +67,7 @@ func ParseConfig(s string) (interface{}, error) {
case strings.HasPrefix(s, "s3:"): case strings.HasPrefix(s, "s3:"):
s = s[3:] s = s[3:]
default: default:
return nil, errors.New("s3: invalid format") return Config{}, errors.New("s3: invalid format")
} }
// use the first entry of the path as the endpoint and the // use the first entry of the path as the endpoint and the
// remainder as bucket name and prefix // remainder as bucket name and prefix
@ -76,9 +76,9 @@ func ParseConfig(s string) (interface{}, error) {
return createConfig(endpoint, bucket, prefix, false) return createConfig(endpoint, bucket, prefix, false)
} }
func createConfig(endpoint, bucket, prefix string, useHTTP bool) (interface{}, error) { func createConfig(endpoint, bucket, prefix string, useHTTP bool) (Config, error) {
if endpoint == "" { if endpoint == "" {
return nil, errors.New("s3: invalid format, host/region or bucket name not found") return Config{}, errors.New("s3: invalid format, host/region or bucket name not found")
} }
if prefix != "" { if prefix != "" {

View File

@ -229,12 +229,11 @@ func newS3TestSuite(t testing.TB) *test.Suite {
// NewConfig returns a config for a new temporary backend that will be used in tests. // NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) { NewConfig: func() (interface{}, error) {
s3cfg, err := s3.ParseConfig(os.Getenv("RESTIC_TEST_S3_REPOSITORY")) cfg, err := s3.ParseConfig(os.Getenv("RESTIC_TEST_S3_REPOSITORY"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg := s3cfg.(s3.Config)
cfg.KeyID = os.Getenv("RESTIC_TEST_S3_KEY") cfg.KeyID = os.Getenv("RESTIC_TEST_S3_KEY")
cfg.Secret = options.NewSecretString(os.Getenv("RESTIC_TEST_S3_SECRET")) cfg.Secret = options.NewSecretString(os.Getenv("RESTIC_TEST_S3_SECRET"))
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano()) cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())

View File

@ -35,14 +35,14 @@ func init() {
// and sftp:user@host:directory. The directory will be path Cleaned and can // and sftp:user@host:directory. The directory will be path Cleaned and can
// be an absolute path if it starts with a '/' (e.g. // be an absolute path if it starts with a '/' (e.g.
// sftp://user@host//absolute and sftp:user@host:/absolute). // sftp://user@host//absolute and sftp:user@host:/absolute).
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
var user, host, port, dir string var user, host, port, dir string
switch { switch {
case strings.HasPrefix(s, "sftp://"): case strings.HasPrefix(s, "sftp://"):
// parse the "sftp://user@host/path" url format // parse the "sftp://user@host/path" url format
url, err := url.Parse(s) url, err := url.Parse(s)
if err != nil { if err != nil {
return nil, errors.WithStack(err) return Config{}, errors.WithStack(err)
} }
if url.User != nil { if url.User != nil {
user = url.User.Username() user = url.User.Username()
@ -51,7 +51,7 @@ func ParseConfig(s string) (interface{}, error) {
port = url.Port() port = url.Port()
dir = url.Path dir = url.Path
if dir == "" { if dir == "" {
return nil, errors.Errorf("invalid backend %q, no directory specified", s) return Config{}, errors.Errorf("invalid backend %q, no directory specified", s)
} }
dir = dir[1:] dir = dir[1:]
@ -63,7 +63,7 @@ func ParseConfig(s string) (interface{}, error) {
var colon bool var colon bool
host, dir, colon = strings.Cut(s, ":") host, dir, colon = strings.Cut(s, ":")
if !colon { if !colon {
return nil, errors.New("sftp: invalid format, hostname or path not found") return Config{}, errors.New("sftp: invalid format, hostname or path not found")
} }
// split user and host at the "@" // split user and host at the "@"
data := strings.SplitN(host, "@", 3) data := strings.SplitN(host, "@", 3)
@ -75,12 +75,12 @@ func ParseConfig(s string) (interface{}, error) {
host = data[1] host = data[1]
} }
default: default:
return nil, errors.New(`invalid format, does not start with "sftp:"`) return Config{}, errors.New(`invalid format, does not start with "sftp:"`)
} }
p := path.Clean(dir) p := path.Clean(dir)
if strings.HasPrefix(p, "~") { if strings.HasPrefix(p, "~") {
return nil, errors.New("sftp path starts with the tilde (~) character, that fails for most sftp servers.\nUse a relative directory, most servers interpret this as relative to the user's home directory") return Config{}, errors.New("sftp path starts with the tilde (~) character, that fails for most sftp servers.\nUse a relative directory, most servers interpret this as relative to the user's home directory")
} }
cfg := NewConfig() cfg := NewConfig()

View File

@ -50,19 +50,19 @@ func NewConfig() Config {
} }
// ParseConfig parses the string s and extract swift's container name and prefix. // ParseConfig parses the string s and extract swift's container name and prefix.
func ParseConfig(s string) (interface{}, error) { func ParseConfig(s string) (Config, error) {
if !strings.HasPrefix(s, "swift:") { if !strings.HasPrefix(s, "swift:") {
return nil, errors.New("invalid URL, expected: swift:container-name:/[prefix]") return Config{}, errors.New("invalid URL, expected: swift:container-name:/[prefix]")
} }
s = strings.TrimPrefix(s, "swift:") s = strings.TrimPrefix(s, "swift:")
container, prefix, _ := strings.Cut(s, ":") container, prefix, _ := strings.Cut(s, ":")
if prefix == "" { if prefix == "" {
return nil, errors.Errorf("prefix is empty") return Config{}, errors.Errorf("prefix is empty")
} }
if prefix[0] != '/' { if prefix[0] != '/' {
return nil, errors.Errorf("prefix does not start with slash (/)") return Config{}, errors.Errorf("prefix does not start with slash (/)")
} }
prefix = prefix[1:] prefix = prefix[1:]

View File

@ -33,16 +33,11 @@ var configTests = []struct {
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
for _, test := range configTests { for _, test := range configTests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
v, err := ParseConfig(test.s) cfg, err := ParseConfig(test.s)
if err != nil { if err != nil {
t.Fatalf("parsing %q failed: %v", test.s, err) t.Fatalf("parsing %q failed: %v", test.s, err)
} }
cfg, ok := v.(Config)
if !ok {
t.Fatalf("wrong type returned, want Config, got %T", cfg)
}
if cfg != test.cfg { if cfg != test.cfg {
t.Fatalf("wrong output for %q, want:\n %#v\ngot:\n %#v", t.Fatalf("wrong output for %q, want:\n %#v\ngot:\n %#v",
test.s, test.cfg, cfg) test.s, test.cfg, cfg)

View File

@ -43,12 +43,11 @@ func newSwiftTestSuite(t testing.TB) *test.Suite {
// NewConfig returns a config for a new temporary backend that will be used in tests. // NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) { NewConfig: func() (interface{}, error) {
swiftcfg, err := swift.ParseConfig(os.Getenv("RESTIC_TEST_SWIFT")) cfg, err := swift.ParseConfig(os.Getenv("RESTIC_TEST_SWIFT"))
if err != nil { if err != nil {
return nil, err return nil, err
} }
cfg := swiftcfg.(swift.Config)
if err = swift.ApplyEnvironment("RESTIC_TEST_", &cfg); err != nil { if err = swift.ApplyEnvironment("RESTIC_TEST_", &cfg); err != nil {
return nil, err return nil, err
} }