diff --git a/.gitignore b/.gitignore index 8103f40..a277fca 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ test autorestic data -dist \ No newline at end of file +dist +*.exe diff --git a/cmd/root.go b/cmd/root.go index 3da9e25..c710893 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "os" "path/filepath" + "runtime" "strings" "github.com/cupcakearmy/autorestic/internal" @@ -37,10 +38,17 @@ func Execute() { } func init() { + restic := "restic" + // For Windows we default the executable differently as the current directory + // is usually not included in search path by default. + if runtime.GOOS == "windows" { + restic = "./restic" + } + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.autorestic.yml or ./.autorestic.yml)") rootCmd.PersistentFlags().BoolVar(&flags.CI, "ci", false, "CI mode disabled interactive mode and colors and enables verbosity") rootCmd.PersistentFlags().BoolVarP(&flags.VERBOSE, "verbose", "v", false, "verbose mode") - rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", "restic", "specify custom restic binary") + rootCmd.PersistentFlags().StringVar(&flags.RESTIC_BIN, "restic-bin", restic, "specify custom restic binary") rootCmd.PersistentFlags().StringVar(&flags.DOCKER_IMAGE, "docker-image", "cupcakearmy/autorestic:"+internal.VERSION, "specify a custom docker image") cobra.OnInitialize(initConfig) } diff --git a/internal/location.go b/internal/location.go index 221c5f3..75b7fb4 100644 --- a/internal/location.go +++ b/internal/location.go @@ -139,7 +139,7 @@ func (l Location) ExecuteHooks(commands []string, options ExecuteOptions) error colors.Secondary.Println("\nRunning hooks") for _, command := range commands { colors.Body.Println("> " + command) - _, out, err := ExecuteCommand(options, "-c", command) + _, out, err := ExecuteCommand(options, shellArgs, command) if err != nil { colors.Error.Println(out) return err @@ -179,7 +179,7 @@ func (l Location) Backup(cron bool, specificBackend string) []error { } cwd, _ := GetPathRelativeToConfig(".") options := ExecuteOptions{ - Command: "bash", + Command: shellName, Dir: cwd, Envs: map[string]string{ "AUTORESTIC_LOCATION": l.name, diff --git a/internal/shell.go b/internal/shell.go new file mode 100644 index 0000000..3bbf7ed --- /dev/null +++ b/internal/shell.go @@ -0,0 +1,12 @@ +//go:build !windows + +package internal + +import "os/exec" + +const shellName string = "bash" +const shellArgs string = "-c" + +func execCommand(cmd string, args ...string) *exec.Cmd { + return exec.Command(cmd, args...) +} diff --git a/internal/shell_windows.go b/internal/shell_windows.go new file mode 100644 index 0000000..4b5354b --- /dev/null +++ b/internal/shell_windows.go @@ -0,0 +1,17 @@ +//go:build windows + +package internal + +import ( + "os/exec" + "syscall" +) + +const shellName string = "PowerShell" +const shellArgs string = "-Command" + +func execCommand(command string, args ...string) *exec.Cmd { + cmd := exec.Command(command, args...) + cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + return cmd +} diff --git a/internal/utils.go b/internal/utils.go index c8bf799..e7a6dc5 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -40,7 +40,7 @@ func (w ColoredWriter) Write(p []byte) (n int, err error) { } func ExecuteCommand(options ExecuteOptions, args ...string) (int, string, error) { - cmd := exec.Command(options.Command, args...) + cmd := execCommand(options.Command, args...) env := os.Environ() for k, v := range options.Envs { env = append(env, fmt.Sprintf("%s=%s", k, v))