exclude items from config when empty and rest options

pull/51/head
cupcakearmy 4 years ago
parent bacbd0f806
commit 188560395d
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9

@ -4,19 +4,25 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/url"
"os" "os"
"strings" "strings"
"github.com/cupcakearmy/autorestic/internal/colors" "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 { type Backend struct {
name string name string
Type string `mapstructure:"type,omitempty"` Type string `yaml:"type,omitempty"`
Path string `mapstructure:"path,omitempty"` Path string `yaml:"path,omitempty"`
Key string `mapstructure:"key,omitempty"` Key string `yaml:"key,omitempty"`
Env map[string]string `mapstructure:"env,omitempty"` Env map[string]string `yaml:"env,omitempty"`
Rest BackendRest `yaml:"rest,omitempty"`
} }
func GetBackend(name string) (Backend, bool) { func GetBackend(name string) (Backend, bool) {
@ -29,7 +35,20 @@ func (b Backend) generateRepo() (string, error) {
switch b.Type { switch b.Type {
case "local": case "local":
return GetPathRelativeToConfig(b.Path) 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 return fmt.Sprintf("%s:%s", b.Type, b.Path), nil
default: default:
return "", fmt.Errorf("backend type \"%s\" is invalid", b.Type) return "", fmt.Errorf("backend type \"%s\" is invalid", b.Type)
@ -70,15 +89,10 @@ func (b Backend) validate() error {
c := GetConfig() c := GetConfig()
tmp := c.Backends[b.name] tmp := c.Backends[b.name]
tmp.Key = key tmp.Key = key
tmp.name = ""
c.Backends[b.name] = tmp c.Backends[b.name] = tmp
file := viper.ConfigFileUsed() if err := c.SaveConfig(); err != nil {
if err := CopyFile(file, file+".old"); err != nil {
return err 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() env, err := b.getEnv()
if err != nil { if err != nil {

@ -12,14 +12,14 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const VERSION = "1.0.3" const VERSION = "1.0.4"
var CI bool = false var CI bool = false
var VERBOSE bool = false var VERBOSE bool = false
type Config struct { type Config struct {
Locations map[string]Location `mapstructure:"locations"` Locations map[string]Location `yaml:"locations"`
Backends map[string]Backend `mapstructure:"backends"` Backends map[string]Backend `yaml:"backends"`
} }
var once sync.Once var once sync.Once
@ -197,3 +197,16 @@ func AddFlagsToCommand(cmd *cobra.Command, backend bool) {
cmd.PersistentFlags().StringSliceP("location", "l", []string{}, "Locations") 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()
}

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

Loading…
Cancel
Save