feat: add parsing draft
This commit is contained in:
parent
abc1b205a9
commit
3fb7f0926e
@ -2,5 +2,8 @@
|
||||
name = "befehlswerk"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
description = "PowerShell Scripts for EVERYONE!"
|
||||
repository = "https://git.itsscb.de/itsscb/BefehlsWerk"
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0.140"
|
||||
|
39
src/lib.rs
39
src/lib.rs
@ -1,3 +1,38 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
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/get-metadata.ps1");
|
||||
assert!(got.is_ok());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user