1
0

exit.go 773 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package utils
  2. import (
  3. "os"
  4. )
  5. func ExitByError(err error, code ...int) int {
  6. if err == nil {
  7. return ExitByErrorMsg("")
  8. } else {
  9. return ExitByErrorMsg(err.Error(), code...)
  10. }
  11. }
  12. func ExitByErrorMsg(msg string, code ...int) int {
  13. if len(msg) == 0 {
  14. msg = "exit: unknown error"
  15. }
  16. return ErrorExit(msg, code...)
  17. }
  18. func ErrorExit(msg string, code ...int) int {
  19. if len(msg) == 0 {
  20. SayGoodByef("%s", "Encountered an error, abnormal offline/shutdown.")
  21. } else {
  22. SayGoodByef("Encountered an error, abnormal offline/shutdown: %s\n", msg)
  23. }
  24. if len(code) == 1 && code[0] != 0 {
  25. return Exit(code[0])
  26. } else {
  27. return Exit(1)
  28. }
  29. }
  30. func Exit(code ...int) int {
  31. if len(code) == 1 {
  32. os.Exit(code[0])
  33. return code[0]
  34. } else {
  35. os.Exit(0)
  36. return 0
  37. }
  38. }