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

Formatter: expose CLI options to write formatting, for check mode and for recursive screening in repo #121

Open
thibaut-lo opened this issue Jan 8, 2024 · 3 comments

Comments

@thibaut-lo
Copy link

thibaut-lo commented Jan 8, 2024

Thanks for lot for making this util.

I have been using it to

  • format Dockerfiles in my git repo
  • check formatting and raise error if formatting differs from existing format
    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 . and black . 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).

make format-dockerfiles:
	@echo "Formatting Dockerfiles... "
	@cd .. && for file in $$( (git ls-files ; git ls-files --others --exclude-standard) | sort -u  | grep 'Dockerfile'); do \
		tmpFormattedDockerfile=$$(mktemp) ; \
		echo -n "Formatting $$file file... " ; \
		trap "rm -f $$tmpFormattedDockerfile" EXIT ; \
		dockerfile-utils format -t $$file > "$$tmpFormattedDockerfile" && mv "$$tmpFormattedDockerfile" $$file ; \
		echo OK && rm -f $$tmpFormattedDockerfile ; \
    done; echo Dockerfiles formatted
.PHONY: format-dockerfiles
check-dockerfiles: ## Check coding style
	@echo "Checking Dockerfiles... "
	@cd .. && for file in $$( (git ls-files ; git ls-files --others --exclude-standard) | sort -u  | grep 'Dockerfile'); do \
		tmpFormattedDockerfile=$$(mktemp) ; \
		trap "rm -f $$tmpFormattedDockerfile" EXIT ; \
		echo -n "Checking $$file file... " ; \
		dockerfile-utils format -t $$file > "$$tmpFormattedDockerfile" ; \
		if ! cmp $$file $$tmpFormattedDockerfile ; then \
			echo "Error: $$file is not properly formatted." ; \
			exit 1 ; \
		fi ; \
		echo OK && rm -f $$tmpFormattedDockerfile ; \
    done; echo Dockerfiles checked
.PHONY: check-dockerfiles
@rcjsuen
Copy link
Owner

rcjsuen commented Feb 13, 2024

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"?

@thibaut-lo
Copy link
Author

thibaut-lo commented Apr 17, 2024

Apologies for the delay in answering @rcjsuen, I missed your reply.

Would it still format anything? I guess not if it is just a "check"?

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.
We use it in CI, to ensure that pull requests / merge requests have formatted their code correctly.

@thibaut-lo
Copy link
Author

thibaut-lo commented Apr 17, 2024

In the meantime, I have made that small script to add that --check option. It would need to be generalized (I hardcode my configuration here, ie 4 spaces).

#!/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

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

No branches or pull requests

2 participants