From 19e75c1dadf91e79d8c8cc3262e059f7e9ff604b Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sun, 11 Apr 2021 18:17:21 +0200 Subject: [PATCH] fixes --- cmd/exec.go | 20 ++++++-------- cmd/forget.go | 21 ++++++-------- internal/backend.go | 23 ++++++++++------ internal/config.go | 19 ++++++------- internal/location.go | 65 ++++++++++++++++++++++++++------------------ 5 files changed, 79 insertions(+), 69 deletions(-) diff --git a/cmd/exec.go b/cmd/exec.go index c01dcbc..17a57fc 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -33,17 +33,15 @@ var execCmd = &cobra.Command{ defer lock.Unlock() config := internal.GetConfig() - if err := config.CheckConfig(); err != nil { - panic(err) - } - { - selected, err := internal.GetAllOrSelected(cmd, true) - CheckErr(err) - for _, name := range selected { - fmt.Println(name) - backend, _ := internal.GetBackend(name) - backend.Exec(args) - } + err = config.CheckConfig() + CheckErr(err) + + selected, err := internal.GetAllOrSelected(cmd, true) + CheckErr(err) + for _, name := range selected { + fmt.Println(name) + backend, _ := internal.GetBackend(name) + backend.Exec(args) } }, } diff --git a/cmd/forget.go b/cmd/forget.go index 43bb8b7..f8a822a 100644 --- a/cmd/forget.go +++ b/cmd/forget.go @@ -31,19 +31,16 @@ var forgetCmd = &cobra.Command{ defer lock.Unlock() config := internal.GetConfig() - if err := config.CheckConfig(); err != nil { - panic(err) - } - { - selected, err := internal.GetAllOrSelected(cmd, false) + CheckErr(config.CheckConfig()) + + selected, err := internal.GetAllOrSelected(cmd, false) + CheckErr(err) + prune, _ := cmd.Flags().GetBool("prune") + dry, _ := cmd.Flags().GetBool("dry-run") + for _, name := range selected { + location, _ := internal.GetLocation(name) + err := location.Forget(prune, dry) CheckErr(err) - prune, _ := cmd.Flags().GetBool("prune") - dry, _ := cmd.Flags().GetBool("dry-run") - for _, name := range selected { - location, _ := internal.GetLocation(name) - err := location.Forget(prune, dry) - CheckErr(err) - } } }, } diff --git a/internal/backend.go b/internal/backend.go index 4e1b38c..962d8ce 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -25,7 +25,7 @@ func GetBackend(name string) (Backend, bool) { func (b Backend) generateRepo() (string, error) { switch b.Type { case "local": - return GetPathRelativeToConfig(b.Path), nil + return GetPathRelativeToConfig(b.Path) case "b2", "azure", "gs", "s3", "sftp", "rest": return fmt.Sprintf("%s:%s", b.Type, b.Path), nil default: @@ -33,21 +33,22 @@ func (b Backend) generateRepo() (string, error) { } } -func (b Backend) getEnv() map[string]string { +func (b Backend) getEnv() (map[string]string, error) { env := make(map[string]string) env["RESTIC_PASSWORD"] = b.Key repo, err := b.generateRepo() - if err != nil { - panic(err) - } env["RESTIC_REPOSITORY"] = repo - return env + return env, err } func (b Backend) validate() error { - options := ExecuteOptions{Envs: b.getEnv()} + env, err := b.getEnv() + if err != nil { + return err + } + options := ExecuteOptions{Envs: env} // Check if already initialized - _, err := ExecuteResticCommand(options, "snapshots") + _, err = ExecuteResticCommand(options, "snapshots") if err == nil { return nil } else { @@ -59,7 +60,11 @@ func (b Backend) validate() error { } func (b Backend) Exec(args []string) error { - options := ExecuteOptions{Envs: b.getEnv()} + env, err := b.getEnv() + if err != nil { + return err + } + options := ExecuteOptions{Envs: env} out, err := ExecuteResticCommand(options, args...) fmt.Println(out) return err diff --git a/internal/config.go b/internal/config.go index db280f7..99cd23d 100644 --- a/internal/config.go +++ b/internal/config.go @@ -34,29 +34,26 @@ func GetConfig() *Config { return config } -func GetPathRelativeToConfig(p string) string { +func GetPathRelativeToConfig(p string) (string, error) { if path.IsAbs(p) { - return p + return p, nil } else if strings.HasPrefix(p, "~") { home, err := homedir.Dir() - if err != nil { - panic(err) - } - return path.Join(home, strings.TrimPrefix(p, "~")) + return path.Join(home, strings.TrimPrefix(p, "~")), err } else { - return path.Join(path.Dir(viper.ConfigFileUsed()), p) + return path.Join(path.Dir(viper.ConfigFileUsed()), p), nil } } func (c Config) CheckConfig() error { - for name, backend := range c.Backends { + for _, backend := range c.Backends { if err := backend.validate(); err != nil { - return fmt.Errorf("backend \"%s\": %s", name, err) + return fmt.Errorf("backend \"%s\": %s", backend.Name, err) } } - for name, location := range c.Locations { + for _, location := range c.Locations { if err := location.validate(c); err != nil { - return fmt.Errorf("location \"%s\": %s", name, err) + return fmt.Errorf("location \"%s\": %s", location.Name, err) } } return nil diff --git a/internal/location.go b/internal/location.go index c630eb0..c0c632e 100644 --- a/internal/location.go +++ b/internal/location.go @@ -70,47 +70,56 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error { return nil } -func (l Location) Backup() error { - from := GetPathRelativeToConfig(l.From) +func (l Location) forEachBackend(fn func(ExecuteOptions) error) error { + from, err := GetPathRelativeToConfig(l.From) + if err != nil { + return err + } for _, to := range l.To { backend, _ := GetBackend(to) + env, err := backend.getEnv() + if err != nil { + return nil + } options := ExecuteOptions{ Command: "bash", - Envs: backend.getEnv(), + Envs: env, Dir: from, } + if err := fn(options); err != nil { + return err + } + } + return nil +} +func (l Location) Backup() error { + return l.forEachBackend(func(options ExecuteOptions) error { if err := ExecuteHooks(l.Hooks.Before, options); err != nil { return nil } - { - flags := l.getOptions("backup") - cmd := []string{"backup"} - cmd = append(cmd, flags...) - cmd = append(cmd, ".") - out, err := ExecuteResticCommand(options, cmd...) - fmt.Println(out) - if err != nil { - return err - } + + flags := l.getOptions("backup") + cmd := []string{"backup"} + cmd = append(cmd, flags...) + cmd = append(cmd, ".") + out, err := ExecuteResticCommand(options, cmd...) + fmt.Println(out) + if err != nil { + return err } + if err := ExecuteHooks(l.Hooks.After, options); err != nil { return nil } - } - return nil + return nil + }) } func (l Location) Forget(prune bool, dry bool) error { - from := GetPathRelativeToConfig(l.From) - for _, to := range l.To { - backend, _ := GetBackend(to) - options := ExecuteOptions{ - Envs: backend.getEnv(), - Dir: from, - } + return l.forEachBackend(func(options ExecuteOptions) error { flags := l.getOptions("forget") - cmd := []string{"forget", "--path", from} + cmd := []string{"forget", "--path", options.Dir} if prune { cmd = append(cmd, "--prune") } @@ -123,8 +132,8 @@ func (l Location) Forget(prune bool, dry bool) error { if err != nil { return err } - } - return nil + return nil + }) } func (l Location) hasBackend(backend string) bool { @@ -169,7 +178,11 @@ func (l Location) Restore(to, from string, force bool) error { } backend, _ := GetBackend(from) - err = backend.Exec([]string{"restore", "--target", to, "--path", GetPathRelativeToConfig(l.From), "latest"}) + resolved, err := GetPathRelativeToConfig(l.From) + if err != nil { + return nil + } + err = backend.Exec([]string{"restore", "--target", to, "--path", resolved, "latest"}) if err != nil { return err }