2

尝试在 Windows 上启动并运行预提交,尝试一个简单的 terraform fmt 命令,但没有很多如何运行 exe 的示例,我有以下内容:

我的.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
    
    

但我从以下错误中得到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]

然后它看起来像它多次运行 terraform fmt,因为我不断在循环中收到错误。知道我缺少什么吗?

4

2 回答 2

0

您需要使用以下代码:

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

确保terraform.exe可以使用环境变量调用。

于 2021-12-20T16:57:02.773 回答
0

https://github.com/antonbabenko/pre-commit-terraform你可能会更好

也就是说,您可以使用以下我相信的方法来使您的示例正常工作:

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

请注意,我做了几件事:

  • pass_filenames: false-- 预提交通常通过将文件名传递给钩子来工作,这就是你的东西被多次调用的原因
  • 我删除了args(这是不必要的,只对远程存储库有帮助)并将其与entry

请注意,将其用作本地挂钩通常会比使用上面的存储库更糟糕,因为它始终会针对所有文件而不是仅针对您更改的文件运行(通常会慢得多!)


免责声明:我是 pre-commit 的作者

于 2020-06-27T01:09:23.443 回答