2024-08-31 16:26:45 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-08-31 17:13:01 +00:00
|
|
|
"fmt"
|
2024-08-31 16:26:45 +00:00
|
|
|
"io"
|
|
|
|
"os/exec"
|
2024-08-31 17:13:01 +00:00
|
|
|
"strings"
|
2024-08-31 16:26:45 +00:00
|
|
|
)
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
// Create a single powershell process and leave closing, input and output open
|
2024-08-31 16:26:45 +00:00
|
|
|
func ReadyCmd() (*io.WriteCloser, func() error, error) {
|
|
|
|
cmd := exec.Command("powershell", "-NoLogo", "-NoProfile", "-Command", "-")
|
|
|
|
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stdout
|
|
|
|
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2024-08-31 16:26:45 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stdin, func() error {
|
2024-08-31 17:13:01 +00:00
|
|
|
// Close stdin pipe
|
2024-08-31 16:26:45 +00:00
|
|
|
stdin.Close()
|
|
|
|
|
2024-08-31 17:13:01 +00:00
|
|
|
// Wait for command to flush
|
|
|
|
err := cmd.Wait()
|
2024-08-31 19:11:54 +00:00
|
|
|
return err
|
2024-08-31 16:26:45 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2024-08-31 17:13:01 +00:00
|
|
|
|
|
|
|
func ExecCmds(commands []*Command, stdin *io.WriteCloser) {
|
|
|
|
for _, command := range commands {
|
|
|
|
cmdArgs := strings.Join(command.Arguments, " ")
|
|
|
|
cmdStr := fmt.Sprintf("%s %s", command.Name, cmdArgs)
|
|
|
|
fmt.Fprintln(*stdin, cmdStr)
|
|
|
|
}
|
|
|
|
}
|