Adjust config options and processing

This commit is contained in:
MaxJa4 2023-08-27 20:14:59 +02:00
parent 5e464669f9
commit 536af29222
2 changed files with 83 additions and 83 deletions

View File

@ -16,84 +16,74 @@ import (
// Config holds all configuration values that are expected to be set // Config holds all configuration values that are expected to be set
// by users. // by users.
type Config struct { type Config struct {
AwsS3BucketName string `split_words:"true"` AwsS3BucketName string `env:"AWS_S3_BUCKET_NAME"`
AwsS3Path string `split_words:"true"` AwsS3Path string `env:"AWS_S3_PATH"`
AwsEndpoint string `split_words:"true" default:"s3.amazonaws.com"` AwsEndpoint string `envDefault:"s3.amazonaws.com"`
AwsEndpointProto string `split_words:"true" default:"https"` AwsEndpointProto string `envDefault:"https"`
AwsEndpointInsecure bool `split_words:"true"` AwsEndpointInsecure bool
AwsEndpointCACert CertDecoder `envconfig:"AWS_ENDPOINT_CA_CERT"` AwsEndpointCACert CertDecoder `env:"AWS_ENDPOINT_CA_CERT"`
AwsStorageClass string `split_words:"true"` AwsStorageClass string
AwsAccessKeyID string `envconfig:"AWS_ACCESS_KEY_ID"` AwsAccessKeyID string `env:"AWS_ACCESS_KEY_ID"`
AwsAccessKeyIDFile string `envconfig:"AWS_ACCESS_KEY_ID_FILE"` AwsAccessKeyIDFile string `env:"AWS_ACCESS_KEY_ID_FILE,file"`
AwsSecretAccessKey string `split_words:"true"` AwsSecretAccessKey string
AwsSecretAccessKeyFile string `split_words:"true"` AwsSecretAccessKeyFile string `env:"AWS_SECRET_ACCESS_KEY_FILE,file"`
AwsIamRoleEndpoint string `split_words:"true"` AwsIamRoleEndpoint string
AwsPartSize int64 `split_words:"true"` AwsPartSize int64
BackupCompression CompressionType `split_words:"true" default:"gz"` BackupCompression CompressionType `envDefault:"gz"`
BackupSources string `split_words:"true" default:"/backup"` BackupSources string `envDefault:"/backup"`
BackupFilename string `split_words:"true" default:"backup-%Y-%m-%dT%H-%M-%S.{{ .Extension }}"` BackupFilename string `envDefault:"backup-%Y-%m-%dT%H-%M-%S.{{ .Extension }}"`
BackupFilenameExpand bool `split_words:"true"` BackupFilenameExpand bool
BackupLatestSymlink string `split_words:"true"` BackupLatestSymlink string
BackupArchive string `split_words:"true" default:"/archive"` BackupArchive string `envDefault:"/archive"`
BackupRetentionDays int32 `split_words:"true" default:"-1"` BackupRetentionDays int32 `envDefault:"-1"`
BackupPruningLeeway time.Duration `split_words:"true" default:"1m"` BackupPruningLeeway time.Duration `envDefault:"1m"`
BackupPruningPrefix string `split_words:"true"` BackupPruningPrefix string
BackupStopContainerLabel string `split_words:"true" default:"true"` BackupStopContainerLabel string `envDefault:"true"`
BackupFromSnapshot bool `split_words:"true"` BackupFromSnapshot bool
BackupExcludeRegexp RegexpDecoder `split_words:"true"` BackupExcludeRegexp RegexpDecoder
BackupSkipBackendsFromPrune []string `split_words:"true"` BackupSkipBackendsFromPrune []string
GpgPassphrase string `split_words:"true"` GpgPassphrase string
NotificationURLs []string `envconfig:"NOTIFICATION_URLS"` NotificationURLs []string `env:"NOTIFICATION_URLS"`
NotificationLevel string `split_words:"true" default:"error"` NotificationLevel string `envDefault:"error"`
EmailNotificationRecipient string `split_words:"true"` EmailNotificationRecipient string
EmailNotificationSender string `split_words:"true" default:"noreply@nohost"` EmailNotificationSender string `envDefault:"noreply@nohost"`
EmailSMTPHost string `envconfig:"EMAIL_SMTP_HOST"` EmailSMTPHost string `env:"EMAIL_SMTP_HOST"`
EmailSMTPPort int `envconfig:"EMAIL_SMTP_PORT" default:"587"` EmailSMTPPort int `env:"EMAIL_SMTP_PORT" envDefault:"587"`
EmailSMTPUsername string `envconfig:"EMAIL_SMTP_USERNAME"` EmailSMTPUsername string `env:"EMAIL_SMTP_USERNAME"`
EmailSMTPPassword string `envconfig:"EMAIL_SMTP_PASSWORD"` EmailSMTPPassword string `env:"EMAIL_SMTP_PASSWORD"`
WebdavUrl string `split_words:"true"` WebdavUrl string
WebdavUrlInsecure bool `split_words:"true"` WebdavUrlInsecure bool
WebdavPath string `split_words:"true" default:"/"` WebdavPath string `envDefault:"/"`
WebdavUsername string `split_words:"true"` WebdavUsername string
WebdavPassword string `split_words:"true"` WebdavPassword string
SSHHostName string `split_words:"true"` SSHHostName string `env:"SSH_HOST_NAME"`
SSHPort string `split_words:"true" default:"22"` SSHPort string `env:"SSH_PORT" envDefault:"22"`
SSHUser string `split_words:"true"` SSHUser string `env:"SSH_USER"`
SSHPassword string `split_words:"true"` SSHPassword string `env:"SSH_PASSWORD"`
SSHIdentityFile string `split_words:"true" default:"/root/.ssh/id_rsa"` SSHIdentityFile string `env:"SSH_IDENTITY_FILE" envDefault:"/root/.ssh/id_rsa"`
SSHIdentityPassphrase string `split_words:"true"` SSHIdentityPassphrase string `env:"SSH_IDENTITY_PASSPHRASE"`
SSHRemotePath string `split_words:"true"` SSHRemotePath string `env:"SSH_REMOTE_PATH"`
ExecLabel string `split_words:"true"` ExecLabel string
ExecForwardOutput bool `split_words:"true"` ExecForwardOutput bool
LockTimeout time.Duration `split_words:"true" default:"60m"` LockTimeout time.Duration `envDefault:"60m"`
AzureStorageAccountName string `split_words:"true"` AzureStorageAccountName string
AzureStoragePrimaryAccountKey string `split_words:"true"` AzureStoragePrimaryAccountKey string
AzureStorageContainerName string `split_words:"true"` AzureStorageContainerName string
AzureStoragePath string `split_words:"true"` AzureStoragePath string
AzureStorageEndpoint string `split_words:"true" default:"https://{{ .AccountName }}.blob.core.windows.net/"` AzureStorageEndpoint string `envDefault:"https://{{ .AccountName }}.blob.core.windows.net/"`
DropboxEndpoint string `split_words:"true" default:"https://api.dropbox.com/"` DropboxEndpoint string `envDefault:"https://api.dropbox.com/"`
DropboxOAuth2Endpoint string `envconfig:"DROPBOX_OAUTH2_ENDPOINT" default:"https://api.dropbox.com/"` DropboxOAuth2Endpoint string `env:"DROPBOX_OAUTH2_ENDPOINT" envDefault:"https://api.dropbox.com/"`
DropboxRefreshToken string `split_words:"true"` DropboxRefreshToken string
DropboxAppKey string `split_words:"true"` DropboxAppKey string
DropboxAppSecret string `split_words:"true"` DropboxAppSecret string
DropboxRemotePath string `split_words:"true"` DropboxRemotePath string
DropboxConcurrencyLevel NaturalNumber `split_words:"true" default:"6"` DropboxConcurrencyLevel NaturalNumber `envDefault:"6"`
}
func (c *Config) resolveSecret(envVar string, secretPath string) (string, error) {
if secretPath == "" {
return envVar, nil
}
data, err := os.ReadFile(secretPath)
if err != nil {
return "", fmt.Errorf("resolveSecret: error reading secret path: %w", err)
}
return string(data), nil
} }
type CompressionType string type CompressionType string
func (c *CompressionType) Decode(v string) error { func (c *CompressionType) UnmarshalText(text []byte) error {
v := string(text)
switch v { switch v {
case "gz", "zst": case "gz", "zst":
*c = CompressionType(v) *c = CompressionType(v)
@ -111,7 +101,8 @@ type CertDecoder struct {
Cert *x509.Certificate Cert *x509.Certificate
} }
func (c *CertDecoder) Decode(v string) error { func (c *CertDecoder) UnmarshalText(text []byte) error {
v := string(text)
if v == "" { if v == "" {
return nil return nil
} }
@ -132,7 +123,8 @@ type RegexpDecoder struct {
Re *regexp.Regexp Re *regexp.Regexp
} }
func (r *RegexpDecoder) Decode(v string) error { func (r *RegexpDecoder) UnmarshalText(text []byte) error {
v := string(text)
if v == "" { if v == "" {
return nil return nil
} }
@ -146,7 +138,8 @@ func (r *RegexpDecoder) Decode(v string) error {
type NaturalNumber int type NaturalNumber int
func (n *NaturalNumber) Decode(v string) error { func (n *NaturalNumber) UnmarshalText(text []byte) error {
v := string(text)
asInt, err := strconv.Atoi(v) asInt, err := strconv.Atoi(v)
if err != nil { if err != nil {
return fmt.Errorf("config: error converting %s to int", v) return fmt.Errorf("config: error converting %s to int", v)

View File

@ -140,14 +140,21 @@ func newScript() (*script, error) {
} }
if s.c.AwsS3BucketName != "" { if s.c.AwsS3BucketName != "" {
accessKeyID, err := s.c.resolveSecret(s.c.AwsAccessKeyID, s.c.AwsAccessKeyIDFile) var accessKeyID string
if err != nil { var secretAccessKey string
return nil, fmt.Errorf("newScript: error resolving AwsAccessKeyID: %w", err)
if s.c.AwsAccessKeyIDFile != "" {
accessKeyID = s.c.AwsAccessKeyIDFile
} else {
accessKeyID = s.c.AwsAccessKeyID
} }
secretAccessKey, err := s.c.resolveSecret(s.c.AwsSecretAccessKey, s.c.AwsSecretAccessKeyFile)
if err != nil { if s.c.AwsSecretAccessKeyFile != "" {
return nil, fmt.Errorf("newScript: error resolving AwsSecretAccessKey: %w", err) secretAccessKey = s.c.AwsSecretAccessKeyFile
} else {
secretAccessKey = s.c.AwsSecretAccessKey
} }
s3Config := s3.Config{ s3Config := s3.Config{
Endpoint: s.c.AwsEndpoint, Endpoint: s.c.AwsEndpoint,
AccessKeyID: accessKeyID, AccessKeyID: accessKeyID,