【问题标题】:pre-commit on windows for terraform在 windows 上预先提交 terraform
【发布时间】:2020-10-16 09:14:06
【问题描述】:

尝试在 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,因为我不断在循环中收到错误。知道我缺少什么吗?

【问题讨论】:

    标签: windows terraform pre-commit pre-commit.com


    【解决方案1】:

    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 的作者

    【讨论】:

    • 感谢它在我删除 .在条目中,是否可以阻止``` repos:-repo:本地钩子:-id:terraform-fmt 名称:terraform fmt 描述:运行terraform fmt 条目:terraform fmt -recursive language:系统 pass_filenames:false ``` Ive尝试移动它 re:您关于提高性能的建议。你能澄清如何解决它吗?是的,你把它的链接好多了,但遗憾的是它不适用于 windows gruntworks :(
    【解决方案2】:

    您需要使用以下代码:

    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 可以使用环境变量调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 2013-02-07
      • 1970-01-01
      • 2021-06-28
      相关资源
      最近更新 更多