Code Monkey home page Code Monkey logo

pre-commit's Introduction

Maintained by Gruntwork.io

Pre-commit hooks

This repo defines Git pre-commit hooks intended for use with pre-commit. The currently supported hooks are:

  • terraform-fmt: Automatically run terraform fmt on all Terraform code (*.tf files).
  • terraform-validate: Automatically run terraform validate on all Terraform code (*.tf files).
  • packer-validate: Automatically run packer validate on all Packer code (*.pkr.* files).
  • terragrunt-hclfmt: Automatically run terragrunt hclfmt on all Terragrunt configurations.
  • tflint: Automatically run tflint on all Terraform code (*.tf files).
  • shellcheck: Run shellcheck to lint files that contain a bash shebang.
  • gofmt: Automatically run gofmt on all Golang code (*.go files).
  • goimports: Automatically run goimports on all Golang code (*.go files).
  • golint: Automatically run golint on all Golang code (*.go files). [DEPRECATED]: Please use golangci-lint below.
  • golangci-lint: Automatically run golangci-lint on all Golang code (*.go files).
  • yapf: Automatically run yapf on all python code (*.py files).
  • helmlint Automatically run helm lint on your Helm chart files. See caveats here.
  • markdown-link-check Automatically run markdown-link-check on markdown doc files.
  • sentinel-fmt: Automatically run sentinel fmt on all Sentinel code (*.sentinel.* files).

General Usage

In each of your repos, add a file called .pre-commit-config.yaml with the following contents:

repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: <VERSION> # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases
    hooks:
      - id: terraform-fmt
      - id: terraform-validate
      - id: tflint
      - id: shellcheck
      - id: gofmt
      - id: golint

Next, have every developer: 

  1. Install pre-commit. E.g. brew install pre-commit.
  2. Run pre-commit install in the repo.

That’s it! Now every time you commit a code change (.tf file), the hooks in the hooks: config will execute.

Running Against All Files At Once

Example: Formatting all files

If you'd like to format all of your code at once (rather than one file at a time), you can run:

pre-commit run terraform-fmt --all-files

Example: Enforcing in CI

If you'd like to enforce all your hooks, you can configure your CI build to fail if the code doesn't pass checks by adding the following to your build scripts:

pip install pre-commit
pre-commit install
pre-commit run --all-files

If all the hooks pass, the last command will exit with an exit code of 0. If any of the hooks make changes (e.g., because files are not formatted), the last command will exit with a code of 1, causing the build to fail.

Helm Lint Caveats

Detecting charts

The helmlint pre-commit hook runs helm lint on the charts that have been changed by the commit. It will run once per changed chart that it detects.

Note that charts are detected by walking up the directory tree of the changed file and looking for a Chart.yaml file that exists on the path.

linter_values.yaml

helm lint requires input values to look for configuration errors in your helm chart. However, this means that the linter needs a complete values file. Because we want to develop charts that define required values that the operator should provide, we don't want to specify defaults for all the values the chart expects in the default values.yaml file.

Therefore, to support this, this pre-commit hook looks for a special linter_values.yaml file defined in the chart path. This will be combined with the values.yaml file before running helm lint. In your charts, you should define the required values in linter_values.yaml.

For example, suppose you had a helm chart that defined two input values: containerImage and containerTag. Suppose that your chart required containerImage to be defined, but not containerTag. To enforce this, you created the following values.yaml file for your chart:

# values.yaml

# containerImage is required and defines which image to use

# containerTag specifies the image tag to use. Defaults to latest.
containerTag: latest

If you run helm lint on this chart, it will fail because somewhere in your chart you will reference .Values.containerImage which will be undefined with this values.yaml file. To handle this, you can define a linter_values.yaml file that defines containerImage:

# linter_values.yaml
containerImage: nginx

Now when the pre-commit hook runs, it will call helm lint with both linter_values.yaml and values.yaml:

helm lint -f values.yaml -f linter_values.yaml .

Shellcheck Arguments

To enable optional shellcheck features you can use the --enable flag. Other shellcheck flags can not be passed through.

repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: <VERSION>
    hooks:
      - id: shellcheck
        args: ["--enable require-variable-braces,deprecate-which"]

tflint Caveats

Using the --config argument

With the introduction of --chdir into tflint, the --config argument is now bound to whatever subdirectory you are running the check against. For mono-repos this isn't ideal as you may have a central configuration file you'd like to use. If this matches your use-case, you can specify the placeholder __GIT_ROOT__ value in the --config argument that will evaluate to the root of the repository you are in.

repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: <VERSION>
    hooks:
    - id: tflint
      args:
        - "--config=__GIT_ROOT__/.tflint.hcl"

Changing the placeholder value

You can change the value of the placeholder by populating the PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD environment variable.

export PRECOMMIT_TFLINT_REPO_ROOT_KEYWORD=__foo__

cat <<EOF > .pre-commit-config.yaml
---
repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: v0.1.22
    hooks:
    - id: terragrunt-hclfmt
    - id: tflint
      args:
        - "--config=__foo__/.tflint.hcl"
EOF

pre-commit run

License

This code is released under the Apache 2.0 License. Please see LICENSE and NOTICE for more details.

Copyright © 2019 Gruntwork, Inc.

pre-commit's People

Contributors

06kellyjac avatar alain-odea-sequence avatar alainodea avatar alias-dev avatar brikis98 avatar brycelowe avatar davidalger avatar eak12913 avatar ellisonc avatar endrec avatar infraredgirl avatar joshschmitter avatar jottr avatar macbutch avatar mcalhoun avatar msgongora avatar nilsonneto avatar oasys avatar parkalla86 avatar pauloconnor avatar queglay avatar robmorgan avatar shmileee avatar sirosen avatar stigkj avatar thepoppingone avatar tpdownes avatar wolverinefan avatar yorinasub17 avatar zackproser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pre-commit's Issues

Add sentinel fmt hook enhancement

Describe the solution you'd like
Add a pre-commit hook for running sentinel fmt, similar to the hook for running terraform fmt

Describe alternatives you've considered
NA

Additional context
NA

Markdown link check pre-commit hook doesn't allow user-defined configuration

Describe the bug
Unable to provide my own configuration to markdown-link-check via pre-commit

To Reproduce

Expected behavior
I added the markdown-link-check hook to my .pre-commit-config.yaml, and ran pre-commit run -a but found that private links I'm trying to check give a 503. For my case, I am trying to check markdown in a private Gitlab repo. I found I can add a cookie header to the config in order to grant the tool access to check these links if I run markdown-link-check -c config.json README.md but if try to do it by adding -c config.json to the args as below in .pre-commit-config.yaml, then markdown-link-check fails to run properly and throws an error about the configuration:

  - id: markdown-link-check
    args:
    - -c config.json

So I did some checking and I found the script creates its own temp config and provides the -c flag itself to markdown-link-check, which seems to conflict with my -c flag in the args.

Nice to have

  • Terminal output
  • Screenshots

Additional context
Add any other context about the problem here.

terraform validate pre-commit hook fails for modules

Dear Team,

I think your pre-commit-collection is really great. I would like to use also the terraform validate pre-commit hook and this fails for plain vanilla terraform modules (without terragrunt), because e.g. arguments are not set (because it is a module).
Could we suppress tf validate if it is a module, meaning no backend definition is there or no provider is present?

Trying to set chdir so only scans certain folders is not working

Describe the bug
I want to be able to scan folders. I have tried these settings. All of these settings fail to scan JUST the folder I set:

#EXAMPLE 1

#EXAMPLE 2

#EXAMPLE 3

#EXAMPLE 4
#This fails as well

To Reproduce
Terraform version 1.3.9

// paste code snippets here
To Trip the linter I added this in my IaC/environments/1-test folder

variable "not_used" {
  description = "To trip linters"
  type        = string
  default     = null
}

tflint...................................................................Failed
- hook id: tflint
- exit code: 2

1 issue(s) found:

Warning: variable "not_used" is declared but not used (terraform_unused_declarations)

  on IaC/environments/1-test/compute-priv-variables.tf line 183:
 183: variable "not_used" {

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.2.2/docs/rules/terraform_unused_declarations.md

**Expected behavior**
Since I am setting a chdir argument to scan the IaC/environments/2-staging/ folder (I tried without the trailing slash, fails as well)

I would NOT expect the linter to find the issue in the IaC/environments/1-test folder.

I would expect no errors reported

**Nice to have**
- [ ] Terminal output
- [ ] Screenshots

**Additional context**
I want to be able to set different exclusion per environment.  I want production to have all rules applied with lower environments I want the ability to exclude rules.  I cannot do that, since chdir is not being honored.  I submitted this bug in the tflint repo, they it was not their issue.

Fix `tflint` deprecation warning - "tflint FILE/DIR" is deprecated

Describe the solution you'd like
I don't want to see these warnings

WARNING: "tflint FILE/DIR" is deprecated and will error in a future version. Use --chdir or --filter instead.

Describe alternatives you've considered
Stop using gruntwork-io/pre-commit. Don't really want to go there.
Downgrade tflint. Don't want to do that either.

Additional context
tflint release notes on deprecation - https://github.com/terraform-linters/tflint/releases/tag/v0.46.0
Tested with gruntwork-io/pre-commit v0.1.19

OpenTofu Support

Describe the solution you'd like

As a founding member of OpenTofu, it would be advantageous for Gruntworks very useful pre-commit hooks to support it.

Describe alternatives you've considered

No. It seems like a no-brainer.

Additional context

I have implemented this in #107

terraform hooks fail on windows with git bash

We are currently integrating the pre-commit hooks into our projects.
We are mainly setup on Windows systems.
Using git bash, we found that "/bin/bash" could not be found.
If we change the shebang to "#!/usr/bin/env bash" in the shell scripts (fmt, validate, lint), then we can use the scripts normally.
Is it possible to adapt the terraform-based hooks with the more compatible shebang?
You have already made the adjustment for shellcheck.sh.

Many Thanks
Thorsten

.tflint.hcl is rules are ignored

Describe the bug
We utilize a monorepo for our terraform configurations and we have integrated into our CI pipline precommit checks with the tflint precommit plugin. What I've noticed is that ever since the release of chdir my .tflint.hcl file is no longer effective. It looks like because tflint is actually changing the current working directory (https://github.com/terraform-linters/tflint/blob/0e58cd19e334b6c1edfc4ba6d9f4778a28a3342e/cmd/inspect.go#L35C20-L35C20 and https://github.com/terraform-linters/tflint/blob/0e58cd19e334b6c1edfc4ba6d9f4778a28a3342e/cmd/cli.go#L178) and then loading the config file (https://github.com/terraform-linters/tflint/blob/0e58cd19e334b6c1edfc4ba6d9f4778a28a3342e/cmd/inspect.go#L110). The result is tflint failing on rules that I've called out as disabled in my base .tflint.hcl file.

To Reproduce

mkdir -pv tflint-demo/module
pushd tflint-demo >/dev/null
git init

cat <<EOF > .pre-commit-config.yaml
---
repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: v0.1.22
    hooks:
    - id: terragrunt-hclfmt
    - id: tflint
      args:
        - "--config=.tflint.hcl"
EOF

cat <<EOF > .tflint.hcl
rule "terraform_required_providers" {
  enabled = false
}
EOF

pushd module >/dev/null

cat <<EOF > main.tf
resource "null_resource" "foo" {
  triggers = {
    "coffee" = "sandwich"
  }
}
EOF

popd >/dev/null

git add .
pre-commit install
pre-commit run

Expected behavior
I'd expect the .tflint.hcl file to be honored from the repo root directory like it used to, but the functionality of tflint has changed.

Nice to have

  • Terminal output
➜  project-scratch-space mkdir -pv tflint-demo/module
pushd tflint-demo >/dev/null
git init

cat <<EOF > .pre-commit-config.yaml
---
repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: v0.1.22
    hooks:
    - id: terragrunt-hclfmt
    - id: tflint
      args:
        - "--config=.tflint.hcl"
EOF

cat <<EOF > .tflint.hcl
rule "terraform_required_providers" {
  enabled = false
}
EOF

pushd module >/dev/null

cat <<EOF > main.tf
resource "null_resource" "foo" {
  triggers = {
    "coffee" = "sandwich"
  }
}
EOF

popd >/dev/null

git add .
pre-commit install
pre-commit run
tflint-demo
tflint-demo/module
Initialized empty Git repository in /Users/blowe/project-scratch-space/tflint-demo/.git/
pre-commit installed at .git/hooks/pre-commit
Terragrunt hclfmt........................................................Passed
tflint...................................................................Failed
- hook id: tflint
- exit code: 2

1 issue(s) found:

Warning: Missing version constraint for provider "null" in `required_providers` (terraform_required_providers)

  on module/main.tf line 1:
   1: resource "null_resource" "foo" {

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.4.0/docs/rules/terraform_required_providers.md
  • Screenshots
image

Additional context
I realize this isn't a gruntwork problem pre-se, but there are other pre-commit hooks that do handle this.

"terraform-validate", "terraform-fmt" fails with "command not found" in Linux-based systems due to ^M carriage return character

Describe the bug
"pre-commit run terraform-validate --all-files" command fails until I follow Remove the spurious CR characters steps to use sed to remove "The ^M is a carriage return character":
Screen Shot 2021-12-31 at 15 42 29

To Reproduce
Steps to reproduce the behavior including the relevant Terraform/Terragrunt/Packer version number and any code snippets and module inputs you used.

  1. Install pre-commit in Mac OS or some linux flavor OS (Ubuntu, etc.) & create a .pre-commit-config.yaml as defined in Pre-commit README.md > General Usage
  2. Run pre-commit run terraform-validate --all-files
  3. See failures, now run sed -i -e 's/\r$//' /Users/arriaga/.cache/pre-commit/repo57cjvft5/hooks/terraform-validate.sh
  4. Re-try pre-commit run terraform-validate --all-files and see how errors g oaway

Expected behavior
Maybe pre-commit hooks should avoid having the "^M" carriage return character?

Additional context
Tried to follow "Step 3 - Static Analysis" steps of Getting Started with Terraform on Azure: Testing blog article

Allow specifying arbitrary args for shellcheck

Unfortunately args currently only allows --enable.
This is problematic when other shellcheck options are necesarry.

Concrete use-case:
When sourcing files in scripts shellcheck requires the --external-sources / -x option to follow these. Otherwise it will fail with a SC1091 error.

Please enable passing all options through to shellcheck.

Support for `--strict` in helmlint hook

Describe the solution you'd like
helm supports the --strict option for failing on warnings when linting. It would be nice if the pre-commit hook also supported this. I've tried messing around with it but it looks like the only way to support this is a change in the helmlint.sh file.

Describe alternatives you've considered
Not sure if there are any alternatives, as the helm command is called directly in the script

Additional context
Add any other context or screenshots about the feature request here.

validate packer

I thought it could be handy to be able to validate packer files. Would you accept a PR for this?

`gofmt` check fails if `go.mod` and `go.sum` are not in the root directory

Describe the bug
The gofmt check fails if Go code is stored in a subdirectory of the repository:

gofmt....................................................................Failed
- hook id: gofmt
- exit code: 1

go: cannot find main module, but found .git/config in /Users/gchappel/code/sample-directory
	to create a module there, run:
	go mod init

To Reproduce

  • Create an empty directory
  • git init .
  • Configure .pre-commit-config.yaml with the Gruntwork pre-commit repo and enable the gofmt hook
  • Create a subdirectory
  • In this subdirectory run go mod init test
  • In the subdirectory write a deliberately-malformed Go file, for example using spaces instead of tabs:
package main

import (
  "fmt" // this line starts with two spaces
)

func main() {
  fmt.Println("test")  // this line starts with two spaces
}
  • git add . to add the Go code and the .pre-commit-config.yaml to the staging area
  • run pre-commit run gofmt
  • see error

Expected behavior
The malformed Go file has its spaces replaced with tabs

Nice to have

  • Terminal output
  • Screenshots

Additional context
Add any other context about the problem here.

terragrunt validate - issues with root terragrunt.hcl

Failures in root terragrunt.hcl terragrunt validate.
Consider the following dir structure

├── dev
│   └── eu-west-1
│       └── project
│           └── terragrunt.hcl
└── terragrunt.hcl
└── common.hcl

Where the child includes the parent via find_in_parent_folders().

If the parent has many read_terraform_config(find_in_parent_folders("common.hcl")) type includes it will fail when terragrunt validate runs against it in singular. As the dir traversal fails to find the file.

Anything that can be done about this?

Add packer fmt hook

Describe the solution you'd like
Add a pre-commit hook for running packer fmt, similar to the hook for running terraform fmt

Describe alternatives you've considered
NA

Additional context
NA

Please add an option to download/install terraform

Describe the solution you'd like
I would like to be able to use terraform-fmt on pre-commit.ci, the hosted CI pre-commit service, but unfortunately I cannot because terraform doesn't come pre-installed, and the hooks here do not install/bundle it.

Terraform fmt............................................................Failed
- hook id: terraform-fmt
- exit code: 127

/pc/clone/RKgVfIZxQCiSAX665qKF9Q/hooks/terraform-fmt.sh: line 15: terraform: command not found

It'd be nice if there was a way to say that you need terraform installed as part of the hook.

Describe alternatives you've considered
Not running terraform hooks on pre-commit.ci.

tflint support for cloud block in terraform

Describe the solution you'd like
Need support for the cloud stanza in the terraform block. See https://www.terraform.io/cli/cloud/settings Currently it errors out saying cloud is an "Unsupported block type" so we have to disable tflint

Describe alternatives you've considered
There are no alternatives, other than disabling tflint

Additional context
This is relatively new syntax from HashiCorp, but it does need to be addressed.

terraform-validate breaks after 0.1.5

Execution of hook in 0.1.5

Terraform validate.......................................................Failed
- hook id: terraform-validate
- exit code: 1

Initializing modules...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "null" (hashicorp/null) 2.1.2...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.65.0...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.null: version = "~> 2.1"
* provider.template: version = "~> 2.1"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Error: Error in function call

  on terraform/modules/nsqd/data.tf line 2, in data "template_file" "opt_nsqd_etc_nsqd_config":
   2:   template = file(
   3: 
   4: 
    |----------------
    | path.module is "terraform/modules/nsqd"

Call to function "file" failed: no file exists at
terraform/templates/nsqd/nsqd_config.tpl.

Execution of hook in 0.1.8

Terraform validate.......................................................Failed
- hook id: terraform-validate
- exit code: 1

Terraform initialized in an empty directory!

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.

Error: Failed to read module directory

Module directory terraform does not exist or cannot be read.

Directory structure:

tree -L 2                                                                                                                                                    
.
├── README.md
└── terraform
    ├── bin
    ├── config
    ├── main.tf
    ├── Makefile
    ├── modules
    ├── templates
    ├── variables.tf
    └── versions.tf

helmlint always passed

Describe the bug
helmlint doesn't really lint

the folder structure looks like

tree test-hook
test-hook
└── test
    ├── Chart.yaml
    ├── charts
    ├── templates
    │   ├── NOTES.txt
    │   ├── _helpers.tpl
    │   ├── deployment.yaml
    │   ├── hpa.yaml
    │   ├── ingress.yaml
    │   ├── service.yaml
    │   ├── serviceaccount.yaml
    │   └── tests
    │       └── test-connection.yaml
    └── values.yaml

4 directories, 10 files

To Reproduce
Steps to reproduce the behavior including the relevant Terraform/Terragrunt/Packer version number and any code snippets and module inputs you used.

a default helm lint test run looks like

helm lint test 
==> Linting test
[INFO] Chart.yaml: icon is recommended
[ERROR] values.yaml: unable to parse YAML: error converting YAML to JSON: yaml: line 79: did not find expected key
[ERROR] templates/: cannot load values.yaml: error converting YAML to JSON: yaml: line 79: did not find expected key
[ERROR] : unable to load chart
        cannot load values.yaml: error converting YAML to JSON: yaml: line 79: did not find expected key

Error: 1 chart(s) linted, 1 chart(s) failed

But the pre-commit run looks like

pre-commit run --all-files
helmlint.................................................................Passed

here is the pre-commit config

repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: v0.1.17 # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases
    hooks:
      - id: helmlint

Expected behavior
The helmlint should give the same result as helm lint

Additional context
Add any other context about the problem here.

Documentation for shellcheck should include that it is configurable with a .shellcheckrc

Describe the solution you'd like
A simple README.md update is needed to clarify that in addition to the --enable flag, the .shellcheckrc can be used to configure additional entries.

Describe alternatives you've considered
Linking directly to the shellcheck documentation

Additional context
Allow for passing cli args directly through to shellcheck

Other shellcheck flags can not be passed through.

Which is true, but is misleading to those looking for an option to set disable entries.

tflint hook filters out too many issues

Describe the bug
The --filter argument to tflint is too restrictive.

This is the relevant code:

for file in "${FILES[@]}"
do
  tflint "${ARGS[@]}" --chdir "$(dirname "$file")" --filter "$(basename "$file")"
done

tflint runs for each file and then shows only the issues in that file. However some issues don't reference specific files, namely those issues where something is missing, like a missing required_version or provider declaration.

To Reproduce
Run the hook with a terraform file like this:

terraform {
  # required_version = "~>1.6.6"
}

which should find this issue

1 issue(s) found:

Warning: terraform "required_version" attribute is required (terraform_required_version)

  on  line 0:
   (source code not available)

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.5.0/docs/rules/terraform_required_version.md

but this is filtered out, because it does not reference a specific source code file.

Expected behavior
The issue should be found and the hook should fail.

Additional context
Finding issues like this requires that tflint no longer runs for each changed file, but for all files at once.
I think this trade-off should be taken in order to find all issues.

terraform-fmt hook behaviour change

Previously the terraform-fmt hook made changes to the files, whereas now it only shows the difference and errors out. I did some digging and found that it was an intentional change as part of this PR: #46

It would be nice to have the old behaviour as an option. Generally my terraform files are syntactically valid, but incorrectly formatted, the previous behaviour took away the pain point of needing to run terraform fmt after every change. The differences don't need to be analysed, so there's no need for the --diff --check.

Thanks for providing these hooks, they've been super useful!

Running terratest

Hey,

Wondering if there's a reason why this repo doesn't supply a hook for running tests written with terratest (perhaps by default expecting to run go test from an expected test/ subdirectory)?

Sorta like:

- id: terratest
          name: terratest
          description: "Run terratest unit tests"
          entry: sh -c "cd test; go test -count=1 -timeout 30m -tags=unit"
          language: system
          files: '\.tf$'
          pass_filenames: false

Thanks!

Pathspec '0.1.10' did not match any files known to git

Hello,

I'm receiving the following error:

MacBook-Pro$ pre-commit run terraform-fmt --all-files
[INFO] Initializing environment for https://github.com/gruntwork-io/pre-commit.
An unexpected error has occurred: CalledProcessError: command: ('/usr/local/bin/git', 'checkout', '0.1.10')
return code: 1
expected return code: 0
stdout: (none)
stderr:
    error: pathspec '0.1.10' did not match any file(s) known to git.

My yaml:

# This configuration file allows our pre-commit hooks to be used with pre-commit: http://pre-commit.com/
repos:
  -   repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v2.3.0
      hooks:
        -   id: check-yaml
        -   id: end-of-file-fixer
        -   id: trailing-whitespace
  -   repo: https://github.com/psf/black
      rev: 19.3b0
      hooks:
        -   id: black
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: "0.1.10" # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases
    hooks:
      - id: terraform-fmt
      - id: terraform-validate
      - id: tflint
      - id: shellcheck
      - id: gofmt
      - id: golint

Add `terragrunt-providers-lock` hook

Describe the solution you'd like

It would be nice to have a hook which locks versions in .terraform.lock.hcl file automatically. For example, terragrunt-providers-lock hook which runs terragrunt providers lock command for specified platforms under the hood.

Describe alternatives you've considered

Write a local hook, something like:

  - repo: local
    hooks:
      - id: terragrunt-providers-lock
        name: Terragrunt providers lock
        entry: terragrunt providers lock -platform=darwin_amd64 -platform=darwin_arm64 -platform=linux_amd64
        language: script
        files: '\.hcl$'

Additional context

A similar terraform_providers_lock hook for Terraform is provided by pre-commit-terraform project.

Packer validate not actually validating

The code in #58 runs

$ packer validate -syntax-only
Usage: packer validate [options] TEMPLATE

  Checks the template is valid by parsing the template and also
  checking the configuration with the various builders, provisioners, etc.

  If it is not valid, the errors will be shown and the command will exit
  with a non-zero exit status. If it is valid, it will exit with a zero
  exit status.

Options:

  -syntax-only           Only check syntax. Do not verify config of the template.
  -except=foo,bar,baz    Validate all builds other than these.
  -machine-readable      Produce machine-readable output.
  -only=foo,bar,baz      Validate only these builds.
  -var 'key=value'       Variable for templates, can be used multiple times.
  -var-file=path         JSON or HCL2 file containing user variables.

in the directory of a template. As shown above, you need to actually specify a file or a directory such as

packer validate -syntax-only .

A problem here generally is that TEMPLATE can either be a directory or the path to a specific file. For example, you can put multiple distinct .pkr.hcl files in a directory (not like a Terraform module) OR you can treat all the files in a directory as a template to merge together (like a Terraform module). I will submit a PR that operates under the assumption that every directory is a discrete Packer template like Terraform modules. IMHO this is best practice, although not the only practice.

Additionally, the files filter matches too many files (e.g. *.pkr0). The PR will cover that, too.

Update README for tflint config placeholder substitution

Describe the bug
A clear and concise description of what the bug is.
The README docs state that the tflint hook uses a __GIT_DIR__ placeholder to be substituted with the actual Git repository root. However, the script instead looks for a __GIT_ROOT__ placeholder.

To Reproduce
Steps to reproduce the behavior including the relevant Terraform/Terragrunt/Packer version number and any code snippets and module inputs you used.

// paste code snippets here

Expected behavior
A clear and concise description of what you expected to happen.

Nice to have

  • Terminal output
  • Screenshots

Additional context
Add any other context about the problem here.

add option to customize flags to terraform init for terraform validate

Describe the solution you'd like
The terraform lock file is causing issues with terraform validate being run in an automated job. Terraform init is updating the hashes in the lockfile and thus failing the job because it detects changes were made to files.

For now this can be fixed by not checking in the lock file but this is outside of what hashicorp is recommending.

Ability to use -chdir=DIR with tf validate and tf fmt

Describe the solution you'd like
I need to be able to set the dir via the global terraform argument -chdir=... before running validate:

repos:
- repo: https://github.com/gruntwork-io/pre-commit
  rev: v0.1.23
  hooks:
      - id: terraform-validate
        args: [-chdir=whatever]

However, what the above configuration actually does is attempt to pass -chidr=whatever to the validate subcommand. In other words, it executes this:

terraform validate -chdir=whatever

But that's an error because -chdir is not a valid argument for terraform validate.

What I need is a way to execute this:

terraform -chdir=whatever validate

Describe alternatives you've considered
I haven't found a way to set the working directory via some other means.

Add support to Open Tofu

Either

1)add "tofu-" equivalents for existing hooks "terraform-"
2) or rename "terraform-" to for example "tf-" and autodetect whether tofu is present

For example:
Rename:
terraform-validate
to
tf-validate

and detect tofu or terraform binary similarly like terragrunt does it today.

Helmlint: support for multiple Value files

Describe the solution you'd like
To manage multiple environment tiers we often have base values.yaml file and then environment specific files:

  • values.dev.yaml,
  • values.uat.yaml,
  • values.prod.yaml,
    etc. These are then used as e.g.
helm upgrade myApp . -f values.prod.yaml

The Helm linter has a support for the same arguments:

helm lint . -f values.prod.yaml

It would be great if the helmlint pre-commit hook would support a list of values.yaml files as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.