unlock on error and named arrays for config

This commit is contained in:
2021-04-11 17:02:34 +02:00
parent 8a1fe41825
commit 6e25b90915
14 changed files with 138 additions and 49 deletions

View File

@@ -28,24 +28,20 @@ var backupCmd = &cobra.Command{
Use: "backup",
Short: "Create backups for given locations",
Run: func(cmd *cobra.Command, args []string) {
config := internal.GetConfig()
{
err := config.CheckConfig()
cobra.CheckErr(err)
}
{
err := lock.Lock()
cobra.CheckErr(err)
}
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
{
selected, err := internal.GetAllOrSelected(cmd, false)
cobra.CheckErr(err)
for _, name := range selected {
location := config.Locations[name]
fmt.Printf("Backing up: `%s`", name)
location.Backup()
}
config := internal.GetConfig()
err = config.CheckConfig()
CheckErr(err)
selected, err := internal.GetAllOrSelected(cmd, false)
CheckErr(err)
for _, name := range selected {
location, _ := internal.GetLocation(name)
fmt.Printf("Backing up: `%s`", name)
location.Backup()
}
},
}

View File

@@ -20,6 +20,7 @@ import (
"fmt"
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra"
)
@@ -29,11 +30,16 @@ var checkCmd = &cobra.Command{
Short: "Check if everything is setup",
Run: func(cmd *cobra.Command, args []string) {
if !internal.CheckIfResticIsCallable() {
cobra.CheckErr(errors.New("restic is not callable. Install: https://restic.readthedocs.io/en/stable/020_installation.html"))
CheckErr(errors.New("restic is not callable. Install: https://restic.readthedocs.io/en/stable/020_installation.html"))
}
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
config := internal.GetConfig()
err := config.CheckConfig()
cobra.CheckErr(err)
err = config.CheckConfig()
CheckErr(err)
fmt.Println("Everyting is fine.")
},
}

View File

@@ -17,6 +17,7 @@ package cmd
import (
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra"
)
@@ -26,8 +27,12 @@ var cronCmd = &cobra.Command{
Short: "Run cron job for automated backups",
Long: `Intended to be mainly triggered by an automated system like systemd or crontab. For each location checks if a cron backup is due and runs it.`,
Run: func(cmd *cobra.Command, args []string) {
err := internal.RunCron()
cobra.CheckErr(err)
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
err = internal.RunCron()
CheckErr(err)
},
}

View File

@@ -19,6 +19,7 @@ import (
"fmt"
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra"
)
@@ -27,16 +28,20 @@ var execCmd = &cobra.Command{
Use: "exec",
Short: "Execute arbitrary native restic commands for given backends",
Run: func(cmd *cobra.Command, args []string) {
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
config := internal.GetConfig()
if err := config.CheckConfig(); err != nil {
panic(err)
}
{
selected, err := internal.GetAllOrSelected(cmd, true)
cobra.CheckErr(err)
CheckErr(err)
for _, name := range selected {
fmt.Println(name)
backend := config.Backends[name]
backend, _ := internal.GetBackend(name)
backend.Exec(args)
}
}

View File

@@ -17,6 +17,7 @@ package cmd
import (
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra"
)
@@ -25,19 +26,23 @@ var forgetCmd = &cobra.Command{
Use: "forget",
Short: "Forget and optionally prune snapshots according the specified policies",
Run: func(cmd *cobra.Command, args []string) {
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
config := internal.GetConfig()
if err := config.CheckConfig(); err != nil {
panic(err)
}
{
selected, err := internal.GetAllOrSelected(cmd, false)
cobra.CheckErr(err)
CheckErr(err)
prune, _ := cmd.Flags().GetBool("prune")
dry, _ := cmd.Flags().GetBool("dry-run")
for _, name := range selected {
location := config.Locations[name]
location, _ := internal.GetLocation(name)
err := location.Forget(prune, dry)
cobra.CheckErr(err)
CheckErr(err)
}
}
},

View File

@@ -25,7 +25,7 @@ var installCmd = &cobra.Command{
Short: "Install restic if missing",
Run: func(cmd *cobra.Command, args []string) {
err := bins.InstallRestic()
cobra.CheckErr(err)
CheckErr(err)
},
}

View File

@@ -19,6 +19,7 @@ import (
"fmt"
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra"
)
@@ -27,17 +28,20 @@ var restoreCmd = &cobra.Command{
Use: "restore",
Short: "Restore backup for location",
Run: func(cmd *cobra.Command, args []string) {
config := internal.GetConfig()
err := lock.Lock()
CheckErr(err)
defer lock.Unlock()
location, _ := cmd.Flags().GetString("location")
l, ok := config.Locations[location]
l, ok := internal.GetLocation(location)
if !ok {
cobra.CheckErr(fmt.Errorf("invalid location \"%s\"", location))
CheckErr(fmt.Errorf("invalid location \"%s\"", location))
}
target, _ := cmd.Flags().GetString("to")
from, _ := cmd.Flags().GetString("from")
force, _ := cmd.Flags().GetBool("force")
err := l.Restore(target, from, force)
cobra.CheckErr(err)
err = l.Restore(target, from, force)
CheckErr(err)
},
}

View File

@@ -20,12 +20,21 @@ import (
"os"
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
func CheckErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
lock.Unlock()
os.Exit(1)
}
}
var cfgFile string
// rootCmd represents the base command when called without any subcommands
@@ -38,7 +47,7 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.Execute())
CheckErr(rootCmd.Execute())
}
func init() {
@@ -63,7 +72,7 @@ func initConfig() {
} else {
// Find home directory.
home, err := homedir.Dir()
cobra.CheckErr(err)
CheckErr(err)
viper.AddConfigPath(".")
viper.AddConfigPath(home)

View File

@@ -26,7 +26,7 @@ var upgradeCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
noRestic, _ := cmd.Flags().GetBool("no-restic")
err := bins.Upgrade(!noRestic)
cobra.CheckErr(err)
CheckErr(err)
},
}