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:
parent
25a0be7f26
commit
f903db492c
26 changed files with 165 additions and 146 deletions
|
@ -50,19 +50,19 @@ func NewConfig() Config {
|
|||
}
|
||||
|
||||
// ParseConfig parses the string s and extract swift's container name and prefix.
|
||||
func ParseConfig(s string) (Config, error) {
|
||||
func ParseConfig(s string) (*Config, error) {
|
||||
if !strings.HasPrefix(s, "swift:") {
|
||||
return Config{}, errors.New("invalid URL, expected: swift:container-name:/[prefix]")
|
||||
return nil, errors.New("invalid URL, expected: swift:container-name:/[prefix]")
|
||||
}
|
||||
s = strings.TrimPrefix(s, "swift:")
|
||||
|
||||
container, prefix, _ := strings.Cut(s, ":")
|
||||
if prefix == "" {
|
||||
return Config{}, errors.Errorf("prefix is empty")
|
||||
return nil, errors.Errorf("prefix is empty")
|
||||
}
|
||||
|
||||
if prefix[0] != '/' {
|
||||
return Config{}, errors.Errorf("prefix does not start with slash (/)")
|
||||
return nil, errors.Errorf("prefix does not start with slash (/)")
|
||||
}
|
||||
prefix = prefix[1:]
|
||||
|
||||
|
@ -70,7 +70,7 @@ func ParseConfig(s string) (Config, error) {
|
|||
cfg.Container = container
|
||||
cfg.Prefix = prefix
|
||||
|
||||
return cfg, nil
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
// ApplyEnvironment saves values from the environment to the config.
|
||||
|
|
|
@ -42,14 +42,14 @@ func newSwiftTestSuite(t testing.TB) *test.Suite[swift.Config] {
|
|||
},
|
||||
|
||||
// NewConfig returns a config for a new temporary backend that will be used in tests.
|
||||
NewConfig: func() (swift.Config, error) {
|
||||
NewConfig: func() (*swift.Config, error) {
|
||||
cfg, err := swift.ParseConfig(os.Getenv("RESTIC_TEST_SWIFT"))
|
||||
if err != nil {
|
||||
return swift.Config{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = swift.ApplyEnvironment("RESTIC_TEST_", &cfg); err != nil {
|
||||
return swift.Config{}, err
|
||||
return nil, err
|
||||
}
|
||||
cfg.Prefix += fmt.Sprintf("/test-%d", time.Now().UnixNano())
|
||||
t.Logf("using prefix %v", cfg.Prefix)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue