2021-04-09 01:55:10 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2021-04-12 00:02:35 +02:00
|
|
|
"io"
|
2021-04-09 01:55:10 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2021-04-17 13:04:40 +02:00
|
|
|
|
|
|
|
"github.com/cupcakearmy/autorestic/internal/colors"
|
2022-02-16 21:42:54 +01:00
|
|
|
"github.com/cupcakearmy/autorestic/internal/flags"
|
2022-04-27 00:48:52 +02:00
|
|
|
"github.com/fatih/color"
|
2021-04-09 01:55:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func CheckIfCommandIsCallable(cmd string) bool {
|
|
|
|
_, err := exec.LookPath(cmd)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckIfResticIsCallable() bool {
|
2022-04-27 00:48:52 +02:00
|
|
|
return CheckIfCommandIsCallable(flags.RESTIC_BIN)
|
2021-04-09 01:55:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteOptions struct {
|
|
|
|
Command string
|
|
|
|
Envs map[string]string
|
|
|
|
Dir string
|
2022-04-27 00:48:52 +02:00
|
|
|
Silent bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ColoredWriter struct {
|
|
|
|
target io.Writer
|
|
|
|
color *color.Color
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w ColoredWriter) Write(p []byte) (n int, err error) {
|
|
|
|
colored := []byte(w.color.Sprint(string(p)))
|
|
|
|
w.target.Write(colored)
|
|
|
|
return len(p), nil
|
2021-04-09 01:55:10 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:51:32 +02:00
|
|
|
func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
2021-04-09 01:55:10 +02:00
|
|
|
cmd := exec.Command(options.Command, args...)
|
|
|
|
env := os.Environ()
|
|
|
|
for k, v := range options.Envs {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
cmd.Env = env
|
|
|
|
cmd.Dir = options.Dir
|
|
|
|
|
2022-02-16 21:42:54 +01:00
|
|
|
if flags.VERBOSE {
|
2021-04-17 13:04:40 +02:00
|
|
|
colors.Faint.Printf("> Executing: %s\n", cmd)
|
|
|
|
}
|
|
|
|
|
2021-04-09 01:55:10 +02:00
|
|
|
var out bytes.Buffer
|
|
|
|
var error bytes.Buffer
|
2022-04-27 00:48:52 +02:00
|
|
|
if flags.VERBOSE && !options.Silent {
|
|
|
|
var colored ColoredWriter = ColoredWriter{
|
|
|
|
target: os.Stdout,
|
|
|
|
color: colors.Faint,
|
|
|
|
}
|
|
|
|
mw := io.MultiWriter(colored, &out)
|
|
|
|
cmd.Stdout = mw
|
|
|
|
} else {
|
|
|
|
cmd.Stdout = &out
|
|
|
|
}
|
2021-04-09 01:55:10 +02:00
|
|
|
cmd.Stderr = &error
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
2022-04-27 00:48:52 +02:00
|
|
|
code := -1
|
2022-04-14 11:51:32 +02:00
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
2022-04-27 00:48:52 +02:00
|
|
|
code = exitError.ExitCode()
|
2022-04-14 11:51:32 +02:00
|
|
|
}
|
2022-04-27 00:48:52 +02:00
|
|
|
return code, error.String(), err
|
2021-04-09 01:55:10 +02:00
|
|
|
}
|
2022-04-14 11:51:32 +02:00
|
|
|
return 0, out.String(), nil
|
2021-04-09 01:55:10 +02:00
|
|
|
}
|
|
|
|
|
2022-04-14 11:51:32 +02:00
|
|
|
func ExecuteResticCommand(options ExecuteOptions, args ...string) (int, string, error) {
|
2022-04-27 00:48:52 +02:00
|
|
|
options.Command = flags.RESTIC_BIN
|
2021-10-28 18:10:14 +02:00
|
|
|
var c = GetConfig()
|
2022-04-27 00:48:52 +02:00
|
|
|
var optionsAsString = getOptions(c.Global, []string{"all"})
|
2021-10-28 18:10:14 +02:00
|
|
|
args = append(optionsAsString, args...)
|
2021-04-09 01:55:10 +02:00
|
|
|
return ExecuteCommand(options, args...)
|
|
|
|
}
|
2021-04-12 00:02:35 +02:00
|
|
|
|
|
|
|
func CopyFile(from, to string) error {
|
2022-02-09 20:35:29 +01:00
|
|
|
original, err := os.Open(from)
|
2021-04-12 00:02:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer original.Close()
|
|
|
|
|
2022-02-09 20:35:29 +01:00
|
|
|
new, err := os.Create(to)
|
2021-04-12 00:02:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer new.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(new, original); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-31 22:32:01 +01:00
|
|
|
|
|
|
|
func CheckIfVolumeExists(volume string) bool {
|
2022-04-14 11:51:32 +02:00
|
|
|
_, _, err := ExecuteCommand(ExecuteOptions{Command: "docker"}, "volume", "inspect", volume)
|
2021-10-31 22:32:01 +01:00
|
|
|
return err == nil
|
|
|
|
}
|
2022-04-14 11:51:32 +02:00
|
|
|
|
|
|
|
func ArrayContains[T comparable](arr []T, needle T) bool {
|
|
|
|
for _, item := range arr {
|
|
|
|
if item == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|