Skip to content

Commit 4ab9742

Browse files
psieglrvolosatovs
authored andcommitted
Allow to pass env. vars on CLI
1 parent 4696e4b commit 4ab9742

2 files changed

Lines changed: 58 additions & 13 deletions

File tree

‎crates/wasmtime-cli/src/lib.rs‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,25 +359,30 @@ where
359359
Ok((pre, engine, guest_resources, host_resources))
360360
}
361361

362-
fn new_store<C: Invoke>(
362+
fn new_store<'a, C: Invoke>(
363363
engine: &Engine,
364364
wrpc: C,
365365
cx: C::Context,
366366
arg0: &str,
367367
timeout: Duration,
368+
vars: impl IntoIterator<Item = (&'a str, &'a str)>,
368369
) -> wasmtime::Store<Ctx<C>> {
370+
let mut builder = WasiCtxBuilder::new();
371+
builder
372+
.inherit_env()
373+
.inherit_stdio()
374+
.inherit_network()
375+
.allow_ip_name_lookup(true)
376+
.allow_tcp(true)
377+
.allow_udp(true)
378+
.args(&[arg0]);
379+
for (k, v) in vars {
380+
builder.env(k, v);
381+
}
369382
Store::new(
370383
engine,
371384
Ctx {
372-
wasi: WasiCtxBuilder::new()
373-
.inherit_env()
374-
.inherit_stdio()
375-
.inherit_network()
376-
.allow_ip_name_lookup(true)
377-
.allow_tcp(true)
378-
.allow_udp(true)
379-
.args(&[arg0])
380-
.build(),
385+
wasi: builder.build(),
381386
http: WasiHttpCtx::new(),
382387
table: ResourceTable::new(),
383388
wrpc: WrpcCtx {
@@ -395,6 +400,7 @@ pub async fn handle_run<C>(
395400
clt: C,
396401
cx: C::Context,
397402
timeout: Duration,
403+
vars: Vec<(String, String)>,
398404
workload: &str,
399405
) -> anyhow::Result<()>
400406
where
@@ -403,7 +409,14 @@ where
403409
{
404410
let (pre, engine, _, _) =
405411
instantiate_pre(WASI_SNAPSHOT_PREVIEW1_COMMAND_ADAPTER, workload).await?;
406-
let mut store = new_store(&engine, clt, cx, "command.wasm", timeout);
412+
let mut store = new_store(
413+
&engine,
414+
clt,
415+
cx,
416+
"command.wasm",
417+
timeout,
418+
vars.iter().map(|(k, v)| (k.as_str(), v.as_str())),
419+
);
407420
let cmd = wasmtime_wasi::p2::bindings::CommandPre::new(pre)
408421
.map_err(anyhow::Error::from)
409422
.context("failed to construct `command` instance")?
@@ -605,7 +618,14 @@ where
605618
let invocations = srv
606619
.serve_function(
607620
move || {
608-
new_store(&engine, clt.clone(), cx.clone(), "reactor.wasm", timeout)
621+
new_store(
622+
&engine,
623+
clt.clone(),
624+
cx.clone(),
625+
"reactor.wasm",
626+
timeout,
627+
std::iter::empty(),
628+
)
609629
},
610630
pre.clone(),
611631
Arc::clone(&host_resources),
@@ -665,6 +685,7 @@ where
665685
cx.clone(),
666686
"reactor.wasm",
667687
timeout,
688+
std::iter::empty(),
668689
)
669690
},
670691
pre.clone(),
@@ -768,7 +789,14 @@ where
768789
serve_shared(
769790
&mut handlers,
770791
srv,
771-
new_store(&engine, clt, cx, "reactor.wasm", timeout),
792+
new_store(
793+
&engine,
794+
clt,
795+
cx,
796+
"reactor.wasm",
797+
timeout,
798+
std::iter::empty(),
799+
),
772800
pre,
773801
guest_resources,
774802
host_resources,

‎crates/wasmtime-cli/src/tcp.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,25 @@ pub struct RunArgs {
2424
#[arg(long, default_value = DEFAULT_ADDR)]
2525
import: String,
2626

27+
/// Pass an environment variable to the program.
28+
///
29+
/// `--env NAME=VALUE` sets the environment variable `NAME` to `VALUE`
30+
/// for the guest. Host environment variables are already inherited by
31+
/// default, so only the `NAME=VALUE` form is accepted.
32+
#[arg(long = "env", number_of_values = 1, value_name = "NAME=VALUE", value_parser = parse_env_var)]
33+
vars: Vec<(String, String)>,
34+
2735
/// Path or URL to Wasm command component
2836
workload: String,
2937
}
3038

39+
fn parse_env_var(s: &str) -> Result<(String, String), String> {
40+
let (key, val) = s
41+
.split_once('=')
42+
.ok_or_else(|| format!("invalid `--env` value `{s}`: expected `NAME=VALUE`"))?;
43+
Ok((key.to_string(), val.to_string()))
44+
}
45+
3146
/// Serve a reactor component
3247
#[derive(Parser, Debug)]
3348
pub struct ServeArgs {
@@ -52,13 +67,15 @@ pub async fn handle_run(
5267
RunArgs {
5368
timeout,
5469
import,
70+
vars,
5571
ref workload,
5672
}: RunArgs,
5773
) -> anyhow::Result<()> {
5874
crate::handle_run(
5975
wrpc_transport::tcp::Client::from(import),
6076
(),
6177
*timeout,
78+
vars,
6279
workload,
6380
)
6481
.await

0 commit comments

Comments
 (0)