简体   繁体   中英

pre-commit on windows for terraform

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?

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

You need to use the following code:

repos:
- repo: local
  hooks:
    - id: terraform_fmt
      name: Terraform fmt
      description: Rewrite Terraform configuration files to a canonical format and style.
      entry: terraform
      args: ["fmt", "-recursive"]
      language: system
      files: (\.tf|\.tfvars)$
      exclude: \.terraform\/.*$
      pass_filenames: false

Make sure terraform.exe is callable using environment variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM