-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support shfmt * ci: setup go
- Loading branch information
Showing
10 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use super::{execute_command, read_snippet}; | ||
|
||
#[inline] | ||
pub fn format_using_shfmt(file_path: &std::path::Path) -> std::io::Result<Option<String>> { | ||
let mut cmd = std::process::Command::new("shfmt"); | ||
|
||
// Incase the use hasn't installed biome | ||
cmd.arg("--write").arg(file_path); | ||
|
||
if execute_command(&mut cmd)? { | ||
return read_snippet(file_path).map(Some); | ||
} | ||
|
||
Ok(None) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_shfmt { | ||
use crate::{ | ||
formatters::{setup_snippet, shfmt::format_using_shfmt}, | ||
languages::Language, | ||
}; | ||
|
||
#[test] | ||
fn it_should_format_sh() { | ||
let input = " | ||
#!/bin/sh | ||
add () { | ||
echo \"$1\" + \"$2\" | ||
} | ||
"; | ||
let expected_output = "#!/bin/sh | ||
add() { | ||
\techo \"$1\" + \"$2\" | ||
} | ||
"; | ||
|
||
let snippet = setup_snippet(input, Language::Bash.to_file_ext()) | ||
.expect("it to create a snippet file"); | ||
|
||
let output = format_using_shfmt(snippet.path()) | ||
.expect("it to be succesful") | ||
.expect("it to be some"); | ||
|
||
assert_eq!(expected_output, output); | ||
} | ||
|
||
#[test] | ||
fn it_should_format_bash() { | ||
let input = " | ||
#!/bin/bash | ||
add () { | ||
echo \"$1\" + \"$2\" | ||
} | ||
"; | ||
let expected_output = "#!/bin/bash | ||
add() { | ||
\techo \"$1\" + \"$2\" | ||
} | ||
"; | ||
|
||
let snippet = setup_snippet(input, Language::Bash.to_file_ext()) | ||
.expect("it to create a snippet file"); | ||
|
||
let output = format_using_shfmt(snippet.path()) | ||
.expect("it to be succesful") | ||
.expect("it to be some"); | ||
|
||
assert_eq!(expected_output, output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use schemars::JsonSchema; | ||
|
||
use crate::{config::default_enabled, formatters::shfmt::format_using_shfmt}; | ||
|
||
use super::LanguageFormatter; | ||
|
||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
pub enum BashFormatter { | ||
#[default] | ||
#[serde(rename = "shfmt")] | ||
Shfmt, | ||
} | ||
|
||
#[derive(Debug, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
pub struct Bash { | ||
#[serde(default = "default_enabled")] | ||
pub enabled: bool, | ||
#[serde(default)] | ||
pub formatter: BashFormatter, | ||
} | ||
|
||
impl Default for Bash { | ||
#[inline] | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
formatter: BashFormatter::default(), | ||
} | ||
} | ||
} | ||
|
||
impl LanguageFormatter for Bash { | ||
#[inline] | ||
fn format(&self, snippet_path: &std::path::Path) -> std::io::Result<Option<String>> { | ||
if !self.enabled { | ||
return Ok(None); | ||
} | ||
|
||
match self.formatter { | ||
BashFormatter::Shfmt => format_using_shfmt(snippet_path), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
```sh | ||
|
||
#!/bin/bash | ||
|
||
add () { | ||
echo "$1" + "$2" | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
```sh | ||
|
||
#!/bin/sh | ||
|
||
add () { | ||
echo "$1" + "$2" | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
``` |