Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: --recursive option to sort variables and outputs files recursively #38

Open
GeorgeDavis-TriumphTech opened this issue Apr 20, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@GeorgeDavis-TriumphTech

Hey @AlexNabokikh!,

This is a great little tool. I would love to use a --recursive option in the tool to allow to tfsort the directory, parsing variables.tf and outputs.tf recursively inside directories, from the parent directory.

@AlexNabokikh AlexNabokikh added the enhancement New feature or request label Apr 20, 2023
AlexNabokikh added a commit that referenced this issue Jul 29, 2023
AlexNabokikh added a commit that referenced this issue Jul 29, 2023
AlexNabokikh added a commit that referenced this issue Jul 29, 2023
AlexNabokikh added a commit that referenced this issue Jul 29, 2023
AlexNabokikh added a commit that referenced this issue Jul 29, 2023
@jamesgeddes
Copy link
Contributor

The following GitHub Actions workflow would achieve this

name: Sort Terraform Variables

on:
  push:
    paths:
      - "terraform/**" # Assuming that your Terraform is in the terraform directory
  workflow_dispatch:

jobs:
  tfsort:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Ensure full history for commits

      - name: Install tfsort
        run: |
          curl -sLo tfsort.tar.gz https://github.com/AlexNabokikh/tfsort/releases/download/v0.2.4/tfsort_0.2.4_linux_amd64.tar.gz
          tar -xzf tfsort.tar.gz -C /usr/local/bin
          chmod +x /usr/local/bin/tfsort

      - name: Find and Sort Terraform Variable Files
        run: |
          # Find all `variables.tf` files and sort them
          find terraform -type f -name "variables.tf" -exec tfsort {} \;

      - name: Commit and Push Changes
        if: ${{ success() }}
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add terraform/**/variables.tf
          git commit -m "chore: sort variables.tf using tfsort"
          git push

The key line here is

find terraform -type f -name "variables.tf" -exec tfsort {} \;

This uses find to find all the variables.tf files and run tfsort on them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants