mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2024-08-28 23:27:18 +02:00
127 lines
2.7 KiB
Go
127 lines
2.7 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
const VERSION = "1.0.0"
|
|
|
|
type Config struct {
|
|
Locations []Location `mapstructure:"locations"`
|
|
Backends []Backend `mapstructure:"backends"`
|
|
}
|
|
|
|
var once sync.Once
|
|
var config *Config
|
|
|
|
func GetConfig() *Config {
|
|
if config == nil {
|
|
once.Do(func() {
|
|
config = &Config{}
|
|
if err := viper.UnmarshalExact(config); err != nil {
|
|
panic(err)
|
|
}
|
|
})
|
|
}
|
|
return config
|
|
}
|
|
|
|
func GetPathRelativeToConfig(p string) (string, error) {
|
|
if path.IsAbs(p) {
|
|
return p, nil
|
|
} else if strings.HasPrefix(p, "~") {
|
|
home, err := homedir.Dir()
|
|
return path.Join(home, strings.TrimPrefix(p, "~")), err
|
|
} else {
|
|
return path.Join(path.Dir(viper.ConfigFileUsed()), p), nil
|
|
}
|
|
}
|
|
|
|
func (c *Config) CheckConfig() error {
|
|
found := map[string]bool{}
|
|
for _, backend := range c.Backends {
|
|
if err := backend.validate(); err != nil {
|
|
return err
|
|
}
|
|
if _, ok := found[backend.Name]; ok {
|
|
return fmt.Errorf(`duplicate name for backends "%s"`, backend.Name)
|
|
} else {
|
|
found[backend.Name] = true
|
|
}
|
|
}
|
|
found = map[string]bool{}
|
|
for _, location := range c.Locations {
|
|
if err := location.validate(c); err != nil {
|
|
return err
|
|
}
|
|
if _, ok := found[location.Name]; ok {
|
|
return fmt.Errorf(`duplicate name for locations "%s"`, location.Name)
|
|
} else {
|
|
found[location.Name] = true
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
|
|
var list []string
|
|
if backends {
|
|
for _, b := range config.Backends {
|
|
list = append(list, b.Name)
|
|
}
|
|
} else {
|
|
for _, l := range config.Locations {
|
|
list = append(list, l.Name)
|
|
}
|
|
}
|
|
all, _ := cmd.Flags().GetBool("all")
|
|
if all {
|
|
return list, nil
|
|
} else {
|
|
var selected []string
|
|
if backends {
|
|
tmp, _ := cmd.Flags().GetStringSlice("backend")
|
|
selected = tmp
|
|
} else {
|
|
tmp, _ := cmd.Flags().GetStringSlice("location")
|
|
selected = tmp
|
|
}
|
|
for _, s := range selected {
|
|
found := false
|
|
for _, l := range list {
|
|
if l == s {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
if backends {
|
|
return nil, fmt.Errorf("invalid backend \"%s\"", s)
|
|
} else {
|
|
return nil, fmt.Errorf("invalid location \"%s\"", s)
|
|
}
|
|
}
|
|
}
|
|
if len(selected) == 0 {
|
|
return selected, fmt.Errorf("nothing selected, aborting")
|
|
}
|
|
return selected, nil
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|