1package mg 2 3import ( 4 "errors" 5 "fmt" 6) 7 8type fatalErr struct { 9 code int 10 error 11} 12 13func (f fatalErr) ExitStatus() int { 14 return f.code 15} 16 17type exitStatus interface { 18 ExitStatus() int 19} 20 21// Fatal returns an error that will cause mage to print out the 22// given args and exit with the given exit code. 23func Fatal(code int, args ...interface{}) error { 24 return fatalErr{ 25 code: code, 26 error: errors.New(fmt.Sprint(args...)), 27 } 28} 29 30// Fatalf returns an error that will cause mage to print out the 31// given message and exit with the given exit code. 32func Fatalf(code int, format string, args ...interface{}) error { 33 return fatalErr{ 34 code: code, 35 error: fmt.Errorf(format, args...), 36 } 37} 38 39// ExitStatus queries the error for an exit status. If the error is nil, it 40// returns 0. If the error does not implement ExitStatus() int, it returns 1. 41// Otherwise it retiurns the value from ExitStatus(). 42func ExitStatus(err error) int { 43 if err == nil { 44 return 0 45 } 46 exit, ok := err.(exitStatus) 47 if !ok { 48 return 1 49 } 50 return exit.ExitStatus() 51} 52