From 83905d2993fc41a61d5a9537df4a7d668d27dc95 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Thu, 28 Oct 2021 17:29:32 +0200 Subject: [PATCH] specific location --- cmd/backup.go | 10 ++++++++-- internal/config.go | 18 ++++++++---------- internal/location.go | 20 +++++++++++++++++--- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/cmd/backup.go b/cmd/backup.go index 53c875d..e33af34 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "strings" "github.com/cupcakearmy/autorestic/internal" "github.com/cupcakearmy/autorestic/internal/colors" @@ -22,8 +23,13 @@ var backupCmd = &cobra.Command{ CheckErr(err) errors := 0 for _, name := range selected { - location, _ := internal.GetLocation(name) - errs := location.Backup(false) + var splitted = strings.Split(name, "@") + var specificBackend = "" + if len(splitted) > 1 { + specificBackend = splitted[1] + } + location, _ := internal.GetLocation(splitted[0]) + errs := location.Backup(false, specificBackend) for err := range errs { colors.Error.Println(err) errors++ diff --git a/internal/config.go b/internal/config.go index f63b351..c3c1054 100644 --- a/internal/config.go +++ b/internal/config.go @@ -185,20 +185,18 @@ func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) { selected, _ = cmd.Flags().GetStringSlice("location") } for _, s := range selected { - found := false + var splitted = strings.Split(s, "@") for _, l := range list { - if l == s { - found = true - break + if l == splitted[0] { + goto found } } - if !found { - if backends { - return nil, fmt.Errorf("invalid backend \"%s\"", s) - } else { - return nil, fmt.Errorf("invalid location \"%s\"", s) - } + if backends { + return nil, fmt.Errorf("invalid backend \"%s\"", s) + } else { + return nil, fmt.Errorf("invalid location \"%s\"", s) } + found: } if len(selected) == 0 { diff --git a/internal/location.go b/internal/location.go index 79f9bce..343b582 100644 --- a/internal/location.go +++ b/internal/location.go @@ -125,8 +125,9 @@ func (l Location) getPath() (string, error) { return "", fmt.Errorf("could not get path for location \"%s\"", l.name) } -func (l Location) Backup(cron bool) []error { +func (l Location) Backup(cron bool, specificBackend string) []error { var errors []error + var backends []string colors.PrimaryPrint(" Backing up location \"%s\" ", l.name) t := l.getType() options := ExecuteOptions{ @@ -155,7 +156,20 @@ func (l Location) Backup(cron bool) []error { } // Backup - for i, to := range l.To { + if specificBackend == "" { + backends = l.To + } else { + for _, b := range l.To { + if b == specificBackend { + backends = []string{b} + goto backup + } + } + errors = append(errors, fmt.Errorf("backup location \"%s\" has no backend \"%s\"", l.name, specificBackend)) + return errors + } +backup: + for i, to := range backends { backend, _ := GetBackend(to) colors.Secondary.Printf("Backend: %s\n", backend.name) env, err := backend.getEnv() @@ -338,7 +352,7 @@ func (l Location) RunCron() error { now := time.Now() if now.After(next) { lock.SetCron(l.name, now.Unix()) - l.Backup(true) + l.Backup(true, "") } else { if !CRON_LEAN { colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.name)