From f0a23bb81df3122f3ef48a5fd427d90e2dbad35a Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 14 Mar 2019 12:46:32 +0100 Subject: [PATCH] common shell provisioner: define a ValidExitCode func --- common/shell/exit_code.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 common/shell/exit_code.go diff --git a/common/shell/exit_code.go b/common/shell/exit_code.go new file mode 100644 index 000000000..815e92f88 --- /dev/null +++ b/common/shell/exit_code.go @@ -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 "" + } + return fmt.Sprintf("Script exited with non-zero exit status: %d."+ + "Allowed exit codes are: %v", + e.Code, e.Allowed) +}