Skip to content

Commit

Permalink
have remote.ExitError format errors and status
Browse files Browse the repository at this point in the history
Since all use cases of ExitStatus are just putting it into fmt.Errorf,
usually with the command string, have ExitStatus do that for the caller.
  • Loading branch information
jbardin committed Mar 23, 2018
1 parent 3fbdee0 commit ad8642e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 37 deletions.
3 changes: 0 additions & 3 deletions builtin/provisioners/chef/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,6 @@ func (p *provisioner) runCommand(o terraform.UIOutput, comm communicator.Communi
}

if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Command %q exited with non-zero exit status: %d", cmd.Command, rc)
}
return err
}

Expand Down
3 changes: 0 additions & 3 deletions builtin/provisioners/habitat/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,9 +756,6 @@ func (p *provisioner) runCommand(o terraform.UIOutput, comm communicator.Communi
}

if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Command %q exited with non-zero exit status: %d", cmd.Command, rc)
}
return err
}

Expand Down
5 changes: 1 addition & 4 deletions builtin/provisioners/remote-exec/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ func runScripts(
}

if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Script exited with non-zero exit status: %d", rc)
}
return fmt.Errorf("Remote command exited with error: %s", err)
return err
}

// Upload a blank follow up file in the same path to prevent residual
Expand Down
18 changes: 0 additions & 18 deletions builtin/provisioners/salt-masterless/resource_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ func applyFn(ctx context.Context) error {
}

if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Curl exited with non-zero exit status: %d", rc)
}
return err
}

Expand All @@ -186,9 +183,6 @@ func applyFn(ctx context.Context) error {
}

if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok {
return fmt.Errorf("install_salt.sh exited with non-zero exit status: %d", rc)
}
return err
}
}
Expand Down Expand Up @@ -273,9 +267,6 @@ func applyFn(ctx context.Context) error {
}

if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Script exited with non-zero exit status: %d", rc)
}
return err
}
return nil
Expand Down Expand Up @@ -340,9 +331,6 @@ func (p *provisioner) moveFile(o terraform.UIOutput, comm communicator.Communica
return fmt.Errorf("Unable to move %s to %s: %s", src, dst, err)
}
if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Unable to move %s to %s: exit status: %d", src, dst, rc)
}
return err
}
return nil
Expand All @@ -358,9 +346,6 @@ func (p *provisioner) createDir(o terraform.UIOutput, comm communicator.Communic
}

if err := cmd.Wait(); err != nil {
if _, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Non-zero exit status.")
}
return err
}
return nil
Expand All @@ -375,9 +360,6 @@ func (p *provisioner) removeDir(o terraform.UIOutput, comm communicator.Communic
return err
}
if err := cmd.Wait(); err != nil {
if _, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Non-zero exit status.")
}
return err
}
return nil
Expand Down
27 changes: 18 additions & 9 deletions communicator/remote/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,28 @@ func (c *Cmd) Wait() error {
c.Lock()
defer c.Unlock()

if c.err != nil {
return c.err
}

if c.exitStatus != 0 {
return ExitError(c.exitStatus)
if c.err != nil || c.exitStatus != 0 {
return &ExitError{
Command: c.Command,
ExitStatus: c.exitStatus,
Err: c.err,
}
}

return nil
}

type ExitError int
// ExitError is returned by Wait to indicate and error executing the remote
// command, or a non-zero exit status.
type ExitError struct {
Command string
ExitStatus int
Err error
}

func (e ExitError) Error() string {
return fmt.Sprintf("exit status: %d", e)
func (e *ExitError) Error() string {
if e.Err != nil {
return fmt.Sprintf("error executing %q: %v", e.Command, e.Err)
}
return fmt.Sprintf("%q exit status: %d", e.Command, e.ExitStatus)
}

0 comments on commit ad8642e

Please sign in to comment.