autorestic/cmd/exec.go

48 lines
1.0 KiB
Go
Raw Permalink Normal View History

2021-04-08 23:55:10 +00:00
package cmd
import (
"fmt"
2021-04-08 23:55:10 +00:00
"github.com/cupcakearmy/autorestic/internal"
2021-04-12 14:15:17 +00:00
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/lock"
2021-04-08 23:55:10 +00:00
"github.com/spf13/cobra"
)
var execCmd = &cobra.Command{
Use: "exec",
2021-04-11 11:08:50 +00:00
Short: "Execute arbitrary native restic commands for given backends",
2021-04-08 23:55:10 +00:00
Run: func(cmd *cobra.Command, args []string) {
2021-08-05 19:48:02 +00:00
internal.GetConfig()
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
2021-04-11 16:17:21 +00:00
selected, err := internal.GetAllOrSelected(cmd, true)
CheckErr(err)
var errors []error
2021-04-11 16:17:21 +00:00
for _, name := range selected {
2021-04-12 14:15:17 +00:00
colors.PrimaryPrint(" Executing on \"%s\" ", name)
2021-04-11 16:17:21 +00:00
backend, _ := internal.GetBackend(name)
err := backend.Exec(args)
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
for _, err := range errors {
colors.Error.Printf("%s\n\n", err)
}
CheckErr(fmt.Errorf("%d errors were found", len(errors)))
2021-04-11 11:04:11 +00:00
}
2021-04-08 23:55:10 +00:00
},
}
func init() {
rootCmd.AddCommand(execCmd)
2021-04-11 11:04:11 +00:00
internal.AddFlagsToCommand(execCmd, true)
2021-04-08 23:55:10 +00:00
}