Skip to content

Commit

Permalink
improve ssh command output
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 16, 2019
1 parent 2e746a9 commit 53a8dda
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 70 deletions.
8 changes: 4 additions & 4 deletions core/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (r *Runner) Run() error {
err = r.actionDownload(action.Node.(grammar.NodeUpload))
break
default:
err = errors.New(fmt.Sprintf("Invalid action `%s`", action.Key))
err = fmt.Errorf("invalid action `%s`", action.Key)
}

if err != nil {
Expand Down Expand Up @@ -387,7 +387,7 @@ func (r *Runner) actionRun(params grammar.NodeRun) error {

command = variable.Compile(command, r.variable)

if err := r.ssh.Run(command, ssh.Options{
if _, _, err := r.ssh.Run(command, ssh.Options{
CWD: r.cwdRemote,
Env: r.env,
}); err != nil {
Expand Down Expand Up @@ -482,7 +482,7 @@ func (r *Runner) actionVar(params grammar.NodeVar) error {
return err
}

b, err := r.ssh.RunAndCombineOutput(strings.Join(params.Command.Command, " "), ssh.Options{
stdout, _, err := r.ssh.Run(strings.Join(params.Command.Command, " "), ssh.Options{
CWD: r.cwdRemote,
Env: r.env,
})
Expand All @@ -491,7 +491,7 @@ func (r *Runner) actionVar(params grammar.NodeVar) error {
return err
}

output := string(b)
output := stdout.String()

r.variable[params.Key] = strings.TrimSpace(output)
}
Expand Down
89 changes: 23 additions & 66 deletions core/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ import (
"golang.org/x/crypto/ssh"
)

type Writer struct {
output io.Writer
data *bytes.Buffer
}

func (w Writer) Write(p []byte) (n int, err error) {
if w.data != nil {
if n, err := w.data.Write(p); err != nil {
return n, err
}
}

return w.output.Write(p)
}

type Client struct {
sshClient *ssh.Client
sftpClient *sftp.Client
Expand Down Expand Up @@ -168,37 +183,21 @@ func (c *Client) Env(key string, options Options) (string, error) {
return strings.TrimSpace(stdoutBuf.String()), nil
}

func (c *Client) RunAndCombineOutput(command string, options Options) ([]byte, error) {
// Create a session. It is one session per command.
session, err := c.sshClient.NewSession()

if err != nil {
return nil, err
}

defer session.Close()

if options.CWD != "" {
command = "cd " + options.CWD + " && " + command
}

command = setEnvForCommand(command, options.Env)

return session.CombinedOutput(command)
}
func (c *Client) Run(command string, options Options) (stdout *bytes.Buffer, stderr *bytes.Buffer, err error) {
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)

func (c *Client) RunWithCustomIO(command string, options Options, stdout *bytes.Buffer, stderr *bytes.Buffer) error {
// Create a session. It is one session per command.
session, err := c.sshClient.NewSession()

if err != nil {
return err
return
}

defer session.Close()

session.Stdout = stdout
session.Stderr = stderr
session.Stdout = Writer{output: os.Stdout, data: stdout}
session.Stderr = Writer{output: os.Stderr, data: stderr}

if options.CWD != "" {
command = "cd " + options.CWD + " && " + command
Expand All @@ -207,52 +206,10 @@ func (c *Client) RunWithCustomIO(command string, options Options, stdout *bytes.
command = setEnvForCommand(command, options.Env)

if err = session.Run(command); err != nil {
return err
return
}

return nil
}

func (c *Client) Run(command string, options Options) error {
// Create a session. It is one session per command.
session, err := c.sshClient.NewSession()

if err != nil {
return err
}

defer session.Close()

if options.CWD != "" {
command = "cd " + options.CWD + " && " + command
}

command = setEnvForCommand(command, options.Env)

sessionStdOut, err := session.StdoutPipe()

if err != nil {
return err
}

sessionStdErr, err := session.StderrPipe()

if err != nil {
return err
}

if err = session.Run(command); err != nil {
return err
}

if _, err := io.Copy(os.Stdout, sessionStdOut); err != nil {
return err
}
if _, err := io.Copy(os.Stderr, sessionStdErr); err != nil {
return err
}

return nil
return
}

func (c *Client) downloadFile(remoteFilePath string, localDir string) error {
Expand Down

0 comments on commit 53a8dda

Please sign in to comment.