-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
Formatter: expose CLI options to write formatting, for check mode and for recursive screening in repo #121
Comments
Hi @thibaut-lo, thank you for your interest in this project. Regarding "checking", so you mean if you specified "spaces" but "tabs" were found the program would return a non-zero return code? Would it still format anything? I guess not if it is just a "check"? |
Apologies for the delay in answering @rcjsuen, I missed your reply.
You're right, this check option doesn't perform formatting, it just check that the current formatting complies (ie no change would be applied if formatting was done). Otherwise, it should fail, return a non-zero code. |
In the meantime, I have made that small script to add that #!/bin/bash
#
# Format all Dockerfiles in the current git repo using dockerfile-utils formatter tool.
# Check formatting only if argument --check is passed.
#
# Usage: format-dockerfiles.sh [--check]
set -e
# Check if git is installed
if ! command -v git &>/dev/null; then
echo "git could not be found. Please install git and try again."
exit 1
fi
root_dir=$(git rev-parse --show-toplevel)
check_only=false
if [[ $1 == "--check" ]]; then
check_only=true
fi
if $check_only; then
echo "Checking format of Dockerfiles with dockerfile-utils..."
else
echo "Formatting Dockerfiles with dockerfile-utils"
fi
for file in $( (
cd "$root_dir" && git ls-files
cd "$root_dir" && git ls-files --others --exclude-standard # new uncommitted files
) | sort -u | grep 'Dockerfile'); do
tmpFormattedDockerfile=$(mktemp)
trap 'rm -f $tmpFormattedDockerfile' EXIT
dockerfile-utils format --spaces 4 "$root_dir"/"$file" >"$tmpFormattedDockerfile"
if $check_only; then
if ! cmp "$root_dir"/"$file" "$tmpFormattedDockerfile"; then
echo "Error: $file is not properly formatted."
echo "(hint) fix with \`make format\`"
exit 1
fi
else
mv "$tmpFormattedDockerfile" "$root_dir"/"$file"
fi
rm -f "$tmpFormattedDockerfile"
done
if $check_only; then
echo "Dockerfiles format checked"
else
echo "Dockerfiles formatted"
fi |
Thanks for lot for making this util.
I have been using it to
in CI and with a shell util in dev environment that is "agnostic" (a task runner (Makefile here) not linked to vscode for example).
That would be cool if
dockerfile-utils format
exposes CLI options to match these use case.A bit like
black --check --diff .
andblack .
for another well-known formatted (python here).It could be
dockerfile-utils format --write --recursive .
(-w
for write and-r
for recursively find Dockerfiles, with maybe an optional--regex
that defaults to "Dockerfile")and
dockerfile-utils format --check --recursive .
to raise errors if the formatting differs.The current syntax is a bit cumbersome (below).
The text was updated successfully, but these errors were encountered: