pre-commit 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/sh
  2. LATEST_STABLE_SUPPORTED_GO_VERSION="1.11"
  3. main() {
  4. if local_go_version_is_latest_stable
  5. then
  6. run_gofmt
  7. run_golint
  8. run_govet
  9. fi
  10. run_unit_tests
  11. }
  12. local_go_version_is_latest_stable() {
  13. go version | grep -q $LATEST_STABLE_SUPPORTED_GO_VERSION
  14. }
  15. log_error() {
  16. echo "$*" 1>&2
  17. }
  18. run_gofmt() {
  19. GOFMT_FILES=$(gofmt -l .)
  20. if [ -n "$GOFMT_FILES" ]
  21. then
  22. log_error "gofmt failed for the following files:
  23. $GOFMT_FILES
  24. please run 'gofmt -w .' on your changes before committing."
  25. exit 1
  26. fi
  27. }
  28. run_golint() {
  29. GOLINT_ERRORS=$(golint ./... | grep -v "Id should be")
  30. if [ -n "$GOLINT_ERRORS" ]
  31. then
  32. log_error "golint failed for the following reasons:
  33. $GOLINT_ERRORS
  34. please run 'golint ./...' on your changes before committing."
  35. exit 1
  36. fi
  37. }
  38. run_govet() {
  39. GOVET_ERRORS=$(go tool vet ./*.go 2>&1)
  40. if [ -n "$GOVET_ERRORS" ]
  41. then
  42. log_error "go vet failed for the following reasons:
  43. $GOVET_ERRORS
  44. please run 'go tool vet ./*.go' on your changes before committing."
  45. exit 1
  46. fi
  47. }
  48. run_unit_tests() {
  49. if [ -z "$NOTEST" ]
  50. then
  51. log_error 'Running short tests...'
  52. env AMQP_URL= go test -short
  53. fi
  54. }
  55. main