3

trying to get pre-commit up and running on windows, trying a simple terraform fmt command, but dont many examples of how to run exe piecing it together i have the below:

my .pre-commit-config.yaml

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.1.0  # Use the ref you want to point at
    hooks:
    - id: detect-aws-credentials
    - id: detect-private-key  

-   repo: local
    hooks:
    - id: terraform-fmt
      name: terraform fmt
      description: runs terraform fmt
      entry: terraform fmt 
      args: [-recursive]
      language: system
    
    

but im getting the error below from pre-commit run -a:

Detect AWS Credentials...................................................Passed
Detect Private Key.......................................................Passed
terraform fmt............................................................Failed
- hook id: terraform-fmt
- exit code: 1

The fmt command expects at most one argument.
Usage: terraform fmt [options] [DIR]

It then looks like its running terraform fmt multiple times as i keep getting the error in a loop. Any idea what im missing?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
Staggerlee011
  • 847
  • 2
  • 13
  • 23

1 Answers1

2

you may have better luck with https://github.com/antonbabenko/pre-commit-terraform

that said, you can get your example working by using the following I believe:

-   repo: local
    hooks:
    - id: terraform-fmt
      name: terraform fmt
      description: runs terraform fmt
      entry: terraform fmt -recursive
      language: system
      pass_filenames: false

note that I've done several things:

  • pass_filenames: false -- pre-commit normally works by passing filenames to hooks, this is why your thing is being invoked multiple times
  • I removed args (it's unnecessary and only really helpful for remote repositories) and combined it with entry

note that using this as a local hook will ~generally be worse than using the repository above because it will always run against all files instead of just the files you changed (usually making it much much slower!)


disclaimer: I'm the author of pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thanks it ran when i removed the . in entry, is it possible to block ``` repos: - repo: local hooks: - id: terraform-fmt name: terraform fmt description: runs terraform fmt entry: terraform fmt -recursive language: system pass_filenames: false ``` Ive tried moving it around re: your suggestion about improving the performance. Can you clarify how to fix it? and yes the link you put it a lot better, but sadly doesnt work for windows gruntworks to :( – Staggerlee011 Jun 27 '20 at 09:52