backend: Move environment based configuration into backend
This commit is contained in:
parent
49a6a4f5bf
commit
2f7b4ceae1
5 changed files with 84 additions and 44 deletions
|
@ -559,24 +559,10 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro
|
||||||
|
|
||||||
case "s3":
|
case "s3":
|
||||||
cfg := loc.Config.(s3.Config)
|
cfg := loc.Config.(s3.Config)
|
||||||
if cfg.KeyID == "" {
|
|
||||||
cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.Secret.String() == "" {
|
if err := s3.ApplyEnvironment(&cfg); err != nil {
|
||||||
cfg.Secret = options.NewSecretString(os.Getenv("AWS_SECRET_ACCESS_KEY"))
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
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.Region == "" {
|
|
||||||
cfg.Region = os.Getenv("AWS_DEFAULT_REGION")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -586,10 +572,10 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro
|
||||||
|
|
||||||
case "gs":
|
case "gs":
|
||||||
cfg := loc.Config.(gs.Config)
|
cfg := loc.Config.(gs.Config)
|
||||||
if cfg.ProjectID == "" {
|
|
||||||
cfg.ProjectID = os.Getenv("GOOGLE_PROJECT_ID")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if err := gs.ApplyEnvironment(&cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -599,18 +585,10 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro
|
||||||
|
|
||||||
case "azure":
|
case "azure":
|
||||||
cfg := loc.Config.(azure.Config)
|
cfg := loc.Config.(azure.Config)
|
||||||
if cfg.AccountName == "" {
|
|
||||||
cfg.AccountName = os.Getenv("AZURE_ACCOUNT_NAME")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.AccountKey.String() == "" {
|
if err := azure.ApplyEnvironment(&cfg); err != nil {
|
||||||
cfg.AccountKey = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_KEY"))
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.AccountSAS.String() == "" {
|
|
||||||
cfg.AccountSAS = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_SAS"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -635,22 +613,9 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro
|
||||||
case "b2":
|
case "b2":
|
||||||
cfg := loc.Config.(b2.Config)
|
cfg := loc.Config.(b2.Config)
|
||||||
|
|
||||||
if cfg.AccountID == "" {
|
if err := b2.ApplyEnvironment(&cfg); err != nil {
|
||||||
cfg.AccountID = os.Getenv("B2_ACCOUNT_ID")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.AccountID == "" {
|
|
||||||
return nil, 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 nil, errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
if err := opts.Apply(loc.Scheme, &cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package azure
|
package azure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -53,3 +54,20 @@ func ParseConfig(s string) (interface{}, error) {
|
||||||
cfg.Prefix = prefix
|
cfg.Prefix = prefix
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
|
func ApplyEnvironment(cfgRaw interface{}) error {
|
||||||
|
cfg := cfgRaw.(*Config)
|
||||||
|
if cfg.AccountName == "" {
|
||||||
|
cfg.AccountName = os.Getenv("AZURE_ACCOUNT_NAME")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.AccountKey.String() == "" {
|
||||||
|
cfg.AccountKey = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_KEY"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.AccountSAS.String() == "" {
|
||||||
|
cfg.AccountSAS = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_SAS"))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package b2
|
package b2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -79,3 +80,24 @@ func ParseConfig(s string) (interface{}, error) {
|
||||||
|
|
||||||
return cfg, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package gs
|
package gs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -56,3 +57,12 @@ func ParseConfig(s string) (interface{}, error) {
|
||||||
cfg.Prefix = prefix
|
cfg.Prefix = prefix
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
|
func ApplyEnvironment(cfgRaw interface{}) error {
|
||||||
|
cfg := cfgRaw.(*Config)
|
||||||
|
if cfg.ProjectID == "" {
|
||||||
|
cfg.ProjectID = os.Getenv("GOOGLE_PROJECT_ID")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package s3
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -91,3 +92,27 @@ func createConfig(endpoint, bucket, prefix string, useHTTP bool) (interface{}, e
|
||||||
cfg.Prefix = prefix
|
cfg.Prefix = prefix
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
|
func ApplyEnvironment(cfgRaw interface{}) error {
|
||||||
|
cfg := cfgRaw.(*Config)
|
||||||
|
if cfg.KeyID == "" {
|
||||||
|
cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Secret.String() == "" {
|
||||||
|
cfg.Secret = options.NewSecretString(os.Getenv("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("AWS_DEFAULT_REGION")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue