mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-12-09 22:04:59 +00:00
ability to use keys from envs
This commit is contained in:
@@ -58,7 +58,15 @@ func (b Backend) generateRepo() (string, error) {
|
||||
|
||||
func (b Backend) getEnv() (map[string]string, error) {
|
||||
env := make(map[string]string)
|
||||
env["RESTIC_PASSWORD"] = b.Key
|
||||
if b.Key != "" {
|
||||
env["RESTIC_PASSWORD"] = b.Key
|
||||
} else {
|
||||
key, err := b.getKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
env["RESTIC_PASSWORD"] = key
|
||||
}
|
||||
repo, err := b.generateRepo()
|
||||
env["RESTIC_REPOSITORY"] = repo
|
||||
for key, value := range b.Env {
|
||||
@@ -77,6 +85,16 @@ func generateRandomKey() string {
|
||||
return key
|
||||
}
|
||||
|
||||
func (b Backend) getKey() (string, error) {
|
||||
if b.Key != "" {
|
||||
return b.Key, nil
|
||||
}
|
||||
if key, found := os.LookupEnv("AUTORESTIC_KEY_" + strings.ToUpper(b.name)); found {
|
||||
return key, nil
|
||||
}
|
||||
return "", fmt.Errorf("no key found for backend \"%s\"", b.name)
|
||||
}
|
||||
|
||||
func (b Backend) validate() error {
|
||||
if b.Type == "" {
|
||||
return fmt.Errorf(`Backend "%s" has no "type"`, b.name)
|
||||
@@ -85,14 +103,18 @@ func (b Backend) validate() error {
|
||||
return fmt.Errorf(`Backend "%s" has no "path"`, b.name)
|
||||
}
|
||||
if b.Key == "" {
|
||||
key := generateRandomKey()
|
||||
b.Key = key
|
||||
c := GetConfig()
|
||||
tmp := c.Backends[b.name]
|
||||
tmp.Key = key
|
||||
c.Backends[b.name] = tmp
|
||||
if err := c.SaveConfig(); err != nil {
|
||||
return err
|
||||
// Check if key is set in environment
|
||||
if _, err := b.getKey(); err != nil {
|
||||
// If not generate a new one
|
||||
key := generateRandomKey()
|
||||
b.Key = key
|
||||
c := GetConfig()
|
||||
tmp := c.Backends[b.name]
|
||||
tmp.Key = key
|
||||
c.Backends[b.name] = tmp
|
||||
if err := c.SaveConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
env, err := b.getEnv()
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||
"github.com/cupcakearmy/autorestic/internal/lock"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
@@ -36,7 +37,13 @@ func GetConfig() *Config {
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
if !CRON_LEAN {
|
||||
absConfig, _ := filepath.Abs(viper.ConfigFileUsed())
|
||||
colors.Faint.Println("Using config file:", absConfig)
|
||||
colors.Faint.Println("Using config: \t", absConfig)
|
||||
// Load env file
|
||||
envFile := filepath.Join(filepath.Dir(absConfig), ".autorestic.env")
|
||||
err = godotenv.Load(envFile)
|
||||
if err == nil {
|
||||
colors.Faint.Println("Using env:\t", envFile)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user