Skip to content

Commit

Permalink
feat: support terragrunt hclfmt (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Oct 26, 2024
1 parent 62691bb commit 63c9d6f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [Unreleased](https://github.com/hougesen/mdsf/compare/v0.2.7...HEAD)

- feat: support terragrunt hclfmt [`#505`](https://github.com/hougesen/mdsf/pull/505)
- feat: support shellharden [`#504`](https://github.com/hougesen/mdsf/pull/504)
- feat: support reorder-python-imports [`#503`](https://github.com/hougesen/mdsf/pull/503)
- feat: support reformat-gherkin [`#502`](https://github.com/hougesen/mdsf/pull/502)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ mdsf init
<!-- START_SECTION:supported-tools -->

`mdsf` currently supports 195 commands. Feel free to open an issue/pull-request if your favorite tool is missing! 😃
`mdsf` currently supports 196 commands. Feel free to open an issue/pull-request if your favorite tool is missing! 😃

| Name | Command |
| ------------------------ | -------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -358,6 +358,7 @@ mdsf init
| `taplo` | `taplo format PATH` |
| `templ:fmt` | `templ fmt PATH` |
| `terraform:fmt` | `terraform fmt -write=true PATH` |
| `terragrunt:hclfmt` | `terragrunt hclfmt --terragrunt-hclfmt-file PATH` |
| `tlint:format` | `tlint format PATH` |
| `tofu:fmt` | `tofu fmt -write=true PATH` |
| `topiary` | `topiary format PATH` |
Expand Down
7 changes: 7 additions & 0 deletions mdsf/src/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ pub mod swiftformat;
pub mod taplo;
pub mod templ_fmt;
pub mod terraform_fmt;
pub mod terragrunt_hclfmt;
pub mod tlint_format;
pub mod tofu_fmt;
pub mod topiary;
Expand Down Expand Up @@ -878,6 +879,10 @@ pub enum Tooling {
/// `terraform fmt -write=true $PATH`
TerraformFmt,

#[serde(rename = "terragrunt:hclfmt")]
/// `terragrunt hclfmt --terragrunt-hclfmt-file $PATH`
TerragruntHclfmt,

#[serde(rename = "tlint:format")]
/// `tlint format $PATH`
TlintFormat,
Expand Down Expand Up @@ -1157,6 +1162,7 @@ impl Tooling {
Self::Taplo => taplo::run(snippet_path),
Self::TemplFmt => templ_fmt::run(snippet_path),
Self::TerraformFmt => terraform_fmt::run(snippet_path),
Self::TerragruntHclfmt => terragrunt_hclfmt::run(snippet_path),
Self::TlintFormat => tlint_format::run(snippet_path),
Self::TofuFmt => tofu_fmt::run(snippet_path),
Self::Topiary => topiary::run(snippet_path),
Expand Down Expand Up @@ -1361,6 +1367,7 @@ impl AsRef<str> for Tooling {
Self::Taplo => "taplo",
Self::TemplFmt => "templ_fmt",
Self::TerraformFmt => "terraform_fmt",
Self::TerragruntHclfmt => "terragrunt_hclfmt",
Self::TlintFormat => "tlint_format",
Self::TofuFmt => "tofu_fmt",
Self::Topiary => "topiary",
Expand Down
36 changes: 36 additions & 0 deletions mdsf/src/tools/terragrunt_hclfmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::process::Command;

use crate::{error::MdsfError, execution::execute_command, runners::CommandType};

#[inline]
fn set_terragrunt_hclfmt_args(mut cmd: Command, file_path: &std::path::Path) -> Command {
cmd.arg("hclfmt");
cmd.arg("--terragrunt-hclfmt-file");
cmd.arg(file_path);
cmd
}

#[inline]
pub fn run(file_path: &std::path::Path) -> Result<(bool, Option<String>), MdsfError> {
let commands = [CommandType::Direct("terragrunt")];

for (index, cmd) in commands.iter().enumerate() {
let cmd = set_terragrunt_hclfmt_args(cmd.build(), file_path);
let execution_result = execute_command(cmd, file_path);

if index == commands.len() - 1 {
return execution_result;
}

if let Ok(r) = execution_result {
if !r.0 {
return Ok(r);
}
}
}

Ok((true, None))
}

#[cfg(test)]
mod test_terragrunt_hclfmt {}
5 changes: 5 additions & 0 deletions schemas/v0.2.7/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,11 @@
"type": "string",
"enum": ["terraform:fmt"]
},
{
"description": "`terragrunt hclfmt --terragrunt-hclfmt-file $PATH`",
"type": "string",
"enum": ["terragrunt:hclfmt"]
},
{
"description": "`tlint format $PATH`",
"type": "string",
Expand Down
15 changes: 15 additions & 0 deletions tools/terragrunt/plugin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "../tool.schema.json",
"binary": "terragrunt",
"categories": ["formatter"],
"commands": {
"hclfmt": ["hclfmt", "--terragrunt-hclfmt-file", "$PATH"]
},
"description": "Recursively find hcl files and rewrite them into a canonical format",
"homepage": "https://terragrunt.gruntwork.io/docs/reference/cli-options/#hclfmt",
"languages": ["hcl"],
"name": null,
"npm": null,
"php": null,
"tests": []
}

0 comments on commit 63c9d6f

Please sign in to comment.