have remote.ExitError format errors and status

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.
This commit is contained in:
James Bardin 2018-03-23 11:36:57 -04:00
parent 3fbdee0777
commit ad8642e2c2
5 changed files with 20 additions and 38 deletions

View File

@ -697,9 +697,6 @@ func (p *provisioner) runCommand(o terraform.UIOutput, comm communicator.Communi
} }
if err := cmd.Wait(); err != nil { 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 return err
} }

View File

@ -756,9 +756,6 @@ func (p *provisioner) runCommand(o terraform.UIOutput, comm communicator.Communi
} }
if err := cmd.Wait(); err != nil { 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 return err
} }

View File

@ -198,10 +198,7 @@ func runScripts(
} }
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
if rc, ok := err.(remote.ExitError); ok { return err
return fmt.Errorf("Script exited with non-zero exit status: %d", rc)
}
return fmt.Errorf("Remote command exited with error: %s", err)
} }
// Upload a blank follow up file in the same path to prevent residual // Upload a blank follow up file in the same path to prevent residual

View File

@ -161,9 +161,6 @@ func applyFn(ctx context.Context) error {
} }
if err := cmd.Wait(); err != nil { 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 return err
} }
@ -186,9 +183,6 @@ func applyFn(ctx context.Context) error {
} }
if err := cmd.Wait(); err != nil { 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 return err
} }
} }
@ -273,9 +267,6 @@ func applyFn(ctx context.Context) error {
} }
if err := cmd.Wait(); err != nil { 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 err
} }
return nil return nil
@ -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) return fmt.Errorf("Unable to move %s to %s: %s", src, dst, err)
} }
if err := cmd.Wait(); err != nil { 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 err
} }
return nil return nil
@ -358,9 +346,6 @@ func (p *provisioner) createDir(o terraform.UIOutput, comm communicator.Communic
} }
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
if _, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Non-zero exit status.")
}
return err return err
} }
return nil return nil
@ -375,9 +360,6 @@ func (p *provisioner) removeDir(o terraform.UIOutput, comm communicator.Communic
return err return err
} }
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
if _, ok := err.(remote.ExitError); ok {
return fmt.Errorf("Non-zero exit status.")
}
return err return err
} }
return nil return nil

View File

@ -69,19 +69,28 @@ func (c *Cmd) Wait() error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
if c.err != nil { if c.err != nil || c.exitStatus != 0 {
return c.err return &ExitError{
Command: c.Command,
ExitStatus: c.exitStatus,
Err: c.err,
} }
if c.exitStatus != 0 {
return ExitError(c.exitStatus)
} }
return nil return nil
} }
type ExitError int // ExitError is returned by Wait to indicate and error executing the remote
// command, or a non-zero exit status.
func (e ExitError) Error() string { type ExitError struct {
return fmt.Sprintf("exit status: %d", e) Command string
ExitStatus int
Err error
}
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)
} }