|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2019 Cortex Labs, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. >/dev/null && pwd)" |
| 21 | + |
| 22 | +if ! command -v golint >/dev/null 2>&1; then |
| 23 | + echo "golint must be installed" |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +if ! command -v gofmt >/dev/null 2>&1; then |
| 28 | + echo "gofmt must be installed" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +if ! command -v black >/dev/null 2>&1; then |
| 33 | + echo "black must be installed" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +go vet "$ROOT/..." |
| 38 | + |
| 39 | +output=$(golint "$ROOT/..." | grep -v "comment" || true) |
| 40 | +if [[ $output ]]; then |
| 41 | + echo "$output" |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +output=$(gofmt -s -l "$ROOT") |
| 46 | +if [[ $output ]]; then |
| 47 | + echo "go files not properly formatted:" |
| 48 | + echo "$output" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +output=$(black --quiet --diff --line-length=100 "$ROOT") |
| 53 | +if [[ $output ]]; then |
| 54 | + echo "python files not properly formatted:" |
| 55 | + echo "$output" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +# Check for trailing whitespace |
| 60 | +output=$(cd "$ROOT" && find . -type f \ |
| 61 | +! -path "./vendor/*" \ |
| 62 | +! -path "./bin/*" \ |
| 63 | +! -path "./.git/*" \ |
| 64 | +! -name ".*" \ |
| 65 | +-exec egrep -l " +$" {} \;) |
| 66 | +if [[ $output ]]; then |
| 67 | + echo "File(s) have lines with trailing whitespace:" |
| 68 | + echo "$output" |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +# Check for missing new line at end of file |
| 73 | +output=$(cd "$ROOT" && find . -type f \ |
| 74 | +! -path "./vendor/*" \ |
| 75 | +! -path "./bin/*" \ |
| 76 | +! -path "./.git/*" \ |
| 77 | +! -name ".*" \ |
| 78 | +-print0 | \ |
| 79 | +xargs -0 -L1 bash -c 'test "$(tail -c 1 "$0")" && echo "No new line at end of $0"' || true) |
| 80 | +if [[ $output ]]; then |
| 81 | + echo "$output" |
| 82 | + exit 1 |
| 83 | +fi |
| 84 | + |
| 85 | +# Check for multiple new lines at end of file |
| 86 | +output=$(cd "$ROOT" && find . -type f \ |
| 87 | +! -path "./vendor/*" \ |
| 88 | +! -path "./bin/*" \ |
| 89 | +! -path "./.git/*" \ |
| 90 | +! -name ".*" \ |
| 91 | +-print0 | \ |
| 92 | +xargs -0 -L1 bash -c 'test "$(tail -c 2 "$0")" || echo "Multiple new lines at end of $0"' || true) |
| 93 | +if [[ $output ]]; then |
| 94 | + echo "$output" |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +# Check for new line(s) at beginning of file |
| 99 | +output=$(cd "$ROOT" && find . -type f \ |
| 100 | +! -path "./vendor/*" \ |
| 101 | +! -path "./bin/*" \ |
| 102 | +! -path "./.git/*" \ |
| 103 | +! -name ".*" \ |
| 104 | +-print0 | \ |
| 105 | +xargs -0 -L1 bash -c 'test "$(head -c 1 "$0")" || echo "New line at beginning of $0"' || true) |
| 106 | +if [[ $output ]]; then |
| 107 | + echo "$output" |
| 108 | + exit 1 |
| 109 | +fi |
0 commit comments