Skip to content

Commit

Permalink
refactor: update module imports and file paths for scimon integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kremilly committed Jun 12, 2024
1 parent 3f64870 commit bd1d419
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 53 deletions.
18 changes: 3 additions & 15 deletions src/monset/blocks/runner_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ use std::{
};

use crate::{
system::system::System,

ui::{
ui_base::UI,
errors_commands_alerts::ErrorsCommandsAlerts,
},
ui::ui_base::UI,
system::scripts::Scripts,
};

pub struct RunnerBlock;
Expand Down Expand Up @@ -40,15 +36,7 @@ impl RunnerBlock {
break;
}

if line_trimmed.len() >= 3 {
if line_trimmed.ends_with(".py") {
System::exec_script(&line_trimmed, "python")?;
} else if line_trimmed.ends_with(".js") {
System::exec_script(&line_trimmed, "node")?;
} else {
ErrorsCommandsAlerts::unsupported(&line_trimmed);
}
}
Scripts::read(line_trimmed)?;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/system/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod pdf;
pub mod system;
pub mod hashes;
pub mod scripts;
pub mod markdown;
pub mod providers;
pub mod reporting;
60 changes: 60 additions & 0 deletions src/system/scripts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
extern crate chrono;

use regex::Regex;

use std::{
error::Error,

process::{
Stdio,
Command,
},
};

use crate::{
regexp::regex_core::CoreRegExp,
ui::errors_commands_alerts::ErrorsCommandsAlerts,
};

pub struct Scripts;

impl Scripts {

pub fn exec(line: &str, program: &str) -> Result<(), Box<dyn Error>> {
let line_cleanned = Regex::new(
CoreRegExp::CLEAN_LINE
).unwrap().replace_all(
&line, ""
).to_string();

let output = Command::new(&program)
.arg(line_cleanned)
.stdout(Stdio::piped())
.output()?;

if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
println!("{}", stdout);
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
ErrorsCommandsAlerts::executing(&stderr);
}

Ok(())
}

pub fn read(line_trimmed: &str) -> Result<(), Box<dyn Error>> {
if line_trimmed.len() >= 3 {
if line_trimmed.ends_with(".py") {
Self::exec(&line_trimmed, "python")?;
} else if line_trimmed.ends_with(".js") {
Self::exec(&line_trimmed, "node")?;
} else {
ErrorsCommandsAlerts::unsupported(&line_trimmed);
}
}

Ok(())
}

}
40 changes: 2 additions & 38 deletions src/system/system.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
extern crate chrono;

use regex::Regex;
use chrono::Local;
use once_cell::sync::Lazy;
use dirs_next::config_dir;

use std::{
error::Error,
path::PathBuf,
use std::path::PathBuf;

process::{
Stdio,
Command,
},
};

use crate::{
consts::global::Global,
regexp::regex_core::CoreRegExp,
ui::errors_commands_alerts::ErrorsCommandsAlerts,
};
use crate::consts::global::Global;

pub struct System;

Expand Down Expand Up @@ -55,28 +42,5 @@ impl System {

format!("{} {}", date_formated, hour_formated)
}

pub fn exec_script(line: &str, program: &str) -> Result<(), Box<dyn Error>> {
let line_cleanned = Regex::new(
CoreRegExp::CLEAN_LINE
).unwrap().replace_all(
&line, ""
).to_string();

let output = Command::new(&program)
.arg(line_cleanned)
.stdout(Stdio::piped())
.output()?;

if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
println!("{}", stdout);
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
ErrorsCommandsAlerts::executing(&stderr);
}

Ok(())
}

}

0 comments on commit bd1d419

Please sign in to comment.