diff --git a/cmd/restic/global.go b/cmd/restic/global.go index f08a75b13..1c13fb887 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -550,9 +550,7 @@ func OpenRepository(ctx context.Context, opts GlobalOptions) (*repository.Reposi func parseConfig(loc location.Location, opts options.Options) (interface{}, error) { cfg := loc.Config if cfg, ok := cfg.(restic.ApplyEnvironmenter); ok { - if err := cfg.ApplyEnvironment(""); err != nil { - return nil, err - } + cfg.ApplyEnvironment("") } // only apply options for a particular backend here diff --git a/internal/backend/azure/azure_test.go b/internal/backend/azure/azure_test.go index 0fab5da26..5aee96fbd 100644 --- a/internal/backend/azure/azure_test.go +++ b/internal/backend/azure/azure_test.go @@ -35,11 +35,7 @@ func newAzureTestSuite(t testing.TB) *test.Suite[azure.Config] { return nil, err } - err = cfg.ApplyEnvironment("RESTIC_TEST_") - if err != nil { - return nil, err - } - + cfg.ApplyEnvironment("RESTIC_TEST_") cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano()) return cfg, nil }, diff --git a/internal/backend/azure/config.go b/internal/backend/azure/config.go index 4d4e839ff..6786ec626 100644 --- a/internal/backend/azure/config.go +++ b/internal/backend/azure/config.go @@ -59,7 +59,7 @@ func ParseConfig(s string) (*Config, error) { var _ restic.ApplyEnvironmenter = &Config{} // ApplyEnvironment saves values from the environment to the config. -func (cfg *Config) ApplyEnvironment(prefix string) error { +func (cfg *Config) ApplyEnvironment(prefix string) { if cfg.AccountName == "" { cfg.AccountName = os.Getenv(prefix + "AZURE_ACCOUNT_NAME") } @@ -71,5 +71,4 @@ func (cfg *Config) ApplyEnvironment(prefix string) error { if cfg.AccountSAS.String() == "" { cfg.AccountSAS = options.NewSecretString(os.Getenv(prefix + "AZURE_ACCOUNT_SAS")) } - return nil } diff --git a/internal/backend/b2/b2.go b/internal/backend/b2/b2.go index eb2cfe3c2..3560cca49 100644 --- a/internal/backend/b2/b2.go +++ b/internal/backend/b2/b2.go @@ -58,6 +58,13 @@ func (s *sniffingRoundTripper) RoundTrip(req *http.Request) (*http.Response, err } func newClient(ctx context.Context, cfg Config, rt http.RoundTripper) (*b2.Client, error) { + if cfg.AccountID == "" { + return nil, errors.Fatalf("unable to open B2 backend: Account ID ($B2_ACCOUNT_ID) is empty") + } + if cfg.Key.String() == "" { + return nil, errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty") + } + sniffer := &sniffingRoundTripper{RoundTripper: rt} opts := []b2.ClientOption{b2.Transport(sniffer)} diff --git a/internal/backend/b2/b2_test.go b/internal/backend/b2/b2_test.go index 8e982adda..3a649db1e 100644 --- a/internal/backend/b2/b2_test.go +++ b/internal/backend/b2/b2_test.go @@ -35,11 +35,7 @@ func newB2TestSuite(t testing.TB) *test.Suite[b2.Config] { return nil, err } - err = cfg.ApplyEnvironment("RESTIC_TEST_") - if err != nil { - return nil, err - } - + cfg.ApplyEnvironment("RESTIC_TEST_") cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano()) return cfg, nil }, diff --git a/internal/backend/b2/config.go b/internal/backend/b2/config.go index 548fbef99..94614e44f 100644 --- a/internal/backend/b2/config.go +++ b/internal/backend/b2/config.go @@ -85,21 +85,11 @@ func ParseConfig(s string) (*Config, error) { var _ restic.ApplyEnvironmenter = &Config{} // ApplyEnvironment saves values from the environment to the config. -func (cfg *Config) ApplyEnvironment(prefix string) error { +func (cfg *Config) ApplyEnvironment(prefix string) { if cfg.AccountID == "" { cfg.AccountID = os.Getenv(prefix + "B2_ACCOUNT_ID") } - - if cfg.AccountID == "" { - return errors.Fatalf("unable to open B2 backend: Account ID ($B2_ACCOUNT_ID) is empty") - } - if cfg.Key.String() == "" { cfg.Key = options.NewSecretString(os.Getenv(prefix + "B2_ACCOUNT_KEY")) } - - if cfg.Key.String() == "" { - return errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty") - } - return nil } diff --git a/internal/backend/gs/config.go b/internal/backend/gs/config.go index b2d52c5f8..61a31113f 100644 --- a/internal/backend/gs/config.go +++ b/internal/backend/gs/config.go @@ -62,9 +62,8 @@ func ParseConfig(s string) (*Config, error) { var _ restic.ApplyEnvironmenter = &Config{} // ApplyEnvironment saves values from the environment to the config. -func (cfg *Config) ApplyEnvironment(prefix string) error { +func (cfg *Config) ApplyEnvironment(prefix string) { if cfg.ProjectID == "" { cfg.ProjectID = os.Getenv(prefix + "GOOGLE_PROJECT_ID") } - return nil } diff --git a/internal/backend/s3/config.go b/internal/backend/s3/config.go index 525373d16..8dcad9eee 100644 --- a/internal/backend/s3/config.go +++ b/internal/backend/s3/config.go @@ -97,24 +97,14 @@ func createConfig(endpoint, bucket, prefix string, useHTTP bool) (*Config, error var _ restic.ApplyEnvironmenter = &Config{} // ApplyEnvironment saves values from the environment to the config. -func (cfg *Config) ApplyEnvironment(prefix string) error { +func (cfg *Config) ApplyEnvironment(prefix string) { if cfg.KeyID == "" { cfg.KeyID = os.Getenv(prefix + "AWS_ACCESS_KEY_ID") } - if cfg.Secret.String() == "" { cfg.Secret = options.NewSecretString(os.Getenv(prefix + "AWS_SECRET_ACCESS_KEY")) } - - if cfg.KeyID == "" && cfg.Secret.String() != "" { - return errors.Fatalf("unable to open S3 backend: Key ID ($AWS_ACCESS_KEY_ID) is empty") - } else if cfg.KeyID != "" && cfg.Secret.String() == "" { - return errors.Fatalf("unable to open S3 backend: Secret ($AWS_SECRET_ACCESS_KEY) is empty") - } - if cfg.Region == "" { cfg.Region = os.Getenv(prefix + "AWS_DEFAULT_REGION") } - - return nil } diff --git a/internal/backend/s3/s3.go b/internal/backend/s3/s3.go index dd5cc36e6..10512e809 100644 --- a/internal/backend/s3/s3.go +++ b/internal/backend/s3/s3.go @@ -41,6 +41,12 @@ const defaultLayout = "default" func open(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) { debug.Log("open, config %#v", cfg) + if cfg.KeyID == "" && cfg.Secret.String() != "" { + return nil, errors.Fatalf("unable to open S3 backend: Key ID ($AWS_ACCESS_KEY_ID) is empty") + } else if cfg.KeyID != "" && cfg.Secret.String() == "" { + return nil, errors.Fatalf("unable to open S3 backend: Secret ($AWS_SECRET_ACCESS_KEY) is empty") + } + if cfg.MaxRetries > 0 { minio.MaxRetry = int(cfg.MaxRetries) } diff --git a/internal/backend/swift/config.go b/internal/backend/swift/config.go index b9f5d3995..5be2d9ce0 100644 --- a/internal/backend/swift/config.go +++ b/internal/backend/swift/config.go @@ -77,7 +77,7 @@ func ParseConfig(s string) (*Config, error) { var _ restic.ApplyEnvironmenter = &Config{} // ApplyEnvironment saves values from the environment to the config. -func (cfg *Config) ApplyEnvironment(prefix string) error { +func (cfg *Config) ApplyEnvironment(prefix string) { for _, val := range []struct { s *string env string @@ -130,5 +130,4 @@ func (cfg *Config) ApplyEnvironment(prefix string) error { *val.s = options.NewSecretString(os.Getenv(val.env)) } } - return nil } diff --git a/internal/backend/swift/swift_test.go b/internal/backend/swift/swift_test.go index cb0992010..52278943e 100644 --- a/internal/backend/swift/swift_test.go +++ b/internal/backend/swift/swift_test.go @@ -48,9 +48,7 @@ func newSwiftTestSuite(t testing.TB) *test.Suite[swift.Config] { return nil, err } - if err = cfg.ApplyEnvironment("RESTIC_TEST_"); err != nil { - return nil, err - } + cfg.ApplyEnvironment("RESTIC_TEST_") cfg.Prefix += fmt.Sprintf("/test-%d", time.Now().UnixNano()) t.Logf("using prefix %v", cfg.Prefix) return cfg, nil diff --git a/internal/restic/backend.go b/internal/restic/backend.go index b6653fcb4..555b9d96e 100644 --- a/internal/restic/backend.go +++ b/internal/restic/backend.go @@ -83,5 +83,5 @@ type FileInfo struct { // ApplyEnvironmenter fills in a backend configuration from the environment type ApplyEnvironmenter interface { - ApplyEnvironment(prefix string) error + ApplyEnvironment(prefix string) }