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

feat(puppet): support puppet-lint #233

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ mdsf init
| ObjectiveC | `clang-format` |
| Perl | `perltidy` |
| Protobuf | `buf`, `clang-format` |
| Puppet | `puppet-lint` |
| PureScript | `purs-tidy` |
| Python | `auto-optional`, `autopep8`, `black`, `blue`, `isort`, `pyink`, `ruff`, `usort`, `yapf` |
| ReScript | `rescript_format` |
Expand Down
4 changes: 4 additions & 0 deletions mdsf.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,9 @@
"mustache": {
"enabled": true,
"formatter": "djlint"
},
"puppet": {
"enabled": true,
"formatter": [["puppet-lint"]]
}
}
41 changes: 41 additions & 0 deletions schemas/v0.0.6/mdsf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@
}
]
},
"puppet": {
"default": {
"enabled": true,
"formatter": [["puppet-lint"]]
},
"allOf": [
{
"$ref": "#/definitions/Lang_for_Puppet"
}
]
},
"purescript": {
"default": {
"enabled": true,
Expand Down Expand Up @@ -1349,6 +1360,19 @@
},
"additionalProperties": false
},
"Lang_for_Puppet": {
"type": "object",
"properties": {
"enabled": {
"default": true,
"type": "boolean"
},
"formatter": {
"$ref": "#/definitions/MdsfFormatter_for_Puppet"
}
},
"additionalProperties": false
},
"Lang_for_PureScript": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2124,6 +2148,19 @@
}
]
},
"MdsfFormatter_for_Puppet": {
"anyOf": [
{
"$ref": "#/definitions/Puppet"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/MdsfFormatter_for_Puppet"
}
}
]
},
"MdsfFormatter_for_PureScript": {
"anyOf": [
{
Expand Down Expand Up @@ -2390,6 +2427,10 @@
"type": "string",
"enum": ["buf", "clang-format"]
},
"Puppet": {
"type": "string",
"enum": ["puppet-lint"]
},
"PureScript": {
"type": "string",
"enum": ["purs-tidy"]
Expand Down
6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
groovy::Groovy, handlebars::Handlebars, haskell::Haskell, hcl::Hcl, html::Html, java::Java,
javascript::JavaScript, json::Json, julia::Julia, just::Just, kcl::Kcl, kotlin::Kotlin,
lua::Lua, markdown::Markdown, mustache::Mustache, nim::Nim, nix::Nix, nunjucks::Nunjucks,
objective_c::ObjectiveC, ocaml::OCaml, perl::Perl, protobuf::Protobuf,
objective_c::ObjectiveC, ocaml::OCaml, perl::Perl, protobuf::Protobuf, puppet::Puppet,
purescript::PureScript, python::Python, rescript::ReScript,
restructuredtext::ReStructuredText, roc::Roc, ruby::Ruby, rust::Rust, scala::Scala,
shell::Shell, solidity::Solidity, sql::Sql, swift::Swift, toml::Toml,
Expand Down Expand Up @@ -219,6 +219,9 @@ pub struct MdsfConfig {

#[serde(default)]
pub mustache: Lang<Mustache>,

#[serde(default)]
pub puppet: Lang<Puppet>,
}

impl Default for MdsfConfig {
Expand Down Expand Up @@ -288,6 +291,7 @@ impl Default for MdsfConfig {
zig: Lang::<Zig>::default(),
mustache: Lang::<Mustache>::default(),
nunjucks: Lang::<Nunjucks>::default(),
puppet: Lang::<Puppet>::default(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub mod ocp_indent;
pub mod ormolu;
pub mod perltidy;
pub mod prettier;
pub mod puppet_lint;
pub mod purs_tidy;
pub mod pyink;
pub mod rescript_format;
Expand Down Expand Up @@ -240,6 +241,7 @@ pub fn format_snippet(config: &MdsfConfig, info: &LineInfo, code: &str) -> Strin
Language::Xml => config.xml.format(snippet_path, info),
Language::Yaml => config.yaml.format(snippet_path, info),
Language::Zig => config.zig.format(snippet_path, info),
Language::Puppet => config.puppet.format(snippet_path, info),
} {
let mut f = formatted_code.trim().to_owned();

Expand Down
13 changes: 13 additions & 0 deletions src/formatters/puppet_lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use super::execute_command;
use crate::error::MdsfError;

#[inline]
pub fn format_using_puppet_lint(
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
let mut cmd = std::process::Command::new("puppet-lint");

cmd.arg("--fix").arg(snippet_path);

execute_command(&mut cmd, snippet_path)
}
5 changes: 5 additions & 0 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub mod objective_c;
pub mod ocaml;
pub mod perl;
pub mod protobuf;
pub mod puppet;
pub mod purescript;
pub mod python;
pub mod rescript;
Expand Down Expand Up @@ -220,6 +221,7 @@ pub enum Language {
Yaml,
Zig,
Solidity,
Puppet,
}

impl core::fmt::Display for Language {
Expand Down Expand Up @@ -285,6 +287,7 @@ impl core::fmt::Display for Language {
Self::Xml => f.write_str("xml"),
Self::Yaml => f.write_str("yaml"),
Self::Zig => f.write_str("zig"),
Self::Puppet => f.write_str("puppet"),
}
}
}
Expand Down Expand Up @@ -370,6 +373,7 @@ impl Language {
"yml" | "yaml" => Some(Self::Yaml),
"zig" => Some(Self::Zig),
"zsh" => Some(Self::Shell(ShellFlavor::Zsh)),
"puppet" => Some(Self::Puppet),
_ => None,
}
}
Expand Down Expand Up @@ -445,6 +449,7 @@ impl Language {
Self::Xml => ".xml",
Self::Yaml => ".yml",
Self::Zig => ".zig",
Self::Puppet => ".pp",
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions src/languages/puppet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use schemars::JsonSchema;

use super::{Lang, LanguageFormatter};
use crate::{
error::MdsfError,
formatters::{puppet_lint::format_using_puppet_lint, MdsfFormatter},
};

#[derive(Default, serde::Serialize, serde::Deserialize, JsonSchema)]
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
pub enum Puppet {
#[default]
#[serde(rename = "puppet-lint")]
PuppetLint,
}

impl Default for Lang<Puppet> {
#[inline]
fn default() -> Self {
Self {
enabled: true,
formatter: MdsfFormatter::<Puppet>::default(),
}
}
}

impl Default for MdsfFormatter<Puppet> {
#[inline]
fn default() -> Self {
Self::Multiple(vec![Self::Multiple(vec![Self::Single(Puppet::PuppetLint)])])
}
}

impl LanguageFormatter for Puppet {
#[inline]
fn format_snippet(
&self,
snippet_path: &std::path::Path,
) -> Result<(bool, Option<String>), MdsfError> {
match self {
Self::PuppetLint => format_using_puppet_lint(snippet_path),
}
}
}

impl core::fmt::Display for Puppet {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::PuppetLint => write!(f, "puppet-lint"),
}
}
}

#[cfg(test)]
mod test_fortran {
use super::Puppet;
use crate::languages::Lang;

#[test]
fn it_should_be_enabled_by_default() {
assert!(Lang::<Puppet>::default().enabled);
}
}
Loading