From 4839c013b93eaa813ce7e20be61f5476d3b30d0d Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Tue, 12 Apr 2022 17:35:39 +0200 Subject: [PATCH] restore options --- cmd/restore.go | 20 +++++++++++++++++++- internal/location.go | 21 +++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/cmd/restore.go b/cmd/restore.go index 38df5e0..8faf4a7 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -30,7 +30,19 @@ var restoreCmd = &cobra.Command{ if len(args) > 0 { snapshot = args[0] } - err = l.Restore(target, from, force, snapshot) + + // Get optional flags + optional := []string{} + for _, flag := range []string{"include", "exclude", "iinclude", "iexclude"} { + values, err := cmd.Flags().GetStringSlice(flag) + if err == nil { + for _, value := range values { + optional = append(optional, "--"+flag, value) + } + } + } + + err = l.Restore(target, from, force, snapshot, optional) CheckErr(err) }, } @@ -42,4 +54,10 @@ func init() { restoreCmd.Flags().String("to", "", "Where to restore the data") restoreCmd.Flags().StringP("location", "l", "", "Location to be restored") restoreCmd.MarkFlagRequired("location") + + // Passed on flags + restoreCmd.Flags().StringSliceP("include", "i", []string{}, "Include a pattern") + restoreCmd.Flags().StringSliceP("exclude", "e", []string{}, "Exclude a pattern") + restoreCmd.Flags().StringSlice("iinclude", []string{}, "Include a pattern, case insensitive") + restoreCmd.Flags().StringSlice("iexclude", []string{}, "Exclude a pattern, case insensitive") } diff --git a/internal/location.go b/internal/location.go index 45af39c..fcf8d0e 100644 --- a/internal/location.go +++ b/internal/location.go @@ -297,18 +297,19 @@ func (l Location) hasBackend(backend string) bool { return false } -func (l Location) Restore(to, from string, force bool, snapshot string) error { +func buildRestoreCommand(l Location, to string, snapshot string, options []string) []string { + base := []string{"restore", "--target", to, "--tag", l.getLocationTags(), snapshot} + base = append(base, options...) + return base +} + +func (l Location) Restore(to, from string, force bool, snapshot string, options []string) error { if from == "" { from = l.To[0] } else if !l.hasBackend(from) { return fmt.Errorf("invalid backend: \"%s\"", from) } - to, err := filepath.Abs(to) - if err != nil { - return err - } - if snapshot == "" { snapshot = "latest" } @@ -323,6 +324,10 @@ func (l Location) Restore(to, from string, force bool, snapshot string) error { } switch t { case TypeLocal: + to, err = filepath.Abs(to) + if err != nil { + return err + } // Check if target is empty if !force { notEmptyError := fmt.Errorf("target %s is not empty", to) @@ -341,9 +346,9 @@ func (l Location) Restore(to, from string, force bool, snapshot string) error { } } } - err = backend.Exec([]string{"restore", "--target", to, "--tag", l.getLocationTags(), snapshot}) + err = backend.Exec(buildRestoreCommand(l, to, snapshot, options)) case TypeVolume: - _, err = backend.ExecDocker(l, []string{"restore", "--target", "/", "--tag", l.getLocationTags(), snapshot}) + _, err = backend.ExecDocker(l, buildRestoreCommand(l, "/", snapshot, options)) } if err != nil { return err