1#!/bin/bash
2# Script that checks the code for errors.
3
4SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
5
6function print_real_go_files {
7    grep --files-without-match 'DO NOT EDIT!' $(find . -iname '*.go')
8}
9
10function check_no_documentation_changes {
11  echo "- Running generate-docs.sh"
12  output=$(./generate-docs.sh)
13  if [[ $? -ne 0 ]]; then
14    echo $output
15    echo "ERROR: Failed to generate documentation."
16    exit 1
17  fi
18
19  git diff --name-only | grep -q DOC.md
20  if [[ $? -ne 1 ]]; then
21    echo "ERROR: Documentation changes detected, please commit them."
22    exit 1
23  fi
24}
25
26function check_gofmt {
27    echo "- Running gofmt"
28    out=$(gofmt -l -w $(print_real_go_files))
29    if [[ ! -z $out ]]; then
30        echo "ERROR: gofmt changes detected, please commit them."
31        exit 1
32    fi
33}
34
35function goimports_all {
36    echo "- Running goimports"
37    ${GOPATH}/bin/goimports -l -w $(print_real_go_files)
38    if [[ $? -ne 0 ]]; then
39        echo "ERROR: goimports changes detected, please commit them."
40        exit 1
41    fi
42}
43
44function govet_all {
45    echo "- Running govet"
46    ret=0
47    for i in $(print_real_go_files); do
48        output=$(go tool vet -all=true -tests=false ${i})
49        ret=$(($ret | $?))
50        echo -n ${output}
51    done;
52    if [[ $ret -ne 0 ]]; then
53        echo "ERROR: govet errors detected, please commit/fix them."
54        exit 1
55    fi
56}
57
58check_no_documentation_changes
59check_gofmt
60goimports_all
61govet_all
62echo
63