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

@@ -30,10 +30,10 @@ func newB2TestSuite(t testing.TB) *test.Suite[b2.Config] {
WaitForDelayedRemoval: 10 * time.Second,
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (b2.Config, error) {
NewConfig: func() (*b2.Config, error) {
cfg, err := b2.ParseConfig(os.Getenv("RESTIC_TEST_B2_REPOSITORY"))
if err != nil {
return b2.Config{}, err
return nil, err
}
cfg.AccountID = os.Getenv("RESTIC_TEST_B2_ACCOUNT_ID")

View File

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