| 1 |
package core |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os/exec" |
| 7 |
|
| 8 |
"github.com/dimonomid/nerdlog/log" |
| 9 |
"github.com/juju/errors" |
| 10 |
) |
| 11 |
|
| 12 |
// ShellTransportLocal is an implementation of ShellTransport that opens a |
| 13 |
// local shell. |
| 14 |
type ShellTransportLocal struct { |
| 15 |
params ShellTransportLocalParams |
| 16 |
} |
| 17 |
|
| 18 |
type ShellTransportLocalParams struct { |
| 19 |
Logger *log.Logger |
| 20 |
} |
| 21 |
|
| 22 |
// NewShellTransportLocal creates a new ShellTransportLocal with the given shell command. |
| 23 |
func NewShellTransportLocal(params ShellTransportLocalParams) *ShellTransportLocal { |
| 24 |
return &ShellTransportLocal{ |
| 25 |
params: params, |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// Connect starts the local shell and sends the result to the provided channel. |
| 30 |
func (s *ShellTransportLocal) Connect(resCh chan<- ShellConnUpdate) { |
| 31 |
go s.doConnect(resCh) |
| 32 |
} |
| 33 |
|
| 34 |
func (s *ShellTransportLocal) doConnect( |
| 35 |
resCh chan<- ShellConnUpdate, |
| 36 |
) (res ShellConnResult) { |
| 37 |
logger := s.params.Logger |
| 38 |
|
| 39 |
defer func() { |
| 40 |
if res.Err != nil { |
| 41 |
logger.Errorf("Connection failed: %s", res.Err) |
| 42 |
} |
| 43 |
|
| 44 |
resCh <- ShellConnUpdate{ |
| 45 |
Result: &res, |
| 46 |
} |
| 47 |
}() |
| 48 |
|
| 49 |
// TODO: make it configurable; e.g. it likely won't work on Windows |
| 50 |
shellBin := "/bin/sh" |
| 51 |
|
| 52 |
resCh <- ShellConnUpdate{ |
| 53 |
DebugInfo: &ShellConnDebugInfo{ |
| 54 |
Message: fmt.Sprintf("Running local shell %s", shellBin), |
| 55 |
}, |
| 56 |
} |
| 57 |
|
| 58 |
// Start the local shell command |
| 59 |
cmd := exec.Command(shellBin) |
| 60 |
stdin, err := cmd.StdinPipe() |
| 61 |
if err != nil { |
| 62 |
res.Err = errors.Annotatef(err, "getting stdin pipe") |
| 63 |
} |
| 64 |
stdout, err := cmd.StdoutPipe() |
| 65 |
if err != nil { |
| 66 |
res.Err = errors.Annotatef(err, "getting stdout pipe") |
| 67 |
} |
| 68 |
stderr, err := cmd.StderrPipe() |
| 69 |
if err != nil { |
| 70 |
res.Err = errors.Annotatef(err, "getting stderr pipe") |
| 71 |
} |
| 72 |
|
| 73 |
// Start the shell command |
| 74 |
err = cmd.Start() |
| 75 |
if err != nil { |
| 76 |
res.Err = errors.Annotatef(err, "starting shell") |
| 77 |
} |
| 78 |
|
| 79 |
res.Conn = &ShellConnLocal{stdin: stdin, stdout: stdout, stderr: stderr, cmd: cmd} |
| 80 |
|
| 81 |
return res |
| 82 |
} |
| 83 |
|
| 84 |
type ShellConnLocal struct { |
| 85 |
cmd *exec.Cmd |
| 86 |
|
| 87 |
stdin io.WriteCloser |
| 88 |
stdout io.Reader |
| 89 |
stderr io.Reader |
| 90 |
} |
| 91 |
|
| 92 |
func (s *ShellConnLocal) Stdin() io.Writer { |
| 93 |
return s.stdin |
| 94 |
} |
| 95 |
|
| 96 |
func (s *ShellConnLocal) Stdout() io.Reader { |
| 97 |
return s.stdout |
| 98 |
} |
| 99 |
|
| 100 |
func (s *ShellConnLocal) Stderr() io.Reader { |
| 101 |
return s.stderr |
| 102 |
} |
| 103 |
|
| 104 |
func (s *ShellConnLocal) Close() { |
| 105 |
s.stdin.Close() |
| 106 |
} |