added a new module and the ability to specify a config file if you want

to.
This commit is contained in:
2026-08-12 17:05:35 -05:00
parent 4ce461025c
commit 7b1f9f9d4e
10 changed files with 208 additions and 33 deletions
+111 -16
View File
@@ -689,6 +689,62 @@ impl AppState {
}
}
}
"show_config" | "sc" => {
let _ = self
.main_tx
.send(ToolMessage::Output((0, "Current Config:".to_string())));
self.config.iter().for_each(|setting| {
let _ = self.main_tx.send(ToolMessage::Output((
0,
format!("{}:{}", setting.0, setting.1),
)));
});
}
"set" => {
let mut ready = false;
let mut key = String::new();
let mut value = String::new();
if let Some(args) = command_args.clone() {
let args_vec: Vec<&str> = args.split(" ").collect();
if args_vec.len() == 2 {
key = args_vec[0].to_string();
value = args_vec[1].to_string();
if self.config.contains_key(&key) {
ready = true;
} else {
let _ = self.main_tx.send(ToolMessage::Output((
0,
"Error Unknown setting! {key}, {value}".to_string(),
)));
return Ok(());
}
}
}
if ready {
self.config.insert(key, value);
self.save_config()?;
let _ = self.main_tx.send(ToolMessage::Output((
0,
"Setting changed successfully!".to_string(),
)));
} else {
let _ = self.main_tx.send(ToolMessage::Output((
0,
"Error malformed command!".to_string(),
)));
let _ = self.main_tx.send(ToolMessage::Output((
0,
"Usag: set setting value".to_string(),
)));
}
}
"list_modules" => {
self.module_loader.commands.iter().for_each(|module| {
let _ = self
.main_tx
.send(ToolMessage::Output((0, format!("{}", module.0))));
});
}
"exit" => {
self.save_all()?;
let _ = self.main_tx.send(ToolMessage::AppStateExit);
@@ -752,6 +808,7 @@ impl AppState {
_ => {}
}
}
let worker_tx = self.main_tx.clone();
self.workers.spawn(move || {
match engine.eval_ast_with_scope::<Dynamic>(&mut scope, &ast) {
Ok(result) => {
@@ -768,9 +825,14 @@ impl AppState {
"failed to process array output".into()
}
} else if result.is_string() {
result
let out_string = result
.into_string()
.unwrap_or_else(|_| "failed to parse string".into())
.unwrap_or_else(|_| "failed to parse string".into());
if cmd_meta.calls != "none" {
let _ =
worker_tx.send(ToolMessage::Input((0, out_string.clone())));
}
out_string
} else {
format!("{:?}", result)
};
@@ -795,6 +857,16 @@ impl AppState {
Ok(())
}
pub fn edit_config(&mut self, key: String, value: String) {
if self.config.contains_key(&key) {
self.config.insert(key.clone(), value.clone());
}
let _ = self.main_tx.send(ToolMessage::Output((
0,
format!("Setting saved! {key}:{value}"),
)));
}
pub fn new_project(
&mut self,
org: String,
@@ -1086,6 +1158,7 @@ impl Server {
self.client_id.clone(),
self.message_que.join("||")
);
if let Err(e) = stream.write_all(payload.as_bytes()) {
self.action_que
.push(format!("Error|sending reponse output to server! {e}"));
@@ -1500,6 +1573,7 @@ pub struct ToolCommand {
pub output_type: String,
pub finished: bool,
pub result: bool,
pub calls: String,
}
pub struct ModuleLoader {
@@ -1575,6 +1649,33 @@ impl ModuleLoader {
engine.register_fn("add_port", |host: &mut Host, port: i64| {
host.add_port(port as usize);
});
engine.register_fn("expand_target", |s: &str| -> rhai::Array {
let mut results = rhai::Array::new();
if let Ok(net) = s.trim().parse::<IpNet>() {
for host in net.hosts() {
results.push(host.to_string().into());
}
} else if let Ok(ip) = s.trim().parse::<IpAddr>() {
results.push(ip.to_string().into());
} else if s.contains(".") {
results.push(s.to_string().into());
}
results
});
engine.register_fn("write_file", |path: &str, contents: &str| {
std::fs::write(path, contents).is_ok()
});
engine.register_fn("find_scope_file", |path: PathBuf| -> String {
for entry in walkdir::WalkDir::new(path).max_depth(10) {
if let Ok(entry) = entry {
if entry.file_name().to_string_lossy().contains("scope") {
return entry.path().display().to_string();
}
}
}
String::new()
});
Self {
engine: Arc::new(engine),
commands: HashMap::new(),
@@ -1623,6 +1724,7 @@ impl ModuleLoader {
let mut name = String::new();
let mut output_type = String::new();
let mut args = Vec::new();
let mut calls = String::new();
for line in config_content.lines() {
if let Some((key, val)) = line.split_once(':') {
match key.trim() {
@@ -1635,6 +1737,9 @@ impl ModuleLoader {
.filter(|s| !s.is_empty())
.collect();
}
"calls" => {
calls = val.trim().to_string();
}
_ => {}
}
}
@@ -1659,6 +1764,7 @@ impl ModuleLoader {
output_type,
finished: false,
result: false,
calls,
};
Some((command, ast))
@@ -1685,7 +1791,6 @@ impl DistroBox {
let mut create_command = Command::new("distrobox");
create_command
.arg("create")
.arg("--root")
.arg("--clone")
.arg(self.template.clone())
.arg("--name")
@@ -1743,7 +1848,6 @@ impl DistroBox {
let mut stop_command = Command::new("distrobox");
stop_command
.arg("stop")
.arg("--root")
.arg(self.template.clone())
.arg("--yes");
stop_command.stdin(Stdio::piped());
@@ -1782,11 +1886,7 @@ impl DistroBox {
pub fn stop(&mut self, tx: Sender<ToolMessage>, rid: usize) -> Result<(), Box<dyn Error>> {
let mut stop_command = Command::new("distrobox");
stop_command
.arg("stop")
.arg("--root")
.arg(self.name.clone())
.arg("--yes");
stop_command.arg("stop").arg(self.name.clone()).arg("--yes");
stop_command.stdin(Stdio::piped());
stop_command.stdout(Stdio::piped());
stop_command.stderr(Stdio::piped());
@@ -1823,11 +1923,7 @@ impl DistroBox {
pub fn destroy(&mut self, tx: Sender<ToolMessage>, rid: usize) -> Result<(), Box<dyn Error>> {
let mut destroycmd = Command::new("distrobox");
destroycmd
.arg("rm")
.arg("--root")
.arg(self.name.clone())
.arg("-f");
destroycmd.arg("rm").arg(self.name.clone()).arg("-f");
let res = destroycmd.status()?;
if !res.success() {
let _ = tx.send(ToolMessage::Output((
@@ -1840,7 +1936,7 @@ impl DistroBox {
)));
let _ = tx.send(ToolMessage::Output((
rid,
format!("distrobox rm --root {} -f", self.name),
format!("distrobox rm {} -f", self.name),
)));
}
self.created = false;
@@ -1855,7 +1951,6 @@ impl DistroBox {
term_cmd
.arg("distrobox")
.arg("enter")
.arg("--root")
.arg(self.name.clone());
} else if arg.contains("ENV_NAME=") {
let (_, env_name) = arg.split_once("=").unwrap();