exclude items from config when empty and rest options

This commit is contained in:
cupcakearmy 2021-04-23 13:11:15 +02:00
parent bacbd0f806
commit 188560395d
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
3 changed files with 50 additions and 23 deletions

View File

@ -4,19 +4,25 @@ import (
"crypto/rand"
"encoding/base64"
"fmt"
"net/url"
"os"
"strings"
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/spf13/viper"
)
type BackendRest struct {
User string `yaml:"user,omitempty"`
Password string `yaml:"password,omitempty"`
}
type Backend struct {
name string
Type string `mapstructure:"type,omitempty"`
Path string `mapstructure:"path,omitempty"`
Key string `mapstructure:"key,omitempty"`
Env map[string]string `mapstructure:"env,omitempty"`
Type string `yaml:"type,omitempty"`
Path string `yaml:"path,omitempty"`
Key string `yaml:"key,omitempty"`
Env map[string]string `yaml:"env,omitempty"`
Rest BackendRest `yaml:"rest,omitempty"`
}
func GetBackend(name string) (Backend, bool) {
@ -29,7 +35,20 @@ func (b Backend) generateRepo() (string, error) {
switch b.Type {
case "local":
return GetPathRelativeToConfig(b.Path)
case "b2", "azure", "gs", "s3", "sftp", "rest":
case "rest":
parsed, err := url.Parse(b.Path)
if err != nil {
return "", err
}
if b.Rest.User != "" {
if b.Rest.Password == "" {
parsed.User = url.User(b.Rest.User)
} else {
parsed.User = url.UserPassword(b.Rest.User, b.Rest.Password)
}
}
return fmt.Sprintf("%s:%s", b.Type, parsed.String()), nil
case "b2", "azure", "gs", "s3", "sftp":
return fmt.Sprintf("%s:%s", b.Type, b.Path), nil
default:
return "", fmt.Errorf("backend type \"%s\" is invalid", b.Type)
@ -70,15 +89,10 @@ func (b Backend) validate() error {
c := GetConfig()
tmp := c.Backends[b.name]
tmp.Key = key
tmp.name = ""
c.Backends[b.name] = tmp
file := viper.ConfigFileUsed()
if err := CopyFile(file, file+".old"); err != nil {
if err := c.SaveConfig(); err != nil {
return err
}
colors.Secondary.Println("Saved a backup copy of your file next the the original.")
viper.Set("backends", c.Backends)
viper.WriteConfig()
}
env, err := b.getEnv()
if err != nil {

View File

@ -12,14 +12,14 @@ import (
"github.com/spf13/viper"
)
const VERSION = "1.0.3"
const VERSION = "1.0.4"
var CI bool = false
var VERBOSE bool = false
type Config struct {
Locations map[string]Location `mapstructure:"locations"`
Backends map[string]Backend `mapstructure:"backends"`
Locations map[string]Location `yaml:"locations"`
Backends map[string]Backend `yaml:"backends"`
}
var once sync.Once
@ -197,3 +197,16 @@ func AddFlagsToCommand(cmd *cobra.Command, backend bool) {
cmd.PersistentFlags().StringSliceP("location", "l", []string{}, "Locations")
}
}
func (c *Config) SaveConfig() error {
file := viper.ConfigFileUsed()
if err := CopyFile(file, file+".old"); err != nil {
return err
}
colors.Secondary.Println("Saved a backup copy of your file next the the original.")
viper.Set("backends", c.Backends)
viper.Set("locations", c.Locations)
return viper.WriteConfig()
}

View File

@ -24,19 +24,19 @@ const (
type HookArray = []string
type Hooks struct {
Before HookArray `mapstructure:"before"`
After HookArray `mapstructure:"after"`
Before HookArray `yaml:"before"`
After HookArray `yaml:"after"`
}
type Options map[string]map[string][]string
type Location struct {
name string `mapstructure:",omitempty"`
From string `mapstructure:"from,omitempty"`
To []string `mapstructure:"to,omitempty"`
Hooks Hooks `mapstructure:"hooks,omitempty"`
Cron string `mapstructure:"cron,omitempty"`
Options Options `mapstructure:"options,omitempty"`
name string `yaml:",omitempty"`
From string `yaml:"from,omitempty"`
To []string `yaml:"to,omitempty"`
Hooks Hooks `yaml:"hooks,omitempty"`
Cron string `yaml:"cron,omitempty"`
Options Options `yaml:"options,omitempty"`
}
func GetLocation(name string) (Location, bool) {