Skip to content

Commit

Permalink
Added WCGI-specific flags to the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Mar 6, 2023
1 parent ca52b2e commit bd324f9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
37 changes: 35 additions & 2 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use crate::suggestions::suggest_function_exports;
use crate::warning;
use anyhow::{anyhow, Context, Result};
use clap::Parser;
use std::fs::File;
use std::io::Write;
use std::ops::Deref;
use std::path::PathBuf;
#[cfg(feature = "cache")]
use std::str::FromStr;
use std::{fs::File, net::SocketAddr};
#[cfg(feature = "emscripten")]
use wasmer::FunctionEnv;
use wasmer::*;
Expand Down Expand Up @@ -91,6 +91,10 @@ pub struct RunWithoutFile {
#[clap(long = "verbose")]
pub(crate) verbose: Option<u8>,

#[cfg(feature = "webc_runner")]
#[clap(flatten)]
pub(crate) wcgi: WcgiOptions,

/// Enable coredump generation after a WebAssembly trap.
#[clap(name = "COREDUMP PATH", long = "coredump-on-trap", parse(from_os_str))]
coredump_on_trap: Option<PathBuf>,
Expand Down Expand Up @@ -223,6 +227,7 @@ impl RunWithPathBuf {
fn inner_execute(&self) -> Result<()> {
#[cfg(feature = "webc_runner")]
{
dbg!(&self.path);
if let Ok(pf) = WapmContainer::from_path(self.path.clone()) {
return self.run_container(pf, self.command_name.as_deref(), &self.args);
}
Expand Down Expand Up @@ -400,7 +405,16 @@ impl RunWithPathBuf {

let mut runner = wasmer_wasi::runners::wcgi::WcgiRunner::new(id);
let (store, _compiler_type) = self.store.get_store()?;
runner.config().args(args).store(store);
runner
.config()
.args(args)
.store(store)
.addr(self.wcgi.addr)
.envs(self.wasi.env_vars.clone())
.map_directories(self.wasi.mapped_dirs.iter().map(|(g, h)| (h, g)));
if self.wcgi.forward_host_env {
runner.config().forward_host_env();
}
if runner.can_run_command(id, command).unwrap_or(false) {
return runner.run_cmd(&container, id).context("WCGI runner failed");
}
Expand Down Expand Up @@ -689,3 +703,22 @@ fn generate_coredump(

Ok(())
}

#[derive(Debug, Clone, Parser)]
pub(crate) struct WcgiOptions {
/// The address to serve on.
#[clap(long, short, env, default_value_t = ([127, 0, 0, 1], 8000).into())]
pub(crate) addr: SocketAddr,
/// Forward all host env variables to the wcgi task.
#[clap(long)]
pub(crate) forward_host_env: bool,
}

impl Default for WcgiOptions {
fn default() -> Self {
Self {
addr: ([127, 0, 0, 1], 8000).into(),
forward_host_env: false,
}
}
}
14 changes: 14 additions & 0 deletions lib/wasi/src/runners/wcgi/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,20 @@ impl Config {
self
}

pub fn map_directories<I, H, G>(&mut self, mappings: I) -> &mut Self
where
I: IntoIterator<Item = (H, G)>,
H: Into<PathBuf>,
G: Into<String>,
{
let mappings = mappings.into_iter().map(|(h, g)| MappedDirectory {
host: h.into(),
guest: g.into(),
});
self.mapped_dirs.extend(mappings);
self
}

/// Set callbacks that will be triggered at various points in the runner's
/// lifecycle.
pub fn callbacks(&mut self, callbacks: impl Callbacks + Send + Sync + 'static) -> &mut Self {
Expand Down

0 comments on commit bd324f9

Please sign in to comment.