mirror of
https://gitlab.durp.info/durfy/modules/gopwsh.git
synced 2026-05-07 16:10:29 -05:00
73 lines
985 B
Go
73 lines
985 B
Go
package gopwsh
|
|
|
|
import (
|
|
"os/exec"
|
|
"runtime"
|
|
)
|
|
|
|
type Pwsh struct{}
|
|
|
|
func NewPwshHandler() Pwsh {
|
|
return Pwsh{}
|
|
}
|
|
|
|
func getPwshBinary() string {
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
return "pwsh.exe"
|
|
default:
|
|
return "pwsh"
|
|
}
|
|
}
|
|
|
|
func IsPwshInstalled() bool {
|
|
binary := getPwshBinary()
|
|
cmd := exec.Command(binary)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if string(output) == "" {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func RunPwshCommand(cmd string) (string, error) {
|
|
|
|
ps := NewPwshHandler()
|
|
shell, err := ps.New()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
defer shell.Exit()
|
|
|
|
stdOut, err := shell.Execute(cmd)
|
|
if err != nil {
|
|
return stdOut, err
|
|
}
|
|
|
|
return stdOut, nil
|
|
}
|
|
|
|
func (ps *Pwsh) New() (Shell, error) {
|
|
|
|
binary := getPwshBinary()
|
|
handle, stdIn, stdOut, err := StartProcess(
|
|
binary,
|
|
"-NoExit",
|
|
"-NoProfile",
|
|
"-Command",
|
|
"-",
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &shell{
|
|
handle,
|
|
stdIn,
|
|
stdOut,
|
|
}, nil
|
|
}
|