current state

This commit is contained in:
2021-04-12 10:55:57 +02:00
parent d293e93fa8
commit 640b60c47f
11 changed files with 113 additions and 22 deletions

View File

@@ -44,6 +44,18 @@ func dlJSON(url string) (GithubRelease, error) {
return parsed, nil
}
func Uninstall(restic bool) error {
if err := os.Remove(path.Join(INSTALL_PATH, "autorestic")); err != nil {
fmt.Println(err)
}
if restic {
if err := os.Remove(path.Join(INSTALL_PATH, "restic")); err != nil {
fmt.Println(err)
}
}
return nil
}
func InstallRestic() error {
installed := internal.CheckIfCommandIsCallable("restic")
if installed {

12
internal/colors/colors.go Normal file
View File

@@ -0,0 +1,12 @@
package colors
import (
"github.com/fatih/color"
)
var Body = color.New()
var Primary = color.New(color.Underline, color.Bold, color.BgBlue)
var Secondary = color.New(color.Bold, color.FgCyan)
var Success = color.New(color.FgGreen)
var Error = color.New(color.FgRed, color.Bold)
var Faint = color.New(color.Faint)

View File

@@ -45,6 +45,9 @@ func GetPathRelativeToConfig(p string) (string, error) {
}
func (c *Config) CheckConfig() error {
if !CheckIfResticIsCallable() {
return fmt.Errorf(`restic was not found. Install either with "autorestic install" or manually`)
}
found := map[string]bool{}
for _, backend := range c.Backends {
if err := backend.validate(); err != nil {

View File

@@ -7,6 +7,7 @@ import (
"path/filepath"
"time"
"github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/robfig/cron"
)
@@ -71,11 +72,17 @@ func (l Location) getOptions(key string) []string {
}
func ExecuteHooks(commands []string, options ExecuteOptions) error {
if len(commands) == 0 {
return nil
}
colors.Secondary.Println("🪝 Running hooks")
for _, command := range commands {
colors.Body.Println(command)
out, err := ExecuteCommand(options, "-c", command)
fmt.Println(out)
colors.Faint.Print(out)
return err
}
fmt.Println("")
return nil
}
@@ -103,26 +110,47 @@ func (l Location) forEachBackend(fn func(ExecuteOptions) error) error {
}
func (l Location) Backup() error {
return l.forEachBackend(func(options ExecuteOptions) error {
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
fmt.Printf("\n\n")
colors.Primary.Printf("💽 Backing up location \"%s\"", l.Name)
fmt.Printf("\n")
from, err := GetPathRelativeToConfig(l.From)
if err != nil {
return err
}
options := ExecuteOptions{
Command: "bash",
Dir: from,
}
if err := ExecuteHooks(l.Hooks.Before, options); err != nil {
return nil
}
for _, to := range l.To {
backend, _ := GetBackend(to)
colors.Secondary.Printf("Backend: %s\n", backend.Name)
env, err := backend.getEnv()
if err != nil {
return nil
}
options := ExecuteOptions{
Command: "restic",
Dir: from,
Envs: env,
}
flags := l.getOptions("backup")
cmd := []string{"backup"}
cmd = append(cmd, flags...)
cmd = append(cmd, ".")
out, err := ExecuteResticCommand(options, cmd...)
fmt.Println(out)
colors.Faint.Print(out)
if err != nil {
return err
}
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
return nil
}
}
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
return nil
})
}
colors.Success.Println("✅ Done")
return err
}
func (l Location) Forget(prune bool, dry bool) error {