feat: move parsing logic to script struct
This commit is contained in:
parent
d7a87e8609
commit
26533956ae
39
src/lib.rs
39
src/lib.rs
@ -1,40 +1 @@
|
|||||||
mod script;
|
mod script;
|
||||||
|
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
const PARSE_FUNCTION_RAW: &str = include_str!("../powershell/get-metadata.ps1");
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn get_parse_function() -> String {
|
|
||||||
format!("function parse_ps() {{{PARSE_FUNCTION_RAW}}}")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn parse_powershell_file(path: &str) -> Result<serde_json::Value, String> {
|
|
||||||
let func = get_parse_function();
|
|
||||||
|
|
||||||
let ps_runner = format!("{func}; parse_ps -Path '{path}' | ConvertTo-Json -Depth 10");
|
|
||||||
|
|
||||||
let output = Command::new("pwsh")
|
|
||||||
.args(["-NoProfile", "-Command", &ps_runner])
|
|
||||||
.output()
|
|
||||||
.map_err(|e| e.to_string())?;
|
|
||||||
if output.status.success() {
|
|
||||||
let stdout_str = String::from_utf8_lossy(&output.stdout);
|
|
||||||
Ok(serde_json::from_str(&stdout_str).map_err(|e| e.to_string())?)
|
|
||||||
} else {
|
|
||||||
Err(String::from("Failed to run PowerShell"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_parse_powershell_file() {
|
|
||||||
let got = parse_powershell_file("powershell/test-script.ps1");
|
|
||||||
assert!(got.is_ok());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -1 +1,100 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use parameter::Parameter;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
mod parameter;
|
mod parameter;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
|
||||||
|
pub struct Script {
|
||||||
|
name: Arc<str>,
|
||||||
|
path: Arc<str>,
|
||||||
|
parameters: Vec<Parameter>,
|
||||||
|
default_parameter_set: Option<Arc<str>>,
|
||||||
|
help_uri: Option<Arc<str>>,
|
||||||
|
supports_paging: Option<Arc<str>>,
|
||||||
|
supports_should_process: Option<bool>,
|
||||||
|
positional_binding: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl Script {
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(&self) -> &str {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_file<P: AsRef<str>>(path: P) -> Result<Self, String> {
|
||||||
|
let func = get_parse_function();
|
||||||
|
|
||||||
|
let ps_runner = format!(
|
||||||
|
"{func}; parse_ps -Path '{}' | ConvertTo-Json -Depth 10",
|
||||||
|
path.as_ref()
|
||||||
|
);
|
||||||
|
let output = Command::new("pwsh")
|
||||||
|
.args(["-NoProfile", "-Command", &ps_runner])
|
||||||
|
.output()
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
if output.status.success() {
|
||||||
|
let stdout_str = String::from_utf8_lossy(&output.stdout);
|
||||||
|
Ok(serde_json::from_str(&stdout_str).map_err(|e| e.to_string())?)
|
||||||
|
} else {
|
||||||
|
Err(String::from("Failed to run PowerShell"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
const PARSE_FUNCTION_RAW: &str = include_str!("../powershell/get-metadata.ps1");
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn get_parse_function() -> String {
|
||||||
|
format!("function parse_ps() {{{PARSE_FUNCTION_RAW}}}")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn parse_powershell_file(path: &str) -> Result<serde_json::Value, String> {
|
||||||
|
let func = get_parse_function();
|
||||||
|
|
||||||
|
let ps_runner = format!("{func}; parse_ps -Path '{path}' | ConvertTo-Json -Depth 10");
|
||||||
|
|
||||||
|
let output = Command::new("pwsh")
|
||||||
|
.args(["-NoProfile", "-Command", &ps_runner])
|
||||||
|
.output()
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
if output.status.success() {
|
||||||
|
let stdout_str = String::from_utf8_lossy(&output.stdout);
|
||||||
|
Ok(serde_json::from_str(&stdout_str).map_err(|e| e.to_string())?)
|
||||||
|
} else {
|
||||||
|
Err(String::from("Failed to run PowerShell"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
#![allow(clippy::unwrap_used, clippy::expect_used)]
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_script_from_file() {
|
||||||
|
let got = Script::from_file("powershell/test-script.ps1");
|
||||||
|
dbg!(&got);
|
||||||
|
|
||||||
|
assert!(got.is_ok());
|
||||||
|
|
||||||
|
let got = got.unwrap();
|
||||||
|
|
||||||
|
assert_eq!("test-script.ps1", got.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_powershell_file() {
|
||||||
|
let got = parse_powershell_file("powershell/test-script.ps1");
|
||||||
|
assert!(got.is_ok());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user