Skip to content

Commit

Permalink
feat: support opa fmt (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen authored Oct 26, 2024
1 parent f85327c commit 2c369a0
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 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)
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 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 |
| ------------------------ | -------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -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` |
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 @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -1380,6 +1386,7 @@ impl AsRef<str> 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",
Expand Down
36 changes: 36 additions & 0 deletions mdsf/src/tools/opa_fmt.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_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<String>), 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 {}
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 @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions tools/opa/plugin.json
Original file line number Diff line number Diff line change
@@ -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": []
}

0 comments on commit 2c369a0

Please sign in to comment.