Skip to content

Commit

Permalink
feat: add packer fix command
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Jan 12, 2025
1 parent 88abbe3 commit 97d299b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

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

- feat: support nomad fmt [`#578`](https://github.com/hougesen/mdsf/pull/578)
- build(deps): bump serde_json from 1.0.133 to 1.0.135 [`#575`](https://github.com/hougesen/mdsf/pull/575)
- build(deps): bump clap_complete from 4.5.38 to 4.5.42 [`#572`](https://github.com/hougesen/mdsf/pull/572)
- build(deps): bump which from 7.0.0 to 7.0.1 [`#570`](https://github.com/hougesen/mdsf/pull/570)
- chore: update filetype bindings [`#577`](https://github.com/hougesen/mdsf/pull/577)
- build(cargo-dist): bump to v0.28.0 [`#576`](https://github.com/hougesen/mdsf/pull/576)
- ci: disable beautysh installation [`#560`](https://github.com/hougesen/mdsf/pull/560)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ mdsf init
| [opa](https://www.openpolicyagent.org/docs/latest/cli/) | Format Rego source files | `formatter` | `rego` |
| [ormolu](https://github.com/tweag/ormolu) | A formatter for Haskell source code | `formatter` | `haskell` |
| [oxlint](https://oxc.rs/docs/guide/usage/linter.html) | Oxlint is designed to catch erroneous or useless code without requiring any configurations by default | `linter` | `javascript`, `typescript` |
| [packer](https://developer.hashicorp.com/packer/docs/commands/fmt) | Packer is used to format HCL2 configuration files | `formatter` | `hcl` |
| [packer](https://developer.hashicorp.com/packer/docs/commands) | Packer is used to format HCL2 configuration files | `formatter` | `hcl` |
| [perltidy](https://github.com/perltidy/perltidy) | Perl::Tidy, a source code formatter for Perl | `formatter` | `perl` |
| [pg_format](https://github.com/darold/pgFormatter) | A PostgreSQL SQL syntax beautifier | `formatter` | `sql` |
| [php-cs-fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) | A tool to automatically fix PHP Coding Standards issues | `formatter`, `linter` | `php` |
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 @@ -131,6 +131,7 @@ pub mod ocp_indent;
pub mod opa_fmt;
pub mod ormolu;
pub mod oxlint;
pub mod packer_fix;
pub mod packer_fmt;
pub mod perltidy;
pub mod pg_format;
Expand Down Expand Up @@ -755,6 +756,10 @@ pub enum Tooling {
/// `oxlint --fix $PATH`
Oxlint,

#[serde(rename = "packer:fix")]
/// `packer fix $PATH`
PackerFix,

#[serde(rename = "packer:fmt")]
/// `packer fmt $PATH`
PackerFmt,
Expand Down Expand Up @@ -1245,6 +1250,7 @@ impl Tooling {
Self::OpaFmt => opa_fmt::run(snippet_path),
Self::Ormolu => ormolu::run(snippet_path),
Self::Oxlint => oxlint::run(snippet_path),
Self::PackerFix => packer_fix::run(snippet_path),
Self::PackerFmt => packer_fmt::run(snippet_path),
Self::Perltidy => perltidy::run(snippet_path),
Self::PgFormat => pg_format::run(snippet_path),
Expand Down Expand Up @@ -1474,6 +1480,7 @@ impl AsRef<str> for Tooling {
Self::OpaFmt => "opa_fmt",
Self::Ormolu => "ormolu",
Self::Oxlint => "oxlint",
Self::PackerFix => "packer_fix",
Self::PackerFmt => "packer_fmt",
Self::Perltidy => "perltidy",
Self::PgFormat => "pg_format",
Expand Down
35 changes: 35 additions & 0 deletions mdsf/src/tools/packer_fix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::process::Command;

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

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

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

for (index, cmd) in commands.iter().enumerate() {
let cmd = set_packer_fix_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_packer_fix {}
5 changes: 5 additions & 0 deletions schemas/v0.3.3-dev/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,11 @@
"type": "string",
"enum": ["oxlint"]
},
{
"description": "`packer fix $PATH`",
"type": "string",
"enum": ["packer:fix"]
},
{
"description": "`packer fmt $PATH`",
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion tools/packer/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"binary": "packer",
"categories": ["formatter"],
"commands": {
"fix": ["fix", "$PATH"],
"fmt": ["fmt", "$PATH"]
},
"description": "Packer is used to format HCL2 configuration files",
"homepage": "https://developer.hashicorp.com/packer/docs/commands/fmt",
"homepage": "https://developer.hashicorp.com/packer/docs/commands",
"languages": ["hcl"],
"name": null,
"npm": null,
Expand Down

0 comments on commit 97d299b

Please sign in to comment.