lint: Add shfmt as a linter.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-10-14 19:56:18 -07:00 committed by Tim Abbott
parent dfaea9df65
commit 6099612a15
4 changed files with 29 additions and 3 deletions

View File

@ -3,15 +3,15 @@ root = true
[*] [*]
end_of_line = lf end_of_line = lf
charset = utf-8 charset = utf-8
indent_size = 4
indent_style = space indent_style = space
insert_final_newline = true insert_final_newline = true
trim_trailing_whitespace = true trim_trailing_whitespace = true
[*.{sh,py,pyi,xml,css,hbs,html}] binary_next_line = true # for shfmt
indent_size = 4 switch_case_indent = true # for shfmt
[{*.{js,json,ts},check-openapi}] [{*.{js,json,ts},check-openapi}]
indent_size = 4
max_line_length = 100 max_line_length = 100
[*.{py,pyi}] [*.{py,pyi}]

View File

@ -381,6 +381,8 @@ def main(options: argparse.Namespace) -> "NoReturn":
# Install shellcheck. # Install shellcheck.
run_as_root(["tools/setup/install-shellcheck"]) run_as_root(["tools/setup/install-shellcheck"])
# Install shfmt.
run_as_root(["tools/setup/install-shfmt"])
setup_venvs.main() setup_venvs.main()

View File

@ -75,6 +75,9 @@ def run() -> None:
"(zerver/openapi/zulip.yaml) ") "(zerver/openapi/zulip.yaml) ")
linter_config.external_linter('shellcheck', ['shellcheck', '-x', '-P', 'SCRIPTDIR'], ['sh'], linter_config.external_linter('shellcheck', ['shellcheck', '-x', '-P', 'SCRIPTDIR'], ['sh'],
description="Standard shell script linter") description="Standard shell script linter")
linter_config.external_linter('shfmt', ['shfmt'], ['sh'],
check_arg='-d', fix_arg='-w',
description="Formats shell scripts")
command = ['tools/run-mypy', '--quiet'] command = ['tools/run-mypy', '--quiet']
if args.force: if args.force:
command.append('--force') command.append('--force')

21
tools/setup/install-shfmt Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -eu
version=3.1.2
binary="shfmt_v${version}_linux_amd64"
sha256=c5794c1ac081f0028d60317454fe388068ab5af7740a83e393515170a7157dce
check_version() {
out="$(shfmt --version 2>/dev/null)" && [ "$out" = "v$version" ]
}
if ! check_version; then
tmpdir="$(mktemp -d)"
trap 'rm -r "$tmpdir"' EXIT
cd "$tmpdir"
wget -nv "https://github.com/mvdan/sh/releases/download/v3.1.2/$binary"
sha256sum -c <<<"$sha256 $binary"
chmod +x "$binary"
mv "$binary" /usr/local/bin/shfmt
check_version
fi