2021-09-16 21:38:43 +08:00
//go:build windows
2018-10-27 14:35:07 +02:00
// +build windows
2020-09-29 19:10:57 +10:00
package oscommands
2018-10-27 14:35:07 +02:00
2021-10-24 10:43:48 +11:00
import (
"bytes"
"io"
"os/exec"
2022-08-06 18:50:52 +10:00
"github.com/sasha-s/go-deadlock"
2021-10-24 10:43:48 +11:00
)
type Buffer struct {
b bytes . Buffer
2022-08-07 09:44:50 +10:00
m deadlock . Mutex
2021-10-24 10:43:48 +11:00
}
func ( b * Buffer ) Read ( p [ ] byte ) ( n int , err error ) {
b . m . Lock ( )
defer b . m . Unlock ( )
return b . b . Read ( p )
}
2022-03-19 09:38:49 +11:00
2021-10-24 10:43:48 +11:00
func ( b * Buffer ) Write ( p [ ] byte ) ( n int , err error ) {
b . m . Lock ( )
defer b . m . Unlock ( )
return b . b . Write ( p )
}
// TODO: Remove this hack and replace it with a proper way to run commands live on windows. We still have an issue where if a password is requested, the request for a password is written straight to stdout because we can't control the stdout of a subprocess of a subprocess. Keep an eye on https://github.com/creack/pty/pull/109
2022-01-09 09:52:43 +11:00
func ( self * cmdObjRunner ) getCmdHandler ( cmd * exec . Cmd ) ( * cmdHandler , error ) {
stdoutReader , stdoutWriter := io . Pipe ( )
cmd . Stdout = stdoutWriter
buf := & Buffer { }
cmd . Stdin = buf
if err := cmd . Start ( ) ; err != nil {
return nil , err
}
// because we don't yet have windows support for a pty, we instead just
// pass our standard stream handlers and because there's no pty to close
// we pass a no-op function for that.
return & cmdHandler {
stdoutPipe : stdoutReader ,
stdinPipe : buf ,
close : func ( ) error { return nil } ,
} , nil
2018-10-27 14:35:07 +02:00
}