dry run for forget cmd

This commit is contained in:
cupcakearmy 2021-04-11 14:22:46 +02:00
parent 05be58a3a7
commit 5d92b5bcc1
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
4 changed files with 12 additions and 2 deletions

View File

@ -17,6 +17,7 @@ package cmd
import (
"errors"
"fmt"
"github.com/cupcakearmy/autorestic/internal"
"github.com/spf13/cobra"
@ -33,6 +34,7 @@ var checkCmd = &cobra.Command{
config := internal.GetConfig()
err := config.CheckConfig()
cobra.CheckErr(err)
fmt.Println("Everyting is fine.")
},
}

View File

@ -33,9 +33,10 @@ var forgetCmd = &cobra.Command{
selected, err := internal.GetAllOrSelected(cmd, false)
cobra.CheckErr(err)
prune, _ := cmd.Flags().GetBool("prune")
dry, _ := cmd.Flags().GetBool("dry-run")
for _, name := range selected {
location := config.Locations[name]
err := location.Forget(prune)
err := location.Forget(prune, dry)
cobra.CheckErr(err)
}
}
@ -46,4 +47,5 @@ func init() {
rootCmd.AddCommand(forgetCmd)
internal.AddFlagsToCommand(forgetCmd, false)
forgetCmd.Flags().Bool("prune", false, "Also prune repository")
forgetCmd.Flags().Bool("dry-run", false, "Do not write changes, show what would be affected")
}

View File

@ -101,6 +101,9 @@ func GetAllOrSelected(cmd *cobra.Command, backends bool) ([]string, error) {
}
}
}
if len(selected) == 0 {
return selected, fmt.Errorf("nothing selected, aborting")
}
return selected, nil
}
}

View File

@ -87,7 +87,7 @@ func (l Location) Backup() error {
return nil
}
func (l Location) Forget(prune bool) error {
func (l Location) Forget(prune bool, dry bool) error {
c := GetConfig()
from := GetPathRelativeToConfig(l.From)
for _, to := range l.To {
@ -101,6 +101,9 @@ func (l Location) Forget(prune bool) error {
if prune {
cmd = append(cmd, "--prune")
}
if dry {
cmd = append(cmd, "--dry-run")
}
cmd = append(cmd, flags...)
out, err := ExecuteResticCommand(options, cmd...)
fmt.Println(out)