restic/internal/backend/azure/config.go

74 lines
1.8 KiB
Go
Raw Normal View History

package azure
import (
"os"
"path"
"strings"
2017-08-05 19:46:15 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/options"
)
// Config contains all configuration necessary to connect to an azure compatible
// server.
type Config struct {
AccountName string
2022-03-05 18:16:13 +00:00
AccountSAS options.SecretString
AccountKey options.SecretString
Container string
Prefix string
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
}
// NewConfig returns a new Config with the default values filled in.
func NewConfig() Config {
return Config{
Connections: 5,
}
}
func init() {
options.Register("azure", Config{})
}
// ParseConfig parses the string s and extracts the azure config. The
// configuration format is azure:containerName:/[prefix].
func ParseConfig(s string) (Config, error) {
2017-08-06 18:13:45 +00:00
if !strings.HasPrefix(s, "azure:") {
return Config{}, errors.New("azure: invalid format")
}
2017-08-06 18:13:45 +00:00
// strip prefix "azure:"
s = s[6:]
// use the first entry of the path as the bucket name and the
// remainder as prefix
container, prefix, colon := strings.Cut(s, ":")
if !colon {
return Config{}, errors.New("azure: invalid format: bucket name or path not found")
}
prefix = strings.TrimPrefix(path.Clean(prefix), "/")
2017-08-06 18:13:45 +00:00
cfg := NewConfig()
cfg.Container = container
cfg.Prefix = prefix
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
}