backend: Move environment based configuration into backend

This commit is contained in:
Michael Eischer
2023-04-15 10:25:45 +02:00
parent 49a6a4f5bf
commit 2f7b4ceae1
5 changed files with 84 additions and 44 deletions

View File

@@ -1,6 +1,7 @@
package b2
import (
"os"
"path"
"regexp"
"strings"
@@ -79,3 +80,24 @@ func ParseConfig(s string) (interface{}, error) {
return cfg, nil
}
// ApplyEnvironment saves values from the environment to the config.
func ApplyEnvironment(cfgRaw interface{}) error {
cfg := cfgRaw.(*Config)
if cfg.AccountID == "" {
cfg.AccountID = os.Getenv("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("B2_ACCOUNT_KEY"))
}
if cfg.Key.String() == "" {
return errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty")
}
return nil
}