Skip to content

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

@thibaut-lo

Description

@thibaut-lo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions