Skip to content

Commit 7494e64

Browse files
psieglrvolosatovs
authored andcommitted
Allow to pass env. vars on CLI for serve too
1 parent 4ab9742 commit 7494e64

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ pub async fn serve_stateless<C, S>(
599599
host_resources: Arc<HashMap<Box<str>, HashMap<Box<str>, (ResourceType, ResourceType)>>>,
600600
engine: &Engine,
601601
timeout: Duration,
602+
vars: Vec<(String, String)>,
602603
) -> anyhow::Result<()>
603604
where
604605
C: Invoke + Clone + 'static,
@@ -614,6 +615,7 @@ where
614615
let clt = clt.clone();
615616
let cx = cx.clone();
616617
let engine = engine.clone();
618+
let vars = vars.clone();
617619
info!(?name, "serving root function");
618620
let invocations = srv
619621
.serve_function(
@@ -624,7 +626,7 @@ where
624626
cx.clone(),
625627
"reactor.wasm",
626628
timeout,
627-
std::iter::empty(),
629+
vars.iter().map(|(k, v)| (k.as_str(), v.as_str())),
628630
)
629631
},
630632
pre.clone(),
@@ -675,6 +677,7 @@ where
675677
let clt = clt.clone();
676678
let engine = engine.clone();
677679
let cx = cx.clone();
680+
let vars = vars.clone();
678681
info!(?name, "serving instance function");
679682
let invocations = srv
680683
.serve_function(
@@ -685,7 +688,7 @@ where
685688
cx.clone(),
686689
"reactor.wasm",
687690
timeout,
688-
std::iter::empty(),
691+
vars.iter().map(|(k, v)| (k.as_str(), v.as_str())),
689692
)
690693
},
691694
pre.clone(),
@@ -762,6 +765,7 @@ pub async fn handle_serve<C, S>(
762765
clt: C,
763766
cx: C::Context,
764767
timeout: Duration,
768+
vars: Vec<(String, String)>,
765769
workload: &str,
766770
) -> anyhow::Result<()>
767771
where
@@ -783,20 +787,22 @@ where
783787
host_resources,
784788
&engine,
785789
timeout,
790+
vars,
786791
)
787792
.await?;
788793
} else {
794+
let store = new_store(
795+
&engine,
796+
clt,
797+
cx,
798+
"reactor.wasm",
799+
timeout,
800+
vars.iter().map(|(k, v)| (k.as_str(), v.as_str())),
801+
);
789802
serve_shared(
790803
&mut handlers,
791804
srv,
792-
new_store(
793-
&engine,
794-
clt,
795-
cx,
796-
"reactor.wasm",
797-
timeout,
798-
std::iter::empty(),
799-
),
805+
store,
800806
pre,
801807
guest_resources,
802808
host_resources,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ pub struct ServeArgs {
5858
#[arg(long, default_value = DEFAULT_ADDR)]
5959
export: String,
6060

61+
/// Pass an environment variable to the program.
62+
///
63+
/// `--env NAME=VALUE` sets the environment variable `NAME` to `VALUE`
64+
/// for the guest. Host environment variables are already inherited by
65+
/// default, so only the `NAME=VALUE` form is accepted.
66+
#[arg(long = "env", number_of_values = 1, value_name = "NAME=VALUE", value_parser = parse_env_var)]
67+
vars: Vec<(String, String)>,
68+
6169
/// Path or URL to Wasm command component
6270
workload: String,
6371
}
@@ -87,6 +95,7 @@ pub async fn handle_serve(
8795
timeout,
8896
export,
8997
import,
98+
vars,
9099
ref workload,
91100
}: ServeArgs,
92101
) -> anyhow::Result<()> {
@@ -115,6 +124,7 @@ pub async fn handle_serve(
115124
wrpc_transport::tcp::Client::from(import),
116125
(),
117126
*timeout,
127+
vars,
118128
workload,
119129
)
120130
.await;

0 commit comments

Comments
 (0)