restore options

This commit is contained in:
2022-04-12 17:35:39 +02:00
parent 8a713e497d
commit 4839c013b9
2 changed files with 32 additions and 9 deletions

View File

@@ -30,7 +30,19 @@ var restoreCmd = &cobra.Command{
if len(args) > 0 { if len(args) > 0 {
snapshot = 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) CheckErr(err)
}, },
} }
@@ -42,4 +54,10 @@ func init() {
restoreCmd.Flags().String("to", "", "Where to restore the data") restoreCmd.Flags().String("to", "", "Where to restore the data")
restoreCmd.Flags().StringP("location", "l", "", "Location to be restored") restoreCmd.Flags().StringP("location", "l", "", "Location to be restored")
restoreCmd.MarkFlagRequired("location") 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")
} }

View File

@@ -297,18 +297,19 @@ func (l Location) hasBackend(backend string) bool {
return false 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 == "" { if from == "" {
from = l.To[0] from = l.To[0]
} else if !l.hasBackend(from) { } else if !l.hasBackend(from) {
return fmt.Errorf("invalid backend: \"%s\"", from) return fmt.Errorf("invalid backend: \"%s\"", from)
} }
to, err := filepath.Abs(to)
if err != nil {
return err
}
if snapshot == "" { if snapshot == "" {
snapshot = "latest" snapshot = "latest"
} }
@@ -323,6 +324,10 @@ func (l Location) Restore(to, from string, force bool, snapshot string) error {
} }
switch t { switch t {
case TypeLocal: case TypeLocal:
to, err = filepath.Abs(to)
if err != nil {
return err
}
// Check if target is empty // Check if target is empty
if !force { if !force {
notEmptyError := fmt.Errorf("target %s is not empty", to) 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: case TypeVolume:
_, err = backend.ExecDocker(l, []string{"restore", "--target", "/", "--tag", l.getLocationTags(), snapshot}) _, err = backend.ExecDocker(l, buildRestoreCommand(l, "/", snapshot, options))
} }
if err != nil { if err != nil {
return err return err