You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
autorestic/internal/config.go

213 lines
4.6 KiB

4 years ago
package internal
import (
"fmt"
"path"
"strings"
"sync"
"github.com/cupcakearmy/autorestic/internal/colors"
4 years ago
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const VERSION = "1.0.6"
4 years ago
var CI bool = false
var VERBOSE bool = false
4 years ago
type Config struct {
Locations map[string]Location `yaml:"locations"`
Backends map[string]Backend `yaml:"backends"`
4 years ago
}
var once sync.Once
var config *Config
func GetConfig() *Config {
if config == nil {
once.Do(func() {
if err := viper.ReadInConfig(); err == nil {
colors.Faint.Println("Using config file:", viper.ConfigFileUsed())
} else {
return
}
4 years ago
config = &Config{}
if err := viper.UnmarshalExact(config); err != nil {
panic(err)
4 years ago
}
})
}
return config
}
4 years ago
func GetPathRelativeToConfig(p string) (string, error) {
4 years ago
if path.IsAbs(p) {
4 years ago
return p, nil
4 years ago
} else if strings.HasPrefix(p, "~") {
home, err := homedir.Dir()
4 years ago
return path.Join(home, strings.TrimPrefix(p, "~")), err
4 years ago
} else {
4 years ago
return path.Join(path.Dir(viper.ConfigFileUsed()), p), nil
4 years ago
}
}
4 years ago
func (c *Config) Describe() {
// Locations
4 years ago
for name, l := range c.Locations {
var tmp string
colors.PrimaryPrint(`Location: "%s"`, name)
colors.PrintDescription("From", l.From)
4 years ago
tmp = ""
for _, to := range l.To {
tmp += fmt.Sprintf("\t%s %s\n", colors.Success.Sprint("→"), to)
4 years ago
}
colors.PrintDescription("To", tmp)
4 years ago
if l.Cron != "" {
colors.PrintDescription("Cron", l.Cron)
4 years ago
}
after, before := len(l.Hooks.After), len(l.Hooks.Before)
if after+before > 0 {
tmp = ""
if before > 0 {
tmp += "\tBefore"
4 years ago
for _, cmd := range l.Hooks.Before {
tmp += colors.Faint.Sprintf("\n\t ▶ %s", cmd)
4 years ago
}
}
if after > 0 {
tmp += "\n\tAfter"
4 years ago
for _, cmd := range l.Hooks.After {
tmp += colors.Faint.Sprintf("\n\t ▶ %s", cmd)
4 years ago
}
}
colors.PrintDescription("Hooks", tmp)
4 years ago
}
if len(l.Options) > 0 {
tmp = ""
for t, options := range l.Options {
tmp += "\n\t" + t
4 years ago
for option, values := range options {
for _, value := range values {
tmp += colors.Faint.Sprintf("\n\t ✧ --%s=%s", option, value)
4 years ago
}
}
}
colors.PrintDescription("Options", tmp)
}
}
// Backends
for name, b := range c.Backends {
colors.PrimaryPrint("Backend: \"%s\"", name)
colors.PrintDescription("Type", b.Type)
colors.PrintDescription("Path", b.Path)
if len(b.Env) > 0 {
tmp := ""
for option, value := range b.Env {
tmp += fmt.Sprintf("\n\t%s %s %s", colors.Success.Sprint("✧"), strings.ToUpper(option), colors.Faint.Sprint(value))
}
colors.PrintDescription("Env", tmp)
4 years ago
}
}
}
func CheckConfig() error {
c := GetConfig()
if c == nil {
return fmt.Errorf("config could not be loaded/found")
}
4 years ago
if !CheckIfResticIsCallable() {
return fmt.Errorf(`restic was not found. Install either with "autorestic install" or manually`)
}
for name, backend := range c.Backends {
backend.name = name
4 years ago
if err := backend.validate(); err != nil {
return err
}
4 years ago
}
for name, location := range c.Locations {
location.name = name
4 years ago
if err := location.validate(c); err != nil {
return err
}
4 years ago
}
return nil
}
4 years ago
func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
4 years ago
var list []string
if backends {
for name := range config.Backends {
list = append(list, name)
4 years ago
}
} else {
for name := range config.Locations {
list = append(list, name)
4 years ago
}
}
4 years ago
all, _ := cmd.Flags().GetBool("all")
if all {
4 years ago
return list, nil
}
var selected []string
if backends {
selected, _ = cmd.Flags().GetStringSlice("backend")
4 years ago
} else {
selected, _ = cmd.Flags().GetStringSlice("location")
}
for _, s := range selected {
found := false
for _, l := range list {
if l == s {
found = true
break
4 years ago
}
}
if !found {
if backends {
return nil, fmt.Errorf("invalid backend \"%s\"", s)
} else {
return nil, fmt.Errorf("invalid location \"%s\"", s)
}
}
4 years ago
}
if len(selected) == 0 {
return selected, fmt.Errorf("nothing selected, aborting")
}
return selected, nil
4 years ago
}
func AddFlagsToCommand(cmd *cobra.Command, backend bool) {
cmd.PersistentFlags().BoolP("all", "a", false, "Backup all locations")
if backend {
cmd.PersistentFlags().StringSliceP("backend", "b", []string{}, "backends")
} else {
cmd.PersistentFlags().StringSliceP("location", "l", []string{}, "Locations")
4 years ago
}
}
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()
}