From 188560395d3269ac64c7da1d44dfd6d9d41074a9 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Fri, 23 Apr 2021 13:11:15 +0200 Subject: [PATCH] exclude items from config when empty and rest options --- internal/backend.go | 38 ++++++++++++++++++++++++++------------ internal/config.go | 19 ++++++++++++++++--- internal/location.go | 16 ++++++++-------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 8fb1b7c..50199e8 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -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 { diff --git a/internal/config.go b/internal/config.go index 4f0fcdf..d528a43 100644 --- a/internal/config.go +++ b/internal/config.go @@ -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() +} diff --git a/internal/location.go b/internal/location.go index f3fc35c..61510d5 100644 --- a/internal/location.go +++ b/internal/location.go @@ -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) {