Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows Specific Code To Run Commands With Powershell #431

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
test
autorestic
data
dist
dist
*.exe
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"
"path/filepath"
"runtime"
"strings"

"github.com/cupcakearmy/autorestic/internal"
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions internal/shell.go
Original file line number Diff line number Diff line change
@@ -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...)
}
17 changes: 17 additions & 0 deletions internal/shell_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down