command.go 891 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package client
  2. import (
  3. "io"
  4. "os/exec"
  5. )
  6. // Program is an interface to execute external programs.
  7. type Program interface {
  8. Output() ([]byte, error)
  9. Input(in io.Reader)
  10. }
  11. // ProgramFunc is a type of function that initializes programs based on arguments.
  12. type ProgramFunc func(args ...string) Program
  13. // NewShellProgramFunc creates programs that are executed in a Shell.
  14. func NewShellProgramFunc(name string) ProgramFunc {
  15. return func(args ...string) Program {
  16. return &Shell{cmd: exec.Command(name, args...)}
  17. }
  18. }
  19. // Shell invokes shell commands to talk with a remote credentials helper.
  20. type Shell struct {
  21. cmd *exec.Cmd
  22. }
  23. // Output returns responses from the remote credentials helper.
  24. func (s *Shell) Output() ([]byte, error) {
  25. return s.cmd.Output()
  26. }
  27. // Input sets the input to send to a remote credentials helper.
  28. func (s *Shell) Input(in io.Reader) {
  29. s.cmd.Stdin = in
  30. }