2021-04-08 23:55:10 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-05 19:48:02 +00:00
|
|
|
"os"
|
2021-04-08 23:55:10 +00:00
|
|
|
"path"
|
2021-08-05 19:48:02 +00:00
|
|
|
"path/filepath"
|
2021-04-08 23:55:10 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-04-12 14:15:17 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
2022-02-16 20:42:54 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
2021-08-05 19:48:02 +00:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/lock"
|
2021-10-25 15:36:06 +00:00
|
|
|
"github.com/joho/godotenv"
|
2021-04-08 23:55:10 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2022-09-13 13:16:21 +00:00
|
|
|
const VERSION = "1.7.3"
|
2021-04-12 14:15:17 +00:00
|
|
|
|
2021-10-28 16:10:14 +00:00
|
|
|
type OptionMap map[string][]interface{}
|
|
|
|
type Options map[string]OptionMap
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
type Config struct {
|
2022-04-14 09:51:32 +00:00
|
|
|
Version string `mapstructure:"version"`
|
|
|
|
Extras interface{} `mapstructure:"extras"`
|
|
|
|
Locations map[string]Location `mapstructure:"locations"`
|
|
|
|
Backends map[string]Backend `mapstructure:"backends"`
|
|
|
|
Global Options `mapstructure:"global"`
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
var config *Config
|
|
|
|
|
2021-11-06 21:00:44 +00:00
|
|
|
func exitConfig(err error, msg string) {
|
|
|
|
if err != nil {
|
|
|
|
colors.Error.Println(err)
|
|
|
|
}
|
|
|
|
if msg != "" {
|
|
|
|
colors.Error.Println(msg)
|
|
|
|
}
|
|
|
|
lock.Unlock()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
func GetConfig() *Config {
|
2021-11-06 21:00:44 +00:00
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
if config == nil {
|
|
|
|
once.Do(func() {
|
2021-04-12 14:15:17 +00:00
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2022-02-16 20:42:54 +00:00
|
|
|
absConfig, _ := filepath.Abs(viper.ConfigFileUsed())
|
|
|
|
if !flags.CRON_LEAN {
|
2021-10-25 15:36:06 +00:00
|
|
|
colors.Faint.Println("Using config: \t", absConfig)
|
2022-02-16 20:42:54 +00:00
|
|
|
}
|
|
|
|
// Load env file
|
|
|
|
envFile := filepath.Join(filepath.Dir(absConfig), ".autorestic.env")
|
|
|
|
err = godotenv.Load(envFile)
|
2022-04-14 09:51:32 +00:00
|
|
|
if err == nil && !flags.CRON_LEAN {
|
2022-02-16 20:42:54 +00:00
|
|
|
colors.Faint.Println("Using env:\t", envFile)
|
2021-04-28 08:54:07 +00:00
|
|
|
}
|
2021-04-12 14:15:17 +00:00
|
|
|
} else {
|
2022-03-18 12:06:19 +00:00
|
|
|
text := err.Error()
|
|
|
|
if strings.Contains(text, "no such file or directory") {
|
|
|
|
cfgFileName := ".autorestic"
|
|
|
|
colors.Error.Println(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"cannot find configuration file '%s.yml' or '%s.yaml'.",
|
|
|
|
cfgFileName, cfgFileName))
|
|
|
|
} else {
|
|
|
|
colors.Error.Println("could not load config file\n" + text)
|
|
|
|
}
|
2022-02-16 20:22:42 +00:00
|
|
|
os.Exit(1)
|
2021-04-12 14:15:17 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 21:00:44 +00:00
|
|
|
var versionConfig interface{}
|
|
|
|
viper.UnmarshalKey("version", &versionConfig)
|
|
|
|
if versionConfig == nil {
|
|
|
|
exitConfig(nil, "no version specified in config file. please see docs on how to migrate")
|
|
|
|
}
|
|
|
|
version, ok := versionConfig.(int)
|
|
|
|
if !ok {
|
|
|
|
exitConfig(nil, "version specified in config file is not an int")
|
|
|
|
} else {
|
|
|
|
// Check for version
|
|
|
|
if version != 2 {
|
2021-11-20 16:09:42 +00:00
|
|
|
exitConfig(nil, "unsupported config version number. please check the docs for migration\nhttps://autorestic.vercel.app/migration/")
|
2021-11-06 21:00:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
config = &Config{}
|
|
|
|
if err := viper.UnmarshalExact(config); err != nil {
|
2021-11-06 21:00:44 +00:00
|
|
|
exitConfig(err, "Could not parse config file!")
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-04-11 16:17:21 +00:00
|
|
|
func GetPathRelativeToConfig(p string) (string, error) {
|
2021-04-08 23:55:10 +00:00
|
|
|
if path.IsAbs(p) {
|
2021-04-11 16:17:21 +00:00
|
|
|
return p, nil
|
2021-04-08 23:55:10 +00:00
|
|
|
} else if strings.HasPrefix(p, "~") {
|
|
|
|
home, err := homedir.Dir()
|
2021-04-11 16:17:21 +00:00
|
|
|
return path.Join(home, strings.TrimPrefix(p, "~")), err
|
2021-04-08 23:55:10 +00:00
|
|
|
} else {
|
2021-04-11 16:17:21 +00:00
|
|
|
return path.Join(path.Dir(viper.ConfigFileUsed()), p), nil
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 20:37:15 +00:00
|
|
|
func (c *Config) Describe() {
|
2021-04-17 10:02:00 +00:00
|
|
|
// Locations
|
2021-04-16 20:37:15 +00:00
|
|
|
for name, l := range c.Locations {
|
|
|
|
var tmp string
|
|
|
|
colors.PrimaryPrint(`Location: "%s"`, name)
|
|
|
|
|
2021-10-31 21:32:30 +00:00
|
|
|
tmp = ""
|
|
|
|
for _, path := range l.From {
|
|
|
|
tmp += fmt.Sprintf("\t%s %s\n", colors.Success.Sprint("←"), path)
|
|
|
|
}
|
|
|
|
colors.PrintDescription("From", tmp)
|
2021-04-16 20:37:15 +00:00
|
|
|
|
|
|
|
tmp = ""
|
|
|
|
for _, to := range l.To {
|
2021-04-17 10:02:00 +00:00
|
|
|
tmp += fmt.Sprintf("\t%s %s\n", colors.Success.Sprint("→"), to)
|
2021-04-16 20:37:15 +00:00
|
|
|
}
|
2021-04-17 10:02:00 +00:00
|
|
|
colors.PrintDescription("To", tmp)
|
2021-04-16 20:37:15 +00:00
|
|
|
|
|
|
|
if l.Cron != "" {
|
2021-04-17 10:02:00 +00:00
|
|
|
colors.PrintDescription("Cron", l.Cron)
|
2021-04-16 20:37:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 13:55:32 +00:00
|
|
|
tmp = ""
|
|
|
|
hooks := map[string][]string{
|
|
|
|
"Before": l.Hooks.Before,
|
|
|
|
"After": l.Hooks.After,
|
|
|
|
"Failure": l.Hooks.Failure,
|
|
|
|
"Success": l.Hooks.Success,
|
|
|
|
}
|
|
|
|
for hook, commands := range hooks {
|
|
|
|
if len(commands) > 0 {
|
|
|
|
tmp += "\n\t" + hook
|
|
|
|
for _, cmd := range commands {
|
2021-04-17 10:02:00 +00:00
|
|
|
tmp += colors.Faint.Sprintf("\n\t ▶ %s", cmd)
|
2021-04-16 20:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 13:55:32 +00:00
|
|
|
}
|
|
|
|
if tmp != "" {
|
2021-04-17 10:02:00 +00:00
|
|
|
colors.PrintDescription("Hooks", tmp)
|
2021-04-16 20:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(l.Options) > 0 {
|
|
|
|
tmp = ""
|
|
|
|
for t, options := range l.Options {
|
2021-04-17 10:02:00 +00:00
|
|
|
tmp += "\n\t" + t
|
2021-04-16 20:37:15 +00:00
|
|
|
for option, values := range options {
|
|
|
|
for _, value := range values {
|
2021-04-17 10:02:00 +00:00
|
|
|
tmp += colors.Faint.Sprintf("\n\t ✧ --%s=%s", option, value)
|
2021-04-16 20:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-17 10:02:00 +00:00
|
|
|
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 {
|
2021-04-17 11:04:40 +00:00
|
|
|
tmp += fmt.Sprintf("\n\t%s %s %s", colors.Success.Sprint("✧"), strings.ToUpper(option), colors.Faint.Sprint(value))
|
2021-04-17 10:02:00 +00:00
|
|
|
}
|
|
|
|
colors.PrintDescription("Env", tmp)
|
2021-04-16 20:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 20:02:25 +00:00
|
|
|
func CheckConfig() error {
|
|
|
|
c := GetConfig()
|
2021-04-12 14:15:17 +00:00
|
|
|
if c == nil {
|
|
|
|
return fmt.Errorf("config could not be loaded/found")
|
|
|
|
}
|
2021-04-12 08:55:57 +00:00
|
|
|
if !CheckIfResticIsCallable() {
|
2022-04-26 22:48:52 +00:00
|
|
|
return fmt.Errorf(`%s was not found. Install either with "autorestic install" or manually`, flags.RESTIC_BIN)
|
2021-04-12 08:55:57 +00:00
|
|
|
}
|
2021-04-16 20:02:25 +00:00
|
|
|
for name, backend := range c.Backends {
|
|
|
|
backend.name = name
|
2021-04-08 23:55:10 +00:00
|
|
|
if err := backend.validate(); err != nil {
|
2021-04-11 22:02:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
2021-04-16 20:02:25 +00:00
|
|
|
for name, location := range c.Locations {
|
|
|
|
location.name = name
|
2021-07-11 11:51:04 +00:00
|
|
|
if err := location.validate(); err != nil {
|
2021-04-11 22:02:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-11 11:04:11 +00:00
|
|
|
func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
|
2021-04-08 23:55:10 +00:00
|
|
|
var list []string
|
|
|
|
if backends {
|
2021-04-16 20:02:25 +00:00
|
|
|
for name := range config.Backends {
|
|
|
|
list = append(list, name)
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-16 20:02:25 +00:00
|
|
|
for name := range config.Locations {
|
|
|
|
list = append(list, name)
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-16 20:02:25 +00:00
|
|
|
|
2021-04-08 23:55:10 +00:00
|
|
|
all, _ := cmd.Flags().GetBool("all")
|
|
|
|
if all {
|
2021-04-11 11:04:11 +00:00
|
|
|
return list, nil
|
2021-04-16 20:02:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var selected []string
|
|
|
|
if backends {
|
|
|
|
selected, _ = cmd.Flags().GetStringSlice("backend")
|
2021-04-08 23:55:10 +00:00
|
|
|
} else {
|
2021-04-16 20:02:25 +00:00
|
|
|
selected, _ = cmd.Flags().GetStringSlice("location")
|
|
|
|
}
|
|
|
|
for _, s := range selected {
|
2021-10-28 15:29:32 +00:00
|
|
|
var splitted = strings.Split(s, "@")
|
2021-04-16 20:02:25 +00:00
|
|
|
for _, l := range list {
|
2021-10-28 15:29:32 +00:00
|
|
|
if l == splitted[0] {
|
|
|
|
goto found
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 15:29:32 +00:00
|
|
|
if backends {
|
|
|
|
return nil, fmt.Errorf("invalid backend \"%s\"", s)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("invalid location \"%s\"", s)
|
2021-04-11 12:22:46 +00:00
|
|
|
}
|
2021-10-28 15:29:32 +00:00
|
|
|
found:
|
2021-04-11 11:04:11 +00:00
|
|
|
}
|
2021-04-16 20:02:25 +00:00
|
|
|
|
|
|
|
if len(selected) == 0 {
|
|
|
|
return selected, fmt.Errorf("nothing selected, aborting")
|
|
|
|
}
|
|
|
|
return selected, nil
|
2021-04-11 11:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddFlagsToCommand(cmd *cobra.Command, backend bool) {
|
2021-04-28 08:54:07 +00:00
|
|
|
var usage string
|
|
|
|
if backend {
|
|
|
|
usage = "all backends"
|
|
|
|
} else {
|
|
|
|
usage = "all locations"
|
|
|
|
}
|
|
|
|
cmd.PersistentFlags().BoolP("all", "a", false, usage)
|
2021-04-11 11:04:11 +00:00
|
|
|
if backend {
|
2021-04-28 08:54:07 +00:00
|
|
|
cmd.PersistentFlags().StringSliceP("backend", "b", []string{}, "select backends")
|
2021-04-11 11:04:11 +00:00
|
|
|
} else {
|
2021-04-28 08:54:07 +00:00
|
|
|
cmd.PersistentFlags().StringSliceP("location", "l", []string{}, "select locations")
|
2021-04-08 23:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 11:11:15 +00:00
|
|
|
|
|
|
|
func (c *Config) SaveConfig() error {
|
|
|
|
file := viper.ConfigFileUsed()
|
|
|
|
if err := CopyFile(file, file+".old"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-09 19:35:29 +00:00
|
|
|
colors.Secondary.Println("Saved a backup copy of your file next to the original.")
|
2021-04-23 11:11:15 +00:00
|
|
|
|
|
|
|
viper.Set("backends", c.Backends)
|
|
|
|
viper.Set("locations", c.Locations)
|
|
|
|
|
|
|
|
return viper.WriteConfig()
|
|
|
|
}
|
2021-05-17 20:34:14 +00:00
|
|
|
|
2021-10-28 16:10:14 +00:00
|
|
|
func optionToString(option string) string {
|
|
|
|
if !strings.HasPrefix(option, "-") {
|
|
|
|
return "--" + option
|
|
|
|
}
|
|
|
|
return option
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendOptionsToSlice(str *[]string, options OptionMap) {
|
|
|
|
for key, values := range options {
|
2021-05-17 20:34:14 +00:00
|
|
|
for _, value := range values {
|
2021-10-25 16:29:59 +00:00
|
|
|
// Bool
|
|
|
|
asBool, ok := value.(bool)
|
|
|
|
if ok && asBool {
|
2021-10-28 16:10:14 +00:00
|
|
|
*str = append(*str, optionToString(key))
|
2021-10-25 16:29:59 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-10-31 21:45:03 +00:00
|
|
|
*str = append(*str, optionToString(key), fmt.Sprint(value))
|
2021-05-17 20:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 16:10:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 22:48:52 +00:00
|
|
|
func getOptions(options Options, keys []string) []string {
|
2021-10-28 16:10:14 +00:00
|
|
|
var selected []string
|
|
|
|
for _, key := range keys {
|
|
|
|
appendOptionsToSlice(&selected, options[key])
|
|
|
|
}
|
2021-05-17 20:34:14 +00:00
|
|
|
return selected
|
|
|
|
}
|
2021-10-31 22:07:12 +00:00
|
|
|
|
2022-09-13 13:15:02 +00:00
|
|
|
func combineBackendOptions(key string, b Backend) []string {
|
|
|
|
// Priority: backend > global
|
|
|
|
var options []string
|
|
|
|
gFlags := getOptions(GetConfig().Global, []string{key})
|
|
|
|
bFlags := getOptions(b.Options, []string{"all", key})
|
|
|
|
options = append(options, gFlags...)
|
|
|
|
options = append(options, bFlags...)
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
func combineAllOptions(key string, l Location, b Backend) []string {
|
2021-10-31 22:07:12 +00:00
|
|
|
// Priority: location > backend > global
|
|
|
|
var options []string
|
2022-04-26 22:48:52 +00:00
|
|
|
gFlags := getOptions(GetConfig().Global, []string{key})
|
|
|
|
bFlags := getOptions(b.Options, []string{"all", key})
|
|
|
|
lFlags := getOptions(l.Options, []string{"all", key})
|
2021-10-31 22:07:12 +00:00
|
|
|
options = append(options, gFlags...)
|
|
|
|
options = append(options, bFlags...)
|
|
|
|
options = append(options, lFlags...)
|
|
|
|
return options
|
|
|
|
}
|