Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion sshwrapper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sshwrapper

import (
"context"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -152,7 +153,7 @@ func (s *SSHConn) CombinedOutput(cmd string, in io.Reader) ([]byte, error) {
// Run runs cmd on the remote host.
//
// See https://godoc.org/golang.org/x/crypto/ssh#Session.Run for details.
func (s *SSHConn) Run(cmd string, in io.Reader, outWriter, errWriter io.Writer) error {
func (s *SSHConn) RunContext(ctx context.Context, cmd string, in io.Reader, outWriter, errWriter io.Writer) error {
session, err := s.client.NewSession()
if err != nil {
return err
Expand All @@ -172,10 +173,29 @@ func (s *SSHConn) Run(cmd string, in io.Reader, outWriter, errWriter io.Writer)
session.Stdout = outWriter
session.Stderr = errWriter
session.Stdin = in

exit := make(chan struct{}, 1)
defer close(exit)

go func() {
select {
case <-ctx.Done():
if ctx.Err() != nil {
session.Signal(ssh.SIGINT)
session.Close()
}
case <-exit:
}
}()

err = session.Run(cmd)
return err
}

func (s *SSHConn) Run(cmd string, in io.Reader, outWriter, errWriter io.Writer) error {
return s.RunContext(context.Background(), cmd, in, outWriter, errWriter)
}

// SetEnvs specifies the environment that will be applied
// to any command executed by Output/CombinedOutput/Run.
func (s *SSHConn) SetEnvs(e map[string]string) {
Expand Down