Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kinode/packages/terminal/cat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ fn init(_our: Address) {
println!("no file found at {}", file_path);
return;
};
let _ = Response::new().body(blob.bytes).send();
println!("{}", String::from_utf8(blob.bytes).unwrap());
}
12 changes: 5 additions & 7 deletions kinode/packages/terminal/echo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use kinode_process_lib::{await_next_request_body, call_init, println, Address, Response};
use kinode_process_lib::{await_next_request_body, call_init, println, Address};

wit_bindgen::generate!({
path: "wit",
Expand All @@ -16,10 +16,8 @@ fn init(_our: Address) {
return;
};

let _ = Response::new()
.body(format!(
"{}",
String::from_utf8(args).unwrap_or("echo: error".into())
))
.send();
match String::from_utf8(args.clone()) {
Ok(s) => println!("{}", s),
Err(_) => println!("{:?}", args),
}
}
16 changes: 11 additions & 5 deletions kinode/packages/terminal/hi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use kinode_process_lib::{
await_next_request_body, call_init, println, Address, Request, SendError,
await_next_request_body, call_init, println, Address, Request, SendError, SendErrorKind,
};

wit_bindgen::generate!({
Expand Down Expand Up @@ -46,9 +46,15 @@ fn init(our: Address) {
println!("response from {node_id}: {:?}", msg.body());
}
}
Err(SendError { kind, .. }) => {
println!("hi: net error: {:?}", kind);
return;
}
Err(SendError { kind, .. }) => match kind {
SendErrorKind::Timeout => {
println!("hi: message to {node_id} timed out");
return;
}
SendErrorKind::Offline => {
println!("hi: {node_id} is offline or does not exist");
return;
}
},
}
}
8 changes: 1 addition & 7 deletions kinode/packages/terminal/m/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,7 @@ fn init(_our: Address) {
println!("m: awaiting response for {}s", s);
match req.send_and_await_response(*s).unwrap() {
Ok(res) => {
// TODO piping is broken. Patching it by just printing the response
// let _ = Response::new().body(res.body()).send();
if let Ok(txt) = std::str::from_utf8(&res.body()) {
println!("{txt}");
} else {
println!("{:?}", res.body());
}
println!("{}", String::from_utf8(res.body().to_vec()).unwrap());
}
Err(e) => {
println!(
Expand Down
54 changes: 3 additions & 51 deletions kinode/packages/terminal/terminal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,7 @@ fn parse_command(state: &mut TerminalState, line: &str) -> anyhow::Result<()> {
},
};

let re = Regex::new(r"(.*?)\|(\d+)\s*(.*)").unwrap();
let pipe = match re.captures(args) {
Some(caps) => {
let parsed_args = caps
.get(1)
.map_or("", |m| m.as_str())
.trim_end()
.to_string();

let time_str = caps.get(2).map_or("", |m| m.as_str());
let time: u64 = time_str.parse().unwrap_or(0);

let pipe = caps
.get(3)
.map_or("", |m| m.as_str())
.trim_start()
.to_string();

(parsed_args, Some((pipe, time)))
}
None => (args.to_string(), None),
};

match handle_run(&state.our, &process, pipe.0, pipe.1) {
match handle_run(&state.our, &process, args.to_string()) {
Ok(_) => Ok(()), // TODO clean up process
Err(e) => Err(anyhow!("failed to instantiate script: {}", e)),
}
Expand Down Expand Up @@ -162,12 +139,7 @@ impl Guest for Component {
}
}

fn handle_run(
our: &Address,
process: &ProcessId,
args: String,
pipe: Option<(String, u64)>,
) -> anyhow::Result<()> {
fn handle_run(our: &Address, process: &ProcessId, args: String) -> anyhow::Result<()> {
let wasm_path = format!("{}.wasm", process.process());
let package = format!("{}:{}", process.package(), process.publisher());
let drive_path = format!("/{}/pkg", package);
Expand Down Expand Up @@ -352,27 +324,7 @@ fn handle_run(
.target(("our", parsed_new_process_id))
.body(args.into_bytes());

let Some(pipe) = pipe else {
req.send().unwrap();
return Ok(());
};

let Ok(res) = req.clone().send_and_await_response(pipe.1).unwrap() else {
return Err(anyhow::anyhow!("script timed out"));
};

let _ = Request::new()
.target(our)
.body(
format!(
"{} {}",
pipe.0,
String::from_utf8(res.body().to_vec()).unwrap()
)
.into_bytes()
.to_vec(),
)
.send()?;
req.send().unwrap();

Ok(())
}
Expand Down