#!/bin/bash
# original source: https://raw.githubusercontent.com/streadway/amqp/master/pre-commit

GOFMT_OK=1
GOLINT_OK=1
GOVET_OK=1
STATICCHECK_OK=1
DEPCHECK_OK=1

# TODO: uncomment each of these tests during future pull-requests with
#       resolving code changes
main() {
    mv_vendor_out
    run_gofmt
    run_golint
    mv_vendor_in
    run_govet
    run_staticcheck
    check_results
}

log_error() {
  echo "$*" 1>&2
}

mv_vendor_out() {
    mv ./vendor/ /tmp/
}

mv_vendor_in() {
    mv /tmp/vendor ./
}

check_results() {
    if [[ "$GOFMT_OK" == "1" && "$GOLINT_OK" == "1" && "$GOVET_OK" == "1" && "$STATICCHECK_OK" == "1" && "$DEPCHECK_OK" == "1" ]]
    then
        echo "Formatting, Linting, Vetting, and Static Checks all returned OK."
        exit 0
    else
        echo "One of Formatting, Linting, Vetting, or Static Checks, or vendor check is invalid. Check logs for more information."
        exit 1
    fi
}

run_gofmt() {
  GOFMT_FILES=$(gofmt -l .)
  if [ -n "$GOFMT_FILES" ]
  then
    log_error "gofmt failed for the following files:
$GOFMT_FILES

please run 'gofmt -w .' on your changes before committing."
    GOFMT_OK=0
  fi
}

run_golint() {
  GOLINT_ERRORS=$(golint ./... | grep -v "Id should be")
  if [ -n "$GOLINT_ERRORS" ]
  then
    log_error "golint failed for the following reasons:
$GOLINT_ERRORS

please run 'golint ./...' on your changes before committing."
    GOLINT_OK=0
  fi
}

run_staticcheck() {
  STATIC_ERRORS=$(staticcheck ./...)
  if [ -n "${STATIC_ERRORS}" ]
  then
    log_error "staticcheck failed:
$STATIC_ERRORS

please run 'staticcheck ./...' on your changes before committing."
    STATICCHECK_OK=0
  fi
}

run_govet() {
  go mod download  # run this to avoid stdout causing invalid failure
  GOVET_ERRORS=$(go vet ./... 2>&1)
  if [ -n "$GOVET_ERRORS" ]
  then
    log_error "go vet failed for the following reasons:
$GOVET_ERRORS

please run 'go vet ./...' on your changes before committing."
    GOVET_OK=0
  fi
}

run_dep_check() {
  DEPCHECK_ERRORS=$(dep check)
  if [ -n "$DEPCHECK_ERRORS" ]
  then
    log_error "dep check failed for the following reasons:
$DEPCHECK_ERRORS

please run 'dep check' on your changes before committing."
    DEPCHECK_OK=0
  fi
}

main
# vim: set ft=sh:ts=2:sw=2:noai:
