mirror of
https://github.com/magefile/mage.git
synced 2026-10-24 06:19:18 +03:00
* add tests * fix lint errors and align ci and lint workflows * update to require go 1.18 since golangci-lint requires it, and because it adds version control info into the binary automatically * use built-in version info from go 1.18 * remove gopath from install docs
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package mg
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
type fatalError struct {
|
|
error
|
|
|
|
code int
|
|
}
|
|
|
|
func (f fatalError) ExitStatus() int {
|
|
return f.code
|
|
}
|
|
|
|
type exitStatus interface {
|
|
ExitStatus() int
|
|
}
|
|
|
|
// Fatal returns an error that will cause mage to print out the
|
|
// given args and exit with the given exit code.
|
|
func Fatal(code int, args ...interface{}) error {
|
|
return fatalError{
|
|
code: code,
|
|
error: errors.New(fmt.Sprint(args...)),
|
|
}
|
|
}
|
|
|
|
// Fatalf returns an error that will cause mage to print out the
|
|
// given message and exit with the given exit code.
|
|
func Fatalf(code int, format string, args ...interface{}) error {
|
|
return fatalError{
|
|
code: code,
|
|
error: fmt.Errorf(format, args...),
|
|
}
|
|
}
|
|
|
|
// ExitStatus queries the error for an exit status. If the error is nil, it
|
|
// returns 0. If the error does not implement ExitStatus() int, it returns 1.
|
|
// Otherwise it returns the value from ExitStatus().
|
|
func ExitStatus(err error) int {
|
|
if err == nil {
|
|
return 0
|
|
}
|
|
var exit exitStatus
|
|
if errors.As(err, &exit) {
|
|
return exit.ExitStatus()
|
|
}
|
|
return 1
|
|
}
|