mirror of
https://github.com/Baipyrus/ProxySwitcher.git
synced 2024-12-27 05:01:45 +00:00
46 lines
875 B
Go
46 lines
875 B
Go
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// Create a single powershell process and leave closing, input and output open
|
|
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
|
|
}
|
|
|
|
err = cmd.Start()
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
return &stdin, func() error {
|
|
// Close stdin pipe
|
|
stdin.Close()
|
|
|
|
// Wait for command to flush
|
|
err := cmd.Wait()
|
|
return err
|
|
}, nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|