2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

feat: support reading REST credentials from env

This commit is contained in:
Agatha V. Lovelace 2023-09-26 08:56:52 +02:00
parent 6e586b64e4
commit f1877e721e
No known key found for this signature in database
GPG Key ID: 01D0B3AB10CED4F8

View File

@ -2,10 +2,12 @@ package rest
import ( import (
"net/url" "net/url"
"os"
"strings" "strings"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/options" "github.com/restic/restic/internal/options"
"github.com/restic/restic/internal/restic"
) )
// Config contains all configuration necessary to connect to a REST server. // Config contains all configuration necessary to connect to a REST server.
@ -70,3 +72,19 @@ func prepareURL(s string) string {
} }
return s return s
} }
var _ restic.ApplyEnvironmenter = &Config{}
// ApplyEnvironment saves values from the environment to the config.
func (cfg *Config) ApplyEnvironment(prefix string) {
username := cfg.URL.User.Username()
_, pwdSet := cfg.URL.User.Password()
// Only apply env variable values if neither username nor password are provided.
if username == "" && !pwdSet {
envName := os.Getenv(prefix + "RESTIC_REST_USERNAME")
envPwd := os.Getenv(prefix + "RESTIC_REST_PASSWORD")
cfg.URL.User = url.UserPassword(envName, envPwd)
}
}