mirror of
https://gitlab.durp.info/durfy/modules/gopwsh.git
synced 2026-05-09 00:31:40 -05:00
initial commit
This commit is contained in:
parent
eb87a9c881
commit
6cdb3d2426
6 changed files with 377 additions and 0 deletions
73
pwsh.go
Normal file
73
pwsh.go
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue