mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
command/token: Use cmd on windows instead of sh
This commit is contained in:
@@ -3,8 +3,10 @@ package token
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/kardianos/osext"
|
||||
@@ -57,19 +59,24 @@ type Helper struct {
|
||||
|
||||
// Erase deletes the contents from the helper.
|
||||
func (h *Helper) Erase() error {
|
||||
cmd := h.cmd("erase")
|
||||
cmd, err := h.cmd("erase")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error: %s", err)
|
||||
}
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf(
|
||||
"Error: %s\n\n%s", err, string(output))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get gets the token value from the helper.
|
||||
func (h *Helper) Get() (string, error) {
|
||||
var buf, stderr bytes.Buffer
|
||||
cmd := h.cmd("get")
|
||||
cmd, err := h.cmd("get")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Error: %s", err)
|
||||
}
|
||||
cmd.Stdout = &buf
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
@@ -83,7 +90,10 @@ func (h *Helper) Get() (string, error) {
|
||||
// Store stores the token value into the helper.
|
||||
func (h *Helper) Store(v string) error {
|
||||
buf := bytes.NewBufferString(v)
|
||||
cmd := h.cmd("store")
|
||||
cmd, err := h.cmd("store")
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error: %s", err)
|
||||
}
|
||||
cmd.Stdin = buf
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf(
|
||||
@@ -93,7 +103,24 @@ func (h *Helper) Store(v string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Helper) cmd(op string) *exec.Cmd {
|
||||
cmd := exec.Command("sh", "-c", strings.Replace(h.Path, "\\", "\\\\", -1)+" "+op)
|
||||
return cmd
|
||||
func (h *Helper) cmd(op string) (*exec.Cmd, error) {
|
||||
script := strings.Replace(h.Path, "\\", "\\\\", -1) + " " + op
|
||||
return ExecScript(script)
|
||||
}
|
||||
|
||||
// ExecScript returns a command to execute a script
|
||||
func ExecScript(script string) (*exec.Cmd, error) {
|
||||
var shell, flag string
|
||||
if runtime.GOOS == "windows" {
|
||||
shell = "cmd"
|
||||
flag = "/C"
|
||||
} else {
|
||||
shell = "/bin/sh"
|
||||
flag = "-c"
|
||||
}
|
||||
if other := os.Getenv("SHELL"); other != "" {
|
||||
shell = other
|
||||
}
|
||||
cmd := exec.Command(shell, flag, script)
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user