diff --git a/CHANGELOG.md b/CHANGELOG.md index d8ac7c4..38b8fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 opa fmt [`#516`](https://github.com/hougesen/mdsf/pull/516) - feat: support purty [`#515`](https://github.com/hougesen/mdsf/pull/515) - feat: support nginxbeautifier [`#514`](https://github.com/hougesen/mdsf/pull/514) - feat: support cabal-fmt [`#513`](https://github.com/hougesen/mdsf/pull/513) diff --git a/README.md b/README.md index 98072ed..c15a3cc 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,7 @@ mdsf init -`mdsf` currently supports 206 commands. Feel free to open an issue/pull-request if your favorite tool is missing! 😃 +`mdsf` currently supports 207 commands. Feel free to open an issue/pull-request if your favorite tool is missing! 😃 | Name | Command | | ------------------------ | -------------------------------------------------------------------------------------- | @@ -311,6 +311,7 @@ mdsf init | `npm-groovy-lint` | `npm-groovy-lint --format PATH` | | `ocamlformat` | `ocamlformat --ignore-invalid-option --inplace --enable-outside-detected-project PATH` | | `ocp-indent` | `ocp-indent --inplace PATH` | +| `opa:fmt` | `opa fmt PATH -w` | | `ormolu` | `ormolu --mode inplace PATH` | | `oxlint` | `oxlint --fix PATH` | | `packer:fmt` | `packer fmt PATH` | diff --git a/mdsf/src/tools/mod.rs b/mdsf/src/tools/mod.rs index 5ef8142..8ec48aa 100644 --- a/mdsf/src/tools/mod.rs +++ b/mdsf/src/tools/mod.rs @@ -121,6 +121,7 @@ pub mod nph; pub mod npm_groovy_lint; pub mod ocamlformat; pub mod ocp_indent; +pub mod opa_fmt; pub mod ormolu; pub mod oxlint; pub mod packer_fmt; @@ -701,6 +702,10 @@ pub enum Tooling { /// `ocp-indent --inplace $PATH` OcpIndent, + #[serde(rename = "opa:fmt")] + /// `opa fmt $PATH -w` + OpaFmt, + #[serde(rename = "ormolu")] /// `ormolu --mode inplace $PATH` Ormolu, @@ -1165,6 +1170,7 @@ impl Tooling { Self::NpmGroovyLint => npm_groovy_lint::run(snippet_path), Self::Ocamlformat => ocamlformat::run(snippet_path), Self::OcpIndent => ocp_indent::run(snippet_path), + Self::OpaFmt => opa_fmt::run(snippet_path), Self::Ormolu => ormolu::run(snippet_path), Self::Oxlint => oxlint::run(snippet_path), Self::PackerFmt => packer_fmt::run(snippet_path), @@ -1380,6 +1386,7 @@ impl AsRef for Tooling { Self::NpmGroovyLint => "npm_groovy_lint", Self::Ocamlformat => "ocamlformat", Self::OcpIndent => "ocp_indent", + Self::OpaFmt => "opa_fmt", Self::Ormolu => "ormolu", Self::Oxlint => "oxlint", Self::PackerFmt => "packer_fmt", diff --git a/mdsf/src/tools/opa_fmt.rs b/mdsf/src/tools/opa_fmt.rs new file mode 100644 index 0000000..6022a28 --- /dev/null +++ b/mdsf/src/tools/opa_fmt.rs @@ -0,0 +1,36 @@ +use std::process::Command; + +use crate::{error::MdsfError, execution::execute_command, runners::CommandType}; + +#[inline] +fn set_opa_fmt_args(mut cmd: Command, file_path: &std::path::Path) -> Command { + cmd.arg("fmt"); + cmd.arg(file_path); + cmd.arg("-w"); + cmd +} + +#[inline] +pub fn run(file_path: &std::path::Path) -> Result<(bool, Option), MdsfError> { + let commands = [CommandType::Direct("opa")]; + + for (index, cmd) in commands.iter().enumerate() { + let cmd = set_opa_fmt_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_opa_fmt {} diff --git a/schemas/v0.2.7/mdsf.schema.json b/schemas/v0.2.7/mdsf.schema.json index 1cef7ea..8bf882c 100644 --- a/schemas/v0.2.7/mdsf.schema.json +++ b/schemas/v0.2.7/mdsf.schema.json @@ -673,6 +673,11 @@ "type": "string", "enum": ["ocp-indent"] }, + { + "description": "`opa fmt $PATH -w`", + "type": "string", + "enum": ["opa:fmt"] + }, { "description": "`ormolu --mode inplace $PATH`", "type": "string", diff --git a/tools/opa/plugin.json b/tools/opa/plugin.json new file mode 100644 index 0000000..9b04c48 --- /dev/null +++ b/tools/opa/plugin.json @@ -0,0 +1,15 @@ +{ + "$schema": "../tool.schema.json", + "binary": "opa", + "categories": ["formatter"], + "commands": { + "fmt": ["fmt", "$PATH", "-w"] + }, + "description": "Format Rego source files", + "homepage": "https://www.openpolicyagent.org/docs/latest/cli/", + "languages": ["rego"], + "name": null, + "npm": null, + "php": null, + "tests": [] +}