tvl-depot/tools/depotfmt.nix
Vincent Ambo 4c23a9c1d4 feat(depotfmt): run formatting checks in CI
Right now this only includes Go, but more is to come.

Change-Id: Idd8fc27c0eb25e82688ef8337ba20810d834f4b6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4504
Reviewed-by: zseri <zseri.devel@ytrizja.de>
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Autosubmit: tazjin <mail@tazj.in>
2021-12-24 19:26:00 +00:00

34 lines
1.1 KiB
Nix

# Builds treefmt for depot, with a hardcoded configuration that
# includes the right paths to formatters.
{ depot, pkgs, ... }:
let
config = pkgs.writeText "depot-treefmt-config" ''
[formatter.go]
command = "${pkgs.go}/bin/gofmt"
options = [ "-w" ]
includes = ["*.go"]
[formatter.tf]
command = "${pkgs.terraform}/bin/terraform"
options = [ "fmt" ]
includes = [ "*.tf" ]
'';
# helper tool for formatting the depot interactively
depotfmt = pkgs.writeShellScriptBin "depotfmt" ''
exec ${pkgs.treefmt}/bin/treefmt ''${@} \
--config-file ${config} \
--tree-root $(${pkgs.git}/bin/git rev-parse --show-toplevel)
'';
# wrapper for running formatting checks in CI
check = pkgs.runCommandNoCC "depotfmt-check" {} ''
${pkgs.git}/bin/git clone ${depot.path.origSrc} depot
export HOME="$(${pkgs.coreutils}/bin/realpath .)"
${pkgs.treefmt}/bin/treefmt \
--fail-on-change \
--config-file ${config} \
--tree-root depot && : > $out
'';
in depotfmt // depot.nix.readTree.drvTargets { inherit check; }