unlock on error and named arrays for config

This commit is contained in:
2021-04-11 17:02:34 +02:00
parent 8a1fe41825
commit 6e25b90915
14 changed files with 138 additions and 49 deletions

View File

@@ -5,12 +5,23 @@ import (
)
type Backend struct {
Name string `mapstructure:"name"`
Type string `mapstructure:"type"`
Path string `mapstructure:"path"`
Key string `mapstructure:"key"`
Env map[string]string `mapstructure:"env"`
}
func GetBackend(name string) (Backend, bool) {
c := GetConfig()
for _, b := range c.Backends {
if b.Name == name {
return b, true
}
}
return Backend{}, false
}
func (b Backend) generateRepo() (string, error) {
switch b.Type {
case "local":

View File

@@ -15,8 +15,8 @@ import (
const VERSION = "1.0.0"
type Config struct {
Locations map[string]Location `mapstructure:"locations"`
Backends map[string]Backend `mapstructure:"backends"`
Locations []Location `mapstructure:"locations"`
Backends []Backend `mapstructure:"backends"`
}
var once sync.Once
@@ -65,12 +65,12 @@ func (c Config) CheckConfig() error {
func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
var list []string
if backends {
for key := range config.Backends {
list = append(list, key)
for _, b := range config.Backends {
list = append(list, b.Name)
}
} else {
for key := range config.Locations {
list = append(list, key)
for _, l := range config.Locations {
list = append(list, l.Name)
}
}
all, _ := cmd.Flags().GetBool("all")

View File

@@ -21,6 +21,7 @@ type Hooks struct {
type Options map[string]map[string][]string
type Location struct {
Name string `mapstructure:"name"`
From string `mapstructure:"from"`
To []string `mapstructure:"to"`
Hooks Hooks `mapstructure:"hooks"`
@@ -28,10 +29,20 @@ type Location struct {
Options Options `mapstructure:"options"`
}
func GetLocation(name string) (Location, bool) {
c := GetConfig()
for _, b := range c.Locations {
if b.Name == name {
return b, true
}
}
return Location{}, false
}
func (l Location) validate(c Config) error {
// Check if backends are all valid
for _, to := range l.To {
_, ok := c.Backends[to]
_, ok := GetBackend(to)
if !ok {
return fmt.Errorf("invalid backend `%s`", to)
}
@@ -60,10 +71,9 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error {
}
func (l Location) Backup() error {
c := GetConfig()
from := GetPathRelativeToConfig(l.From)
for _, to := range l.To {
backend := c.Backends[to]
backend, _ := GetBackend(to)
options := ExecuteOptions{
Command: "bash",
Envs: backend.getEnv(),
@@ -92,10 +102,9 @@ func (l Location) Backup() error {
}
func (l Location) Forget(prune bool, dry bool) error {
c := GetConfig()
from := GetPathRelativeToConfig(l.From)
for _, to := range l.To {
backend := c.Backends[to]
backend, _ := GetBackend(to)
options := ExecuteOptions{
Envs: backend.getEnv(),
Dir: from,
@@ -159,8 +168,7 @@ func (l Location) Restore(to, from string, force bool) error {
}
}
c := GetConfig()
backend := c.Backends[from]
backend, _ := GetBackend(from)
err = backend.Exec([]string{"restore", "--target", to, "--path", GetPathRelativeToConfig(l.From), "latest"})
if err != nil {
return err