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

fix(programs): prevent yamlfmt from modifying unchanged files #167

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/formatter-yamlfmt.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Example generated by ../examples.sh
[formatter.yamlfmt]
command = "yamlfmt"
command = "yamlfmt-forall"
excludes = []
includes = ["*.yaml", "*.yml"]
options = []
28 changes: 27 additions & 1 deletion programs/yamlfmt.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
{ lib, pkgs, config, ... }:
let
cfg = config.programs.yamlfmt;
# Run the given program with the given argument (filepath), while expecting
# the program to *always* write back to the filepath. The program takes
# -in for alternative input.
#
# The resultant script behaves the same except for avoiding writing to file if
# the output hasn't changed.
modify-unless-unchanged = pkgs.writeShellScriptBin "modify-unless-unchanged"
''
PROG=$1
FILEPATH=$2
OUTPUT=$($PROG -in < "$FILEPATH")
echo "$OUTPUT" | diff -q "$FILEPATH" - > /dev/null || echo "$OUTPUT" > "$FILEPATH"
'';
in
{
options.programs.yamlfmt = {
Expand All @@ -10,7 +23,20 @@ in

config = lib.mkIf cfg.enable {
settings.formatter.yamlfmt = {
command = cfg.package;
command = pkgs.writeShellApplication {
name = "yamlfmt-forall";
runtimeInputs = [
cfg.package
modify-unless-unchanged
];
text = ''
for file in "$@"; do
# Use the modify-unless-unchanged hack until this is implemented:
# https://github.com/google/yamlfmt/issues/163
modify-unless-unchanged yamlfmt "$file"
done
'';
};
includes = [ "*.yaml" "*.yml" ];
};
};
Expand Down