autorestic/internal/utils/utils.go

120 lines
2.5 KiB
Go
Raw Normal View History

package utils
2021-04-08 23:55:10 +00:00
import (
"bytes"
"fmt"
2021-04-11 22:02:35 +00:00
"io"
2021-04-08 23:55:10 +00:00
"os"
"os/exec"
2021-04-17 11:04:40 +00:00
"github.com/cupcakearmy/autorestic/internal/colors"
2022-02-16 20:42:54 +00:00
"github.com/cupcakearmy/autorestic/internal/flags"
"github.com/cupcakearmy/autorestic/internal/options"
"github.com/fatih/color"
2021-04-08 23:55:10 +00:00
)
func CheckIfCommandIsCallable(cmd string) bool {
_, err := exec.LookPath(cmd)
return err == nil
}
func CheckIfResticIsCallable() bool {
return CheckIfCommandIsCallable(flags.RESTIC_BIN)
2021-04-08 23:55:10 +00:00
}
type ExecuteOptions struct {
Command string
Envs map[string]string
Dir string
Silent bool
Global options.Options
}
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-08 23:55:10 +00:00
}
func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) {
2021-04-08 23:55:10 +00: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 20:42:54 +00:00
if flags.VERBOSE {
2021-04-17 11:04:40 +00:00
colors.Faint.Printf("> Executing: %s\n", cmd)
}
2021-04-08 23:55:10 +00:00
var out bytes.Buffer
var error bytes.Buffer
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-08 23:55:10 +00:00
cmd.Stderr = &error
err := cmd.Run()
if err != nil {
code := -1
if exitError, ok := err.(*exec.ExitError); ok {
code = exitError.ExitCode()
}
return code, error.String(), err
2021-04-08 23:55:10 +00:00
}
return 0, out.String(), nil
2021-04-08 23:55:10 +00:00
}
func ExecuteResticCommand(options ExecuteOptions, args ...string) (int, string, error) {
options.Command = flags.RESTIC_BIN
var optionsAsString = options.Global.GetOptions([]string{"all"})
2021-10-28 16:10:14 +00:00
args = append(optionsAsString, args...)
2021-04-08 23:55:10 +00:00
return ExecuteCommand(options, args...)
}
2021-04-11 22:02:35 +00:00
func CopyFile(from, to string) error {
original, err := os.Open(from)
2021-04-11 22:02:35 +00:00
if err != nil {
return nil
}
defer original.Close()
new, err := os.Create(to)
2021-04-11 22:02:35 +00:00
if err != nil {
return nil
}
defer new.Close()
if _, err := io.Copy(new, original); err != nil {
return err
}
return nil
}
2021-10-31 21:32:01 +00:00
func CheckIfVolumeExists(volume string) bool {
_, _, err := ExecuteCommand(ExecuteOptions{Command: "docker"}, "volume", "inspect", volume)
2021-10-31 21:32:01 +00:00
return err == nil
}
func ArrayContains[T comparable](arr []T, needle T) bool {
for _, item := range arr {
if item == needle {
return true
}
}
return false
}