backend: let ParseConfig return a Config pointer

In order to change the backend initialization in `global.go` to be able
to generically call cfg.ApplyEnvironment() for supported backends, the
`interface{}` returned by `ParseConfig` must contain a pointer to the
configuration.

An alternative would be to use reflection to convert the type from
`interface{}(Config)` to `interface{}(*Config)` (from value to pointer
type). However, this would just complicate the type mess further.
This commit is contained in:
Michael Eischer
2023-04-21 21:35:34 +02:00
parent 25a0be7f26
commit f903db492c
26 changed files with 165 additions and 146 deletions

View File

@@ -45,7 +45,7 @@ func init() {
// supported configuration formats are s3://host/bucketname/prefix and
// 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.
func ParseConfig(s string) (Config, error) {
func ParseConfig(s string) (*Config, error) {
switch {
case strings.HasPrefix(s, "s3:http"):
// assume that a URL has been specified, parse it and
@@ -53,11 +53,11 @@ func ParseConfig(s string) (Config, error) {
// bucket name and prefix
url, err := url.Parse(s[3:])
if err != nil {
return Config{}, errors.WithStack(err)
return nil, errors.WithStack(err)
}
if url.Path == "" {
return Config{}, errors.New("s3: bucket name not found")
return nil, errors.New("s3: bucket name not found")
}
bucket, path, _ := strings.Cut(url.Path[1:], "/")
@@ -67,7 +67,7 @@ func ParseConfig(s string) (Config, error) {
case strings.HasPrefix(s, "s3:"):
s = s[3:]
default:
return Config{}, errors.New("s3: invalid format")
return nil, errors.New("s3: invalid format")
}
// use the first entry of the path as the endpoint and the
// remainder as bucket name and prefix
@@ -76,9 +76,9 @@ func ParseConfig(s string) (Config, error) {
return createConfig(endpoint, bucket, prefix, false)
}
func createConfig(endpoint, bucket, prefix string, useHTTP bool) (Config, error) {
func createConfig(endpoint, bucket, prefix string, useHTTP bool) (*Config, error) {
if endpoint == "" {
return Config{}, 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")
}
if prefix != "" {
@@ -90,7 +90,7 @@ func createConfig(endpoint, bucket, prefix string, useHTTP bool) (Config, error)
cfg.UseHTTP = useHTTP
cfg.Bucket = bucket
cfg.Prefix = prefix
return cfg, nil
return &cfg, nil
}
// ApplyEnvironment saves values from the environment to the config.

View File

@@ -128,7 +128,7 @@ func newMinioTestSuite(ctx context.Context, t testing.TB) *test.Suite[MinioTestC
return &test.Suite[MinioTestConfig]{
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (MinioTestConfig, error) {
NewConfig: func() (*MinioTestConfig, error) {
cfg := MinioTestConfig{}
cfg.tempdir = rtest.TempDir(t)
@@ -142,7 +142,7 @@ func newMinioTestSuite(ctx context.Context, t testing.TB) *test.Suite[MinioTestC
cfg.Config.UseHTTP = true
cfg.Config.KeyID = key
cfg.Config.Secret = options.NewSecretString(secret)
return cfg, nil
return &cfg, nil
},
// CreateFn is a function that creates a temporary repository for the tests.
@@ -224,10 +224,10 @@ func newS3TestSuite(t testing.TB) *test.Suite[s3.Config] {
MinimalData: true,
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (s3.Config, error) {
NewConfig: func() (*s3.Config, error) {
cfg, err := s3.ParseConfig(os.Getenv("RESTIC_TEST_S3_REPOSITORY"))
if err != nil {
return s3.Config{}, err
return nil, err
}
cfg.KeyID = os.Getenv("RESTIC_TEST_S3_KEY")