Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
Add shfmt formatting of shell files in lint
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Oct 25, 2016
1 parent 0a67594 commit 5570b0e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
7 changes: 6 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ dependencies:
- go install -tags netgo std
- mkdir -p $(dirname $SRCDIR)
- cp -r $(pwd)/ $SRCDIR
- go get github.com/golang/lint/golint github.com/fzipp/gocyclo github.com/kisielk/errcheck
- |
go get \
github.com/fzipp/gocyclo \
github.com/golang/lint/golint \
github.com/kisielk/errcheck \
github.com/mvdan/sh/cmd/shfmt
test:
override:
Expand Down
28 changes: 25 additions & 3 deletions lint
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/bin/bash
# This scipt lints go files for common errors.
# This scipt lints files for common errors.
#
# It runs gofmt and go vet, and optionally golint and
# For go files, it runs gofmt and go vet, and optionally golint and
# gocyclo, if they are installed.
#
# For shell files, it runs shfmt. If you don't have that installed, you can get
# it with:
# go get -u github.com/mvdan/sh/cmd/shfmt
#
# With no arguments, it lints the current files staged
# for git commit. Or you can pass it explicit filenames
# (or directories) and it will lint them.
Expand Down Expand Up @@ -91,7 +95,7 @@ function lint_go {

if [ -n "$(gofmt -s -l "${filename}")" ]; then
lint_result=1
echo "${filename}: run gofmt -s -w ${filename}!"
echo "${filename}: run gofmt -s -w ${filename}"
fi

go tool vet "${filename}" || lint_result=$?
Expand Down Expand Up @@ -123,6 +127,18 @@ function lint_go {
return $lint_result
}

function lint_sh {
filename="$1"
local lint_result=0

if [ -n "$(diff <(shfmt -i 4 "${filename}") "${filename}")" ]; then
lint_result=1
echo "${filename}: run shfmt -p -i 4 -w ${filename}"
fi

return $lint_result
}

function lint {
filename="$1"
ext="${filename##*\.}"
Expand All @@ -140,8 +156,14 @@ function lint {
coverage.html) return;;
esac

if [[ "$(file --mime-type "${filename}" | awk '{print $2}')" = "text/x-shellscript" ]]; then
ext="sh"
fi

case "$ext" in
go) lint_go "${filename}" || lint_result=1
;;
sh) lint_sh "${filename}" || lint_result=1
;;
esac

Expand Down

0 comments on commit 5570b0e

Please sign in to comment.