Skip to content

Commit

Permalink
fix: Modify directory search logic to look for required_providers w…
Browse files Browse the repository at this point in the history
…ithin the Terraform files and ignore file paths with `wrappers` in them (#14)
  • Loading branch information
bryantbiggs authored Nov 13, 2022
1 parent 1c564ff commit 1378076
Show file tree
Hide file tree
Showing 29 changed files with 2,047 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
TERRAFORM_DOCS_VERSION: v0.16.0
HCLEDIT_VERSION: 0.2.3
HCLEDIT_VERSION: 0.2.6

jobs:
collectInputs:
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:

- name: Terraform min/max versions
id: minMax
uses: clowdhaus/terraform-min-max@v1.0.8
uses: clowdhaus/terraform-min-max@v1.2.3
with:
directory: ${{ matrix.directory }}

Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:

- name: Terraform min/max versions
id: minMax
uses: clowdhaus/terraform-min-max@v1.0.8
uses: clowdhaus/terraform-min-max@v1.2.3

- name: Pre-commit Terraform ${{ steps.minMax.outputs.maxVersion }}
uses: ./pre-commit
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.72.1
rev: v1.76.0
hooks:
- id: terraform_fmt
- id: terraform_validate
Expand All @@ -23,7 +23,7 @@ repos:
- '--args=--only=terraform_standard_module_structure'
- '--args=--only=terraform_workspace_remote'
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.3.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
3 changes: 2 additions & 1 deletion directories/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ runs:
shell: bash
id: search
run: |
DIRS=$(python -c "import json; import glob; import re; print(json.dumps([x.replace('/versions.tf', '') for x in glob.glob('./**/versions.tf', recursive=True) if not re.match(r'^.+/_', x)]))")
ls -la
DIRS=$(python ./directories/directories.py)
echo "directories=$DIRS" >> $GITHUB_OUTPUT
30 changes: 30 additions & 0 deletions directories/directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json
import mmap
import os
import re
from glob import glob


def get_directories():
"""
Return all Terraform (*.tf) files that contain the `required_version`
string, but does not contain `wrapper` in the path.
"""

terraform_files = glob('./**/*.tf', recursive=True)
directories = []

for file in terraform_files:
file_size = os.stat(file)

if file_size.st_size > 0:
with open(file, 'rb', 0) as rfile:
contents = mmap.mmap(rfile.fileno(), 0, access=mmap.ACCESS_READ)
if contents.find(b'required_version') != -1 and 'wrapper' not in file:
directories.append(file)

return json.dumps([x.replace('/versions.tf', '') for x in directories if not re.match(r'^.+/_', x)])


if __name__ == '__main__':
print(get_directories())
1 change: 1 addition & 0 deletions examples/0.13/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locals {}
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions examples/0.13/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
provider "aws" {
region = "us-east-1"
}

terraform {
required_version = ">= 0.14"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.0"
}
}
}
1 change: 1 addition & 0 deletions examples/0.14/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
locals {}
Empty file added examples/0.14/outputs.tf
Empty file.
Empty file added examples/0.14/variables.tf
Empty file.
14 changes: 14 additions & 0 deletions examples/0.14/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
provider "aws" {
region = "us-east-1"
}

terraform {
required_version = ">= 0.13"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 2.0"
}
}
}
Loading

0 comments on commit 1378076

Please sign in to comment.