2017-04-17 18:08:24 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-06-05 18:23:21 -04:00
|
|
|
"os"
|
2017-04-17 18:08:24 -04:00
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Program is an interface to execute external programs.
|
|
|
|
type Program interface {
|
|
|
|
Output() ([]byte, error)
|
|
|
|
Input(in io.Reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProgramFunc is a type of function that initializes programs based on arguments.
|
|
|
|
type ProgramFunc func(args ...string) Program
|
|
|
|
|
|
|
|
// NewShellProgramFunc creates programs that are executed in a Shell.
|
|
|
|
func NewShellProgramFunc(name string) ProgramFunc {
|
|
|
|
return func(args ...string) Program {
|
2017-06-05 18:23:21 -04:00
|
|
|
return &Shell{cmd: newCmdRedirectErr(name, args)}
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
func newCmdRedirectErr(name string, args []string) *exec.Cmd {
|
|
|
|
newCmd := exec.Command(name, args...)
|
|
|
|
newCmd.Stderr = os.Stderr
|
|
|
|
return newCmd
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// Shell invokes shell commands to talk with a remote credentials helper.
|
|
|
|
type Shell struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output returns responses from the remote credentials helper.
|
|
|
|
func (s *Shell) Output() ([]byte, error) {
|
|
|
|
return s.cmd.Output()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Input sets the input to send to a remote credentials helper.
|
|
|
|
func (s *Shell) Input(in io.Reader) {
|
|
|
|
s.cmd.Stdin = in
|
|
|
|
}
|