Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a step to CI to check files have been formatted with shfmt #1232

Merged
merged 5 commits into from Oct 10, 2023

Conversation

triarius
Copy link
Contributor

@triarius triarius commented Oct 8, 2023

To reduce loss of productivity due to debates about the formatting of shell scripts, I've added a formatting check to the CI that uses shfmt to detect script that are not formatted in accordance with it. This complements the existing checking of lints using shellcheck.

The options I've supplied to shfmt are:

  • --indent 2 to use 2 spaces for indentation. This matches our conventions for languages other than go.
  • --binary-next-line which allows binary operators onto the next line
# good
echo hello world \
  | grep hello
echo hello world | grep hello

# bad
echo hello world |
  grep hello

If we didn't specify this, the first good example will be formatted to be like the first bad example, which I consider to be less readable as it requires reading to the end of the first line to see what the binary operator is, and is confusing as it does not use a line continuation at all.

@triarius triarius marked this pull request as ready for review October 9, 2023 22:11
@@ -42,8 +42,7 @@ if ! docker run \
--pull=never \
--rm \
"tonistiigi/binfmt@${QEMU_BINFMT_DIGEST}" \
--install all
then
--install all; then
Copy link
Contributor Author

@triarius triarius Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit unfortunate, I would prefer if the formatter did not do this, but I don't consider it important enough to be fatal to the goal of having a formatter.

Luckily, to mitigate potential confusion between the end of the conditional and start of the body of the if statement, most text editors will highlight the then with coloured crayon technology.

@triarius triarius requested a review from a team October 9, 2023 22:30
Copy link
Contributor

@DrJosh9000 DrJosh9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥬

Copy link
Contributor

@moskyb moskyb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

@triarius triarius merged commit ea0d8bf into main Oct 10, 2023
1 check passed
@triarius triarius deleted the pdp-1794-add-shfmt-to-ci branch October 10, 2023 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants