mirror of
https://github.com/cupcakearmy/autorestic.git
synced 2025-09-02 00:30:40 +00:00
cleaner output and ctrl-c
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -80,7 +81,7 @@ func (b Backend) validate() error {
|
||||
if err := CopyFile(file, file+".old"); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Saved a backup copy of your file next the the original.")
|
||||
colors.Secondary.Println("Saved a backup copy of your file next the the original.")
|
||||
viper.Set("backends", c.Backends)
|
||||
viper.WriteConfig()
|
||||
}
|
||||
@@ -96,7 +97,7 @@ func (b Backend) validate() error {
|
||||
} else {
|
||||
// If not initialize
|
||||
out, err := ExecuteResticCommand(options, "init")
|
||||
fmt.Println(out)
|
||||
colors.Faint.Println(out)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -108,6 +109,8 @@ func (b Backend) Exec(args []string) error {
|
||||
}
|
||||
options := ExecuteOptions{Envs: env}
|
||||
out, err := ExecuteResticCommand(options, args...)
|
||||
fmt.Println(out)
|
||||
if VERBOSE {
|
||||
colors.Faint.Println(out)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"github.com/blang/semver/v4"
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||
)
|
||||
|
||||
const INSTALL_PATH = "/usr/local/bin"
|
||||
@@ -46,11 +47,11 @@ func dlJSON(url string) (GithubRelease, error) {
|
||||
|
||||
func Uninstall(restic bool) error {
|
||||
if err := os.Remove(path.Join(INSTALL_PATH, "autorestic")); err != nil {
|
||||
fmt.Println(err)
|
||||
colors.Error.Println(err)
|
||||
}
|
||||
if restic {
|
||||
if err := os.Remove(path.Join(INSTALL_PATH, "restic")); err != nil {
|
||||
fmt.Println(err)
|
||||
colors.Error.Println(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -59,7 +60,7 @@ func Uninstall(restic bool) error {
|
||||
func InstallRestic() error {
|
||||
installed := internal.CheckIfCommandIsCallable("restic")
|
||||
if installed {
|
||||
fmt.Println("restic already installed")
|
||||
colors.Body.Println("restic already installed")
|
||||
return nil
|
||||
} else {
|
||||
body, err := dlJSON("https://api.github.com/repos/restic/restic/releases/latest")
|
||||
@@ -69,10 +70,8 @@ func InstallRestic() error {
|
||||
ending := fmt.Sprintf("_%s_%s.bz2", runtime.GOOS, runtime.GOARCH)
|
||||
for _, asset := range body.Assets {
|
||||
if strings.HasSuffix(asset.Name, ending) {
|
||||
// Found
|
||||
fmt.Println(asset.Link)
|
||||
|
||||
// Download archive
|
||||
colors.Faint.Println("Downloading:", asset.Link)
|
||||
resp, err := http.Get(asset.Link)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -91,7 +90,7 @@ func InstallRestic() error {
|
||||
defer file.Close()
|
||||
io.Copy(file, bz)
|
||||
|
||||
fmt.Printf("Successfully installed restic under %s\n", INSTALL_PATH)
|
||||
colors.Success.Printf("Successfully installed restic under %s\n", INSTALL_PATH)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -103,7 +102,7 @@ func upgradeRestic() error {
|
||||
out, err := internal.ExecuteCommand(internal.ExecuteOptions{
|
||||
Command: "restic",
|
||||
}, "self-update")
|
||||
fmt.Println(out)
|
||||
colors.Faint.Println(out)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -119,8 +118,6 @@ func Upgrade(restic bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(current)
|
||||
|
||||
body, err := dlJSON("https://api.github.com/repos/cupcakearmy/autorestic/releases/latest")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -130,10 +127,10 @@ func Upgrade(restic bool) error {
|
||||
return err
|
||||
}
|
||||
if current.GT(latest) {
|
||||
|
||||
fmt.Println("Updated autorestic")
|
||||
// TODO: Actually download and install
|
||||
colors.Success.Println("Updated autorestic")
|
||||
} else {
|
||||
fmt.Println("Already up to date")
|
||||
colors.Body.Println("Already up to date")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -1,12 +1,22 @@
|
||||
package colors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
var Body = color.New()
|
||||
var Primary = color.New(color.Underline, color.Bold, color.BgBlue)
|
||||
var Primary = color.New(color.Bold, color.BgBlue, color.FgHiWhite)
|
||||
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)
|
||||
|
||||
func PrimaryPrint(msg string, args ...interface{}) {
|
||||
fmt.Printf("\n\n%s\n\n", Primary.Sprintf(" "+msg+" ", args...))
|
||||
}
|
||||
|
||||
func DisableColors(state bool) {
|
||||
color.NoColor = state
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/cupcakearmy/autorestic/internal/colors"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
@@ -13,6 +14,9 @@ import (
|
||||
|
||||
const VERSION = "1.0.0"
|
||||
|
||||
var CI bool = false
|
||||
var VERBOSE bool = false
|
||||
|
||||
type Config struct {
|
||||
Locations []Location `mapstructure:"locations"`
|
||||
Backends []Backend `mapstructure:"backends"`
|
||||
@@ -24,6 +28,13 @@ var config *Config
|
||||
func GetConfig() *Config {
|
||||
if config == nil {
|
||||
once.Do(func() {
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
colors.Faint.Println("Using config file:", viper.ConfigFileUsed())
|
||||
} else {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
config = &Config{}
|
||||
if err := viper.UnmarshalExact(config); err != nil {
|
||||
panic(err)
|
||||
@@ -45,6 +56,9 @@ func GetPathRelativeToConfig(p string) (string, error) {
|
||||
}
|
||||
|
||||
func (c *Config) CheckConfig() error {
|
||||
if c == nil {
|
||||
return fmt.Errorf("config could not be loaded/found")
|
||||
}
|
||||
if !CheckIfResticIsCallable() {
|
||||
return fmt.Errorf(`restic was not found. Install either with "autorestic install" or manually`)
|
||||
}
|
||||
|
@@ -75,44 +75,23 @@ func ExecuteHooks(commands []string, options ExecuteOptions) error {
|
||||
if len(commands) == 0 {
|
||||
return nil
|
||||
}
|
||||
colors.Secondary.Println("🪝 Running hooks")
|
||||
colors.Secondary.Println("\nRunning hooks")
|
||||
for _, command := range commands {
|
||||
colors.Body.Println(command)
|
||||
colors.Body.Println("> " + command)
|
||||
out, err := ExecuteCommand(options, "-c", command)
|
||||
colors.Faint.Print(out)
|
||||
return err
|
||||
}
|
||||
fmt.Println("")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l Location) forEachBackend(fn func(ExecuteOptions) error) error {
|
||||
from, err := GetPathRelativeToConfig(l.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, to := range l.To {
|
||||
backend, _ := GetBackend(to)
|
||||
env, err := backend.getEnv()
|
||||
if VERBOSE {
|
||||
colors.Faint.Println(out)
|
||||
}
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
options := ExecuteOptions{
|
||||
Command: "bash",
|
||||
Envs: env,
|
||||
Dir: from,
|
||||
}
|
||||
if err := fn(options); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
colors.Body.Println("")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l Location) Backup() error {
|
||||
fmt.Printf("\n\n")
|
||||
colors.Primary.Printf("💽 Backing up location \"%s\"", l.Name)
|
||||
fmt.Printf("\n")
|
||||
colors.PrimaryPrint(" Backing up location \"%s\" ", l.Name)
|
||||
from, err := GetPathRelativeToConfig(l.From)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -141,7 +120,9 @@ func (l Location) Backup() error {
|
||||
cmd = append(cmd, flags...)
|
||||
cmd = append(cmd, ".")
|
||||
out, err := ExecuteResticCommand(options, cmd...)
|
||||
colors.Faint.Print(out)
|
||||
if VERBOSE {
|
||||
colors.Faint.Println(out)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -149,12 +130,29 @@ func (l Location) Backup() error {
|
||||
if err := ExecuteHooks(l.Hooks.After, options); err != nil {
|
||||
return nil
|
||||
}
|
||||
colors.Success.Println("✅ Done")
|
||||
colors.Success.Println("Done")
|
||||
return err
|
||||
}
|
||||
|
||||
func (l Location) Forget(prune bool, dry bool) error {
|
||||
return l.forEachBackend(func(options ExecuteOptions) error {
|
||||
colors.PrimaryPrint("Forgetting for location \"%s\"", l.Name)
|
||||
|
||||
from, err := GetPathRelativeToConfig(l.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, to := range l.To {
|
||||
backend, _ := GetBackend(to)
|
||||
colors.Secondary.Printf("For backend \"%s\"\n", backend.Name)
|
||||
env, err := backend.getEnv()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
options := ExecuteOptions{
|
||||
Command: "bash",
|
||||
Envs: env,
|
||||
Dir: from,
|
||||
}
|
||||
flags := l.getOptions("forget")
|
||||
cmd := []string{"forget", "--path", options.Dir}
|
||||
if prune {
|
||||
@@ -165,12 +163,15 @@ func (l Location) Forget(prune bool, dry bool) error {
|
||||
}
|
||||
cmd = append(cmd, flags...)
|
||||
out, err := ExecuteResticCommand(options, cmd...)
|
||||
fmt.Println(out)
|
||||
if VERBOSE {
|
||||
colors.Faint.Println(out)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
colors.Success.Println("Done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l Location) hasBackend(backend string) bool {
|
||||
@@ -193,7 +194,9 @@ func (l Location) Restore(to, from string, force bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Restoring location to %s using %s.\n", to, from)
|
||||
colors.PrimaryPrint("Restoring location \"%s\"", l.Name)
|
||||
colors.Secondary.Println("Restoring lastest snapshot")
|
||||
colors.Body.Printf("%s → %s.\n", from, to)
|
||||
|
||||
// Check if target is empty
|
||||
if !force {
|
||||
@@ -223,6 +226,7 @@ func (l Location) Restore(to, from string, force bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
colors.Success.Println("Done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -242,7 +246,7 @@ func (l Location) RunCron() error {
|
||||
lock.SetCron(l.Name, now.Unix())
|
||||
l.Backup()
|
||||
} else {
|
||||
fmt.Printf("Skipping \"%s\", not due yet.\n", l.Name)
|
||||
colors.Body.Printf("Skipping \"%s\", not due yet.\n", l.Name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user