terminal refactor

This commit is contained in:
Drew Tada 2024-02-07 18:45:22 -05:00
parent e3ae308461
commit 3e6594f11c
3 changed files with 53 additions and 48 deletions

23
Cargo.lock generated
View File

@ -3191,8 +3191,25 @@ dependencies = [
[[package]]
name = "kinode_process_lib"
version = "0.5.8"
source = "git+https://github.com/uqbar-dao/process_lib.git?rev=9668c82#9668c820986e32f7b8430648147c2498d5abb9f8"
version = "0.5.9"
source = "git+https://github.com/uqbar-dao/process_lib.git?tag=v0.5.9-alpha#5e705086bbd10fde89e11d3e3671f6a618a875a7"
dependencies = [
"anyhow",
"bincode",
"http 1.0.0",
"mime_guess",
"rand 0.8.5",
"serde",
"serde_json",
"thiserror",
"url",
"wit-bindgen",
]
[[package]]
name = "kinode_process_lib"
version = "0.6.0"
source = "git+https://github.com/uqbar-dao/process_lib.git?rev=509b1ed#509b1ed65041762dc6dfa7dd85811b7783c1997c"
dependencies = [
"anyhow",
"bincode",
@ -5535,7 +5552,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"bincode",
"kinode_process_lib 0.5.9 (git+https://github.com/kinode-dao/process_lib?tag=v0.5.9-alpha)",
"kinode_process_lib 0.6.0",
"rand 0.8.5",
"regex",
"serde",

View File

@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
bincode = "1.3.3"
kinode_process_lib = { git = "https://github.com/uqbar-dao/process_lib.git", rev = "9668c82" }
kinode_process_lib = { git = "https://github.com/uqbar-dao/process_lib.git", rev = "509b1ed" }
rand = "0.8"
regex = "1.10.3"
serde = { version = "1.0", features = ["derive"] }

View File

@ -3,7 +3,7 @@ use kinode_process_lib::kernel_types as kt;
use kinode_process_lib::kinode::process::standard as wit;
use kinode_process_lib::{
get_blob, get_typed_state, our_capabilities, print_to_terminal, println, set_state, vfs,
Address, Capability, PackageId, ProcessId, Request,
Address, Capability, ProcessId, Request,
};
use regex::Regex;
use serde::{Deserialize, Serialize};
@ -70,9 +70,7 @@ fn parse_command(state: &mut TerminalState, line: &str) -> anyhow::Result<()> {
None => (args.to_string(), None),
};
let wasm_path = format!("{}.wasm", process.process());
let package = PackageId::new(process.package(), process.publisher());
match handle_run(&state.our, &package, wasm_path, pipe.0, pipe.1) {
match handle_run(&state.our, &process, pipe.0, pipe.1) {
Ok(_) => Ok(()), // TODO clean up process
Err(e) => Err(anyhow!("failed to instantiate script: {}", e)),
}
@ -173,28 +171,13 @@ impl Guest for Component {
fn handle_run(
our: &Address,
package: &PackageId,
wasm_path: String,
process: &ProcessId,
args: String,
pipe: Option<(String, u64)>,
) -> anyhow::Result<()> {
let drive_path = format!("/{}/pkg", package);
Request::new()
.target(("our", "vfs", "distro", "sys"))
.body(serde_json::to_vec(&vfs::VfsRequest {
path: format!("{}/scripts.json", drive_path),
action: vfs::VfsAction::Read,
})?)
.send_and_await_response(5)??;
let Some(blob) = get_blob() else {
return Err(anyhow::anyhow!(
"couldn't find /{}/pkg/scripts.json",
package
));
};
let dot_scripts = String::from_utf8(blob.bytes)?;
let dot_scripts = serde_json::from_str::<HashMap<String, kt::DotScriptsEntry>>(&dot_scripts)?;
let Some(entry) = dot_scripts.get(&wasm_path) else {
let wasm_path = format!("{}.wasm", process.process());
let drive_path = format!("/{}:{}/pkg", process.package(), process.publisher());
let Ok(entry) = get_entry(process) else {
return Err(anyhow::anyhow!("script not in scripts.json file"));
};
let wasm_path = if wasm_path.starts_with("/") {
@ -418,27 +401,11 @@ fn handle_alias_change(
) -> anyhow::Result<()> {
match process {
Some(process) => {
// check to make sure the script is actually a script
let drive_path = format!("/{}:{}/pkg", process.package(), process.publisher());
Request::new()
.target(("our", "vfs", "distro", "sys"))
.body(serde_json::to_vec(&vfs::VfsRequest {
path: format!("{}/scripts.json", drive_path),
action: vfs::VfsAction::Read,
})?)
.send_and_await_response(5)??;
let Some(blob) = get_blob() else {
return Err(anyhow::anyhow!(
"couldn't find /{}/pkg/scripts.json",
process.package()
));
};
let dot_scripts = String::from_utf8(blob.bytes)?;
let dot_scripts =
serde_json::from_str::<HashMap<String, kt::DotScriptsEntry>>(&dot_scripts)?;
let Some(_) = dot_scripts.get(&format!("{}.wasm", process.process())) else {
return Err(anyhow::anyhow!("script not in scripts.json file"));
// first check to make sure the script is actually a script
let Ok(_) = get_entry(&process) else {
return Err(anyhow!("terminal: process {} not found", process));
};
state.aliases.insert(alias.clone(), process.clone());
println!("terminal: alias {} set to {}", alias, process);
}
@ -473,4 +440,25 @@ fn handle_process_cleanup(caps_to_remove: Vec<(ProcessId, Capability)>) -> anyho
Ok(())
}
fn get_entry() -> anyhow::Result<()> {}
fn get_entry(process: &ProcessId) -> anyhow::Result<kt::DotScriptsEntry> {
let drive_path = format!("/{}:{}/pkg", process.package(), process.publisher());
Request::new()
.target(("our", "vfs", "distro", "sys"))
.body(serde_json::to_vec(&vfs::VfsRequest {
path: format!("{}/scripts.json", drive_path),
action: vfs::VfsAction::Read,
})?)
.send_and_await_response(5)??;
let Some(blob) = get_blob() else {
return Err(anyhow::anyhow!(
"couldn't find /{}/pkg/scripts.json",
process.package()
));
};
let dot_scripts = String::from_utf8(blob.bytes)?;
let dot_scripts = serde_json::from_str::<HashMap<String, kt::DotScriptsEntry>>(&dot_scripts)?;
let Some(entry) = dot_scripts.get(&format!("{}.wasm", process.process())) else {
return Err(anyhow::anyhow!("script not in scripts.json file"));
};
Ok(entry.clone())
}