common shell provisioner: define a ValidExitCode func

This commit is contained in:
Adrien Delorme 2019-03-14 12:46:32 +01:00
parent 30a65c858a
commit f0a23bb81d
1 changed files with 39 additions and 0 deletions

39
common/shell/exit_code.go Normal file
View File

@ -0,0 +1,39 @@
package shell
import "fmt"
func (p *Provisioner) ValidExitCode(code int) error {
// Check exit code against allowed codes (likely just 0)
validCodes := p.ValidExitCodes
if len(validCodes) == 0 {
validCodes = []int{0}
}
validExitCode := false
for _, v := range validCodes {
if code == v {
validExitCode = true
break
}
}
if !validExitCode {
return &ErrorInvalidExitCode{
Code: code,
Allowed: validCodes,
}
}
return nil
}
type ErrorInvalidExitCode struct {
Code int
Allowed []int
}
func (e *ErrorInvalidExitCode) Error() string {
if e == nil {
return "<nil>"
}
return fmt.Sprintf("Script exited with non-zero exit status: %d."+
"Allowed exit codes are: %v",
e.Code, e.Allowed)
}