-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add packer formatter that runs `packer fmt` on `*.pkr.hcl` and `*.pkrvars.hcl` files. * Update README "Adding new formatters" instructions to (a) remove mention of the deleted `./bors.toml.sh`, (b) consistently render file names in monospace, (c) replace `nix develop -c treefmt` with `nix fmt`, and (d) provide some additional guidance on how to test the newly-introduced formatter. --------- Co-authored-by: Jonas Chevalier <zimbatm@zimbatm.com>
- Loading branch information
Showing
3 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Example generated by ../examples.sh | ||
[formatter.packer] | ||
command = "treefmt-nix-packer-fmt-wrapper" | ||
excludes = [] | ||
includes = ["*.pkr.hcl", "*.pkrvars.hcl"] | ||
options = ["--"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ lib, pkgs, config, ... }: | ||
let | ||
cfg = config.programs.packer; | ||
|
||
# `packer fmt` accepts only a single template as an argument: | ||
# > $ packer fmt -h |& head -n 1 | ||
# > Usage: packer fmt [options] [TEMPLATE] | ||
# `packer fmt` supports recursively formatting all `*.pkr.hcl` and | ||
# `*.pkrvars.hcl` files in a specified directory, but since we want to | ||
# control the exact list of files-to-be-formatted, we instead have to invoke | ||
# `packer fmt` once per selected file. Consequently, we need a means of | ||
# distinguishing which arguments are options (from | ||
# `settings.formatter.packer.options`) and which are filenames; this is so | ||
# that we can pass all options to each `packer fmt`. The traditional `--` | ||
# sequence suits this purpose. | ||
command = pkgs.writers.writeBashBin "treefmt-nix-packer-fmt-wrapper" '' | ||
set -eu | ||
declare -a opts | ||
saw_separator=0 | ||
while (( "$#" > 0 )); do | ||
if [[ "$1" = -- ]]; then | ||
saw_separator=1 | ||
shift | ||
break | ||
fi | ||
opts+=("$1") | ||
shift | ||
done | ||
if [[ "$saw_separator" = 0 ]]; then | ||
echo 1>&2 "\ | ||
Error: ''${0##*/}: the end-of-options separator sequence is missing. | ||
Please ensure that the final entry in 'settings.formatter.packer.options' is a bare double-dash ('--'). | ||
Otherwise, this program cannot determine which arguments are options to 'packer fmt' and which are filenames." | ||
exit 1 | ||
fi | ||
rc=0 | ||
for tmpl in "$@"; do | ||
${lib.escapeShellArg cfg.package}/bin/packer fmt "''${opts[@]}" "$tmpl" || rc="$?" | ||
done | ||
exit "''${rc:-0}" | ||
''; | ||
in | ||
{ | ||
options.programs.packer = { | ||
enable = lib.mkEnableOption "packer"; | ||
package = lib.mkPackageOption pkgs "packer" { }; | ||
}; | ||
|
||
config = lib.mkIf cfg.enable { | ||
settings.formatter.packer = { | ||
inherit command; | ||
options = lib.mkAfter [ "--" ]; | ||
includes = [ "*.pkr.hcl" "*.pkrvars.hcl" ]; | ||
}; | ||
}; | ||
} |