Skip to content

Commit

Permalink
improve cmd errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Nov 10, 2020
1 parent 97f4670 commit 9055751
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

"github.com/pkg/errors"
"golang.org/x/crypto/ssh"
)

Expand All @@ -28,22 +29,34 @@ type Cmd struct {

// CombinedOutput runs cmd on the remote host and returns its combined stdout and stderr.
func (c *Cmd) CombinedOutput() ([]byte, error) {
return c.init().Session.CombinedOutput(c.String())
if err := c.init(); err != nil {
return nil, errors.Wrap(err, "cmd init")
}
return c.Session.CombinedOutput(c.String())
}

// Output runs cmd on the remote host and returns its stdout.
func (c *Cmd) Output() ([]byte, error) {
return c.init().Session.Output(c.String())
if err := c.init(); err != nil {
return nil, errors.Wrap(err, "cmd init")
}
return c.Session.Output(c.String())
}

// Run runs cmd on the remote host.
func (c *Cmd) Run() error {
return c.init().Session.Run(c.String())
if err := c.init(); err != nil {
return errors.Wrap(err, "cmd init")
}
return c.Session.Run(c.String())
}

// Start runs the command on the remote host.
func (c *Cmd) Start() error {
return c.init().Session.Start(c.String())
if err := c.init(); err != nil {
return errors.Wrap(err, "cmd init")
}
return c.Session.Start(c.String())
}

// String return the command line string.
Expand All @@ -52,14 +65,16 @@ func (c *Cmd) String() string {
}

// Init inits and sets session env vars.
func (c *Cmd) init() *Cmd {
func (c *Cmd) init() (err error) {

// Set session env vars
var env []string
for _, value := range c.Env {
env = strings.Split(value, "=")
c.Setenv(env[0], strings.Join(env[1:], "="))
if err = c.Setenv(env[0], strings.Join(env[1:], "=")); err != nil {
return
}
}

return c
return nil
}

0 comments on commit 9055751

Please sign in to comment.