2017-04-17 18:08:24 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-06-05 18:23:21 -04:00
|
|
|
"os"
|
2023-07-17 10:06:44 -04:00
|
|
|
"os/exec"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2017-08-16 16:02:38 -04:00
|
|
|
return NewShellProgramFuncWithEnv(name, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewShellProgramFuncWithEnv creates programs that are executed in a Shell with environment variables
|
|
|
|
func NewShellProgramFuncWithEnv(name string, env *map[string]string) ProgramFunc {
|
2017-04-17 18:08:24 -04:00
|
|
|
return func(args ...string) Program {
|
2017-08-16 16:02:38 -04:00
|
|
|
return &Shell{cmd: createProgramCmdRedirectErr(name, args, env)}
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 16:02:38 -04:00
|
|
|
func createProgramCmdRedirectErr(commandName string, args []string, env *map[string]string) *exec.Cmd {
|
|
|
|
programCmd := exec.Command(commandName, args...)
|
|
|
|
if env != nil {
|
|
|
|
for k, v := range *env {
|
2023-07-17 10:06:44 -04:00
|
|
|
programCmd.Env = append(programCmd.Environ(), k+"="+v)
|
2017-08-16 16:02:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
programCmd.Stderr = os.Stderr
|
|
|
|
return programCmd
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// Shell invokes shell commands to talk with a remote credentials-helper.
|
2017-04-17 18:08:24 -04:00
|
|
|
type Shell struct {
|
|
|
|
cmd *exec.Cmd
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// Output returns responses from the remote credentials-helper.
|
2017-04-17 18:08:24 -04:00
|
|
|
func (s *Shell) Output() ([]byte, error) {
|
|
|
|
return s.cmd.Output()
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:06:44 -04:00
|
|
|
// Input sets the input to send to a remote credentials-helper.
|
2017-04-17 18:08:24 -04:00
|
|
|
func (s *Shell) Input(in io.Reader) {
|
|
|
|
s.cmd.Stdin = in
|
|
|
|
}
|