Add a gofmt check to CI

If we're going to start having folks writing Go code, it'll be good to
make sure that all the code is formatted. It means that folks should
never have formatting changes in their patch that's unrelated to what
they're actually trying to do.

Change-Id: I6cb657327dd4a238356b7ef38b9fb94446723ee5
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
diff --git a/tools/lint/run-ci.sh b/tools/lint/run-ci.sh
new file mode 100755
index 0000000..a1bc07c
--- /dev/null
+++ b/tools/lint/run-ci.sh
@@ -0,0 +1,49 @@
+#!/bin/bash
+
+# Perform runfile initialization here so that child processes have a proper
+# RUNFILES_DIR variable set.
+
+# --- begin runfiles.bash initialization v2 ---
+# Copy-pasted from the Bazel Bash runfiles library v2.
+set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
+source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
+  source "$0.runfiles/$f" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
+# --- end runfiles.bash initialization v2 ---
+
+gofmt() {
+    ./tools/lint/gofmt
+}
+
+git_status_is_clean() {
+    cd "${BUILD_WORKSPACE_DIRECTORY}"
+    if ! git diff --quiet; then
+        echo "One or more linters appear to have made changes to your code!" >&2
+        return 1
+    fi
+}
+
+# All the linters that we are going to run.
+readonly -a LINTERS=(
+    gofmt
+    git_status_is_clean  # This must the last linter.
+)
+
+failure=0
+for linter in "${LINTERS[@]}"; do
+    if ! (eval "${linter}"); then
+        failure=1
+    fi
+done
+
+if ((failure != 0)); then
+    echo "One or more linters failed." >&2
+    cd "${BUILD_WORKSPACE_DIRECTORY}"
+    git --no-pager diff || :
+    exit "${failure}"
+fi
+
+exit 0