diff --git a/bin/lint b/bin/lint index 6bb262d1..3a42aea8 100755 --- a/bin/lint +++ b/bin/lint @@ -13,74 +13,69 @@ set -eu -EXIT_CODE=0 - function lint_go { - filename=$1 + filename="$1" + result=0 - if [ "$(gofmt -d $filename)" != "" ]; then - EXIT_CODE=1 - echo "$filename: run gofmt -w $filename!" + if [ -n "$(gofmt -l "${filename}")" ]; then + result=1 + echo "${filename}: run gofmt -w ${filename}!" fi - go tool vet ${filename} || EXIT_CODE=$? + go tool vet ${filename} || result=$? # golint is completely optional. If you don't like it # don't have it installed. - if [ "$(which golint)" != "" ]; then + if type golint >/dev/null 2>&1; then # golint doesn't set an exit code it seems - lintoutput=$(golint $filename) + lintoutput=$(golint "${filename}") if [ "$lintoutput" != "" ]; then + result=1 echo "$lintoutput" - EXIT_CODE=1 fi fi # gocyclo is completely optional. If you don't like it # don't have it installed. Also never blocks a commit, # it just warns. - if [ "$(which gocyclo)" != "" ]; then - gocyclo -over 25 ${filename} | while read line; do - echo ${filename}: higher than 25 cyclomatic complexity - ${line} + if type gocyclo >/dev/null 2>&1; then + gocyclo -over 25 "${filename}" | while read line; do + echo "${filename}": higher than 25 cyclomatic complexity - "${line}" done fi + + return $result } function lint { - filename=$1 - ext=${filename##*\.} - - # If this is a directory, then lint the files in it - if [[ -d ${filename} ]]; then - for filename in $(find ${filename} -type f); do - lint ${filename} - done - return - fi + filename="$1" + ext="${filename##*\.}" # Don't lint this script - if [ "${filename}" == "bin/lint" ]; then + if [ "${filename}" = "bin/lint" ]; then return fi case "$ext" in - go) lint_go $filename + go) lint_go "${filename}" ;; esac } -if [ $# -gt 0 ]; then - for filename in $@; do - lint ${filename} +function lint_files { + result=0 + while read filename; do + lint "${filename}" || result=1 done -else - for filename in $(git diff --cached --name-only); do - if [ ! -e "${filename}" ]; then - continue - fi + exit $result +} - lint ${filename} - done -fi +function list_files { + if [ $# -gt 0 ]; then + find "$@" -type f + else + git diff --cached --name-only + fi +} -exit $EXIT_CODE +list_files "$@" | lint_files