restore options

This commit is contained in:
cupcakearmy 2022-04-12 17:35:39 +02:00
parent 8a713e497d
commit 4839c013b9
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
2 changed files with 32 additions and 9 deletions

View File

@ -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")
}

View File

@ -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