Browse code

validate: add shfmt

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>

Akihiro Suda authored on 2020/03/01 00:31:43
Showing 4 changed files
... ...
@@ -200,6 +200,15 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
200 200
     --mount=type=cache,target=/go/pkg/mod \
201 201
         PREFIX=/build ./install.sh $INSTALL_BINARY_NAME
202 202
 
203
+FROM base AS shfmt
204
+ENV INSTALL_BINARY_NAME=shfmt
205
+ARG SHFMT_COMMIT
206
+COPY hack/dockerfile/install/install.sh ./install.sh
207
+COPY hack/dockerfile/install/$INSTALL_BINARY_NAME.installer ./
208
+RUN --mount=type=cache,target=/root/.cache/go-build \
209
+    --mount=type=cache,target=/go/pkg/mod \
210
+        PREFIX=/build ./install.sh $INSTALL_BINARY_NAME
211
+
203 212
 FROM dev-base AS dockercli
204 213
 ENV INSTALL_BINARY_NAME=dockercli
205 214
 ARG DOCKERCLI_CHANNEL
... ...
@@ -304,6 +313,7 @@ COPY --from=criu          /build/ /usr/local/
304 304
 COPY --from=vndr          /build/ /usr/local/bin/
305 305
 COPY --from=gotestsum     /build/ /usr/local/bin/
306 306
 COPY --from=golangci_lint /build/ /usr/local/bin/
307
+COPY --from=shfmt         /build/ /usr/local/bin/
307 308
 COPY --from=runc          /build/ /usr/local/bin/
308 309
 COPY --from=containerd    /build/ /usr/local/bin/
309 310
 COPY --from=rootlesskit   /build/ /usr/local/bin/
310 311
new file mode 100755
... ...
@@ -0,0 +1,11 @@
0
+#!/bin/sh
1
+
2
+: "${SHFMT_COMMIT:=01725bdd30658db1fe1b9e02173c3060061fe86f}" # v3.0.2
3
+
4
+install_shfmt() {
5
+	echo "Install shfmt version $SHFMT_COMMIT"
6
+	git clone https://github.com/mvdan/sh.git "$GOPATH/src/github.com/mvdan/sh"
7
+	cd "$GOPATH/src/github.com/mvdan/sh" || exit 1
8
+	git checkout -q "$SHFMT_COMMIT"
9
+	GO111MODULE=on go build -buildmode=pie -v -o "${PREFIX}/shfmt" ./cmd/shfmt
10
+}
... ...
@@ -14,3 +14,4 @@ export SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
14 14
 . "${SCRIPTDIR}"/changelog-date-descending
15 15
 . "${SCRIPTDIR}"/deprecate-integration-cli
16 16
 . "${SCRIPTDIR}"/golangci-lint
17
+. "${SCRIPTDIR}"/shfmt
17 18
new file mode 100755
... ...
@@ -0,0 +1,13 @@
0
+#!/usr/bin/env bash
1
+set -e -o pipefail
2
+
3
+shfmtflags="-bn -ci -sr"
4
+# NOTE: `git grep '^#!'` may also pick up non-shell script files.
5
+# Add exceptional files to `egrep -v` if any false-positive is detected.
6
+if git grep --name-only '^#!' | egrep -v '(vendor|\.go|Jenkinsfile)' \
7
+	| xargs shfmt -d $shfmtflags; then
8
+	echo 'Congratulations! The shell scripts are properly formatted.'
9
+else
10
+	echo "Please reformat the shell scripts with \`shfmt -w ${shfmtflags}\`."
11
+	exit 1
12
+fi