@@ -1,6 +1,6 @@
|
||||
mod agent;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
use std::{net::SocketAddr, path::PathBuf};
|
||||
|
||||
use agent::AgentService;
|
||||
use anyhow::Error;
|
||||
@@ -14,6 +14,7 @@ use axum::{
|
||||
use churn_domain::AgentEnrollReq;
|
||||
use clap::{Parser, Subcommand};
|
||||
use serde_json::json;
|
||||
use wasmtime::{Caller, Engine, Linker, Module, Store};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
||||
@@ -40,6 +41,19 @@ enum Commands {
|
||||
#[arg(env = "CHURN_TOKEN", long)]
|
||||
token: String,
|
||||
},
|
||||
|
||||
Execute {
|
||||
#[arg(env = "CHURN_AGENT_EXE", long)]
|
||||
exe: PathBuf,
|
||||
|
||||
#[command(subcommand)]
|
||||
commands: Option<ExecuteCommands>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum ExecuteCommands {
|
||||
Source,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
@@ -60,8 +74,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
async fn handle_command(cmd: Command) -> anyhow::Result<()> {
|
||||
match cmd.command {
|
||||
Some(Commands::Daemon { host }) => {
|
||||
match cmd.command.unwrap() {
|
||||
Commands::Daemon { host } => {
|
||||
tracing::info!("Starting churn server");
|
||||
|
||||
let app = Router::new()
|
||||
@@ -77,12 +91,38 @@ async fn handle_command(cmd: Command) -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Some(Commands::Connect {
|
||||
Commands::Connect {
|
||||
host: _,
|
||||
token: _,
|
||||
agent_name: _,
|
||||
}) => todo!(),
|
||||
None => todo!(),
|
||||
} => todo!(),
|
||||
Commands::Execute { exe, commands } => match commands {
|
||||
Some(ExecuteCommands::Source) => Ok(()),
|
||||
None => {
|
||||
let engine = Engine::default();
|
||||
let module = Module::from_file(&engine, exe)?;
|
||||
|
||||
// Create a `Linker` which will be later used to instantiate this module.
|
||||
// Host functionality is defined by name within the `Linker`.
|
||||
let mut linker = Linker::new(&engine);
|
||||
linker.func_wrap("print", "print", |caller: Caller<'_, u32>, param: i32| {
|
||||
println!("Got {} from WebAssembly", param);
|
||||
println!("my host state is: {}", caller.data());
|
||||
})?;
|
||||
|
||||
// All wasm objects operate within the context of a "store". Each
|
||||
// `Store` has a type parameter to store host-specific data, which in
|
||||
// this case we're using `4` for.
|
||||
let mut store = Store::new(&engine, 4);
|
||||
let instance = linker.instantiate(&mut store, &module)?;
|
||||
let hello = instance.get_typed_func::<(), ()>(&mut store, "hello")?;
|
||||
|
||||
// And finally we can call the wasm!
|
||||
hello.call(&mut store, ())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user