Skip to content

Commit 64ba587

Browse files
committed
chore: Run cargo fmt
1 parent 984c392 commit 64ba587

File tree

26 files changed

+1030
-375
lines changed

26 files changed

+1030
-375
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wagi"
3-
version = "0.6.2"
3+
version = "0.8.1"
44
authors = ["Matt Butcher <[email protected]>"]
55
edition = "2021"
66

@@ -17,6 +17,7 @@
1717
hyper = { version = "0.14", features = ["full"] }
1818
indexmap = { version = "^1.6.2", features = ["serde"] }
1919
oci-distribution = "0.6"
20+
reqwest = { version = "0.11", features = ["stream"] }
2021
serde = { version = "1.0", features = ["derive"] }
2122
sha2 = "0.9"
2223
tokio = { version = "1.1", features = ["full"] }

docs/configuring_and_running.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ In a nutshell, these are the fields that `modules.toml` supports.
5757
- `route` (REQUIRED): The path that is appended to the server URL to create a full URL (e.g. `/foo` becomes `https://example.com/foo`)
5858
- `module` (REQUIRED): A module reference. See Module References below.
5959
- `repository`: RESERVED for future use
60-
- `entrypoint` (default: `_start`): The name of the function within the module. This will directly execute that function. Most WASM/WASI implementations create a `_start` function by default. An example of a module that declares 3 entrypoints can be found [here](https://github.com/technosophos/hello-wagi).
60+
- `entrypoint` (Optional, default: `_start`): The name of the function within the module. This will directly execute that function. Most WASM/WASI implementations create a `_start` function by default. An example of a module that declares 3 entrypoints can be found [here](https://github.com/technosophos/hello-wagi).
61+
- `argv`: (Optional, default: "${SCRIPT_NAME} ${ARGS}"). This determines what the `argv` array looks like for the invoked program. The CGI 1.1 spec says that the `argv` array should contain the script name followed by the parameters. However, some Wasm modules require specifically formatted `argv`. This allows a way to override the CGI 1.1 defaults. Example: `argv = "ruby index.rb ${SCRIPT_NAME} ${ARGS}"`. This could expand to `ruby index.rb /example param1=val1 param2=val2`
6162

6263
Here is a brief example of a `modules.toml` file that declares two routes:
6364

@@ -262,6 +263,7 @@ The following features are available for Wagi under `feature.wagi.FEATURE`:
262263
| route | The relative path from the server route. e.g. "/foo" is mapped to http://example.com/foo |
263264
| allowed_hosts | A comma-separated list of hosts that the HTTP client is allowed to access |
264265
| file | If this is "true", this parcel will be treated as a file for consumption by a Wagi module |
266+
| argv | If this is set, use this as a template for building the `argv` array. Two values are substituted: `${SCRIPT_NAME}` is replaced with the CGI `$SCRIPT_NAME` and `${ARGS}` is replaced with the query parameters formatted for CGI. |
265267

266268
### Simple Bindle Example
267269

docs/installation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ and download the desired release. Usually, the most recent release is the one yo
88
You can generate and compare the SHA with `shasum`:
99

1010
```console
11-
$ shasum wagi-v0.6.2-linux-amd64.tar.gz
12-
ad4114b2ed9e510a8c24348d5ea544da55c685f5 wagi-v0.6.2-linux-amd64.tar.gz
11+
$ shasum wagi-v0.8.1-linux-amd64.tar.gz
12+
ad4114b2ed9e510a8c24348d5ea544da55c685f5 wagi-v0.8.1-linux-amd64.tar.gz
1313
```
1414

1515
You can then compare that SHA with the one present in the release notes.
@@ -40,7 +40,7 @@ To build a static binary, run the following command:
4040

4141
```console
4242
$ make build
43-
Compiling wagi v0.6.2 (/Users/technosophos/Code/Rust/wagi)
43+
Compiling wagi v0.8.1 (/Users/technosophos/Code/Rust/wagi)
4444
Finished release [optimized] target(s) in 18.47s
4545
```
4646

src/bindle_util.rs

Lines changed: 132 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{collections::{HashMap, HashSet}, iter::FromIterator};
1+
use std::{
2+
collections::{HashMap, HashSet},
3+
iter::FromIterator,
4+
};
25

36
use bindle::{Invoice, Parcel};
47

@@ -45,30 +48,35 @@ impl InvoiceUnderstander {
4548
pub fn classify_parcel(&self, parcel: &Parcel) -> Option<InterestingParcel> {
4649
// Currently only handlers but we have talked of scheduled tasks etc.
4750
parcel.label.feature.as_ref().and_then(|features| {
48-
features.get("wagi").and_then(|wagi_features| {
49-
match wagi_features.get("route") {
51+
features
52+
.get("wagi")
53+
.and_then(|wagi_features| match wagi_features.get("route") {
5054
Some(route) => {
5155
let handler_info = WagiHandlerInfo {
5256
invoice_id: self.id(),
5357
parcel: parcel.clone(),
5458
route: route.to_owned(),
5559
entrypoint: wagi_features.get("entrypoint").map(|s| s.to_owned()),
5660
allowed_hosts: wagi_features.get("allowed_hosts").map(|h| parse_csv(h)),
57-
required_parcels: parcels_required_for(parcel, &self.group_dependency_map),
61+
argv: wagi_features.get("argv").map(|s| s.to_owned()),
62+
required_parcels: parcels_required_for(
63+
parcel,
64+
&self.group_dependency_map,
65+
),
5866
};
5967
Some(InterestingParcel::WagiHandler(handler_info))
60-
},
68+
}
6169
None => None,
62-
}
63-
})
70+
})
6471
})
6572
}
6673

6774
pub fn parse_wagi_handlers(&self) -> Vec<WagiHandlerInfo> {
68-
self
69-
.top_modules().iter()
75+
self.top_modules()
76+
.iter()
7077
.filter_map(|parcel| self.classify_parcel(parcel))
71-
.map(|parcel| match parcel { // If there are other cases of InterestingParcel this may need to become a filter_map, but right now that makes Clippy mad
78+
.map(|parcel| match parcel {
79+
// If there are other cases of InterestingParcel this may need to become a filter_map, but right now that makes Clippy mad
7280
InterestingParcel::WagiHandler(h) => h,
7381
})
7482
.collect()
@@ -87,31 +95,50 @@ pub struct WagiHandlerInfo {
8795
pub entrypoint: Option<String>,
8896
pub allowed_hosts: Option<Vec<String>>,
8997
pub required_parcels: Vec<Parcel>,
98+
pub argv: Option<String>,
9099
}
91100

92101
impl WagiHandlerInfo {
93102
pub fn asset_parcels(&self) -> Vec<Parcel> {
94-
self.required_parcels.iter().filter(|p| is_file(p)).cloned().collect()
103+
self.required_parcels
104+
.iter()
105+
.filter(|p| is_file(p))
106+
.cloned()
107+
.collect()
95108
}
96109
}
97110

98111
const NO_PARCELS: Vec<Parcel> = vec![];
99112

100113
pub fn is_file(parcel: &Parcel) -> bool {
101-
parcel.label.feature.as_ref().and_then(|features| {
102-
features.get("wagi").map(|wagi_features| {
103-
match wagi_features.get("file") {
104-
Some(s) => s == "true",
105-
_ => false,
106-
}
114+
parcel
115+
.label
116+
.feature
117+
.as_ref()
118+
.and_then(|features| {
119+
features
120+
.get("wagi")
121+
.map(|wagi_features| match wagi_features.get("file") {
122+
Some(s) => s == "true",
123+
_ => false,
124+
})
107125
})
108-
}).unwrap_or(false)
126+
.unwrap_or(false)
109127
}
110128

111-
pub fn parcels_required_for(parcel: &Parcel, full_dep_map: &HashMap<String, Vec<Parcel>>) -> Vec<Parcel> {
129+
pub fn parcels_required_for(
130+
parcel: &Parcel,
131+
full_dep_map: &HashMap<String, Vec<Parcel>>,
132+
) -> Vec<Parcel> {
112133
let mut required = HashSet::new();
113134
for group in parcel.directly_requires() {
114-
required.extend(full_dep_map.get(&group).unwrap_or(&NO_PARCELS).iter().cloned());
135+
required.extend(
136+
full_dep_map
137+
.get(&group)
138+
.unwrap_or(&NO_PARCELS)
139+
.iter()
140+
.cloned(),
141+
);
115142
}
116143
Vec::from_iter(required)
117144
}
@@ -142,25 +169,39 @@ pub fn build_full_memberships(invoice: &Invoice) -> HashMap<String, Vec<Parcel>>
142169
for group in direct_memberships.keys() {
143170
let mut all_members = HashSet::new();
144171
for dep_group in gg_deps.get(group).unwrap() {
145-
all_members.extend(direct_memberships.get(dep_group).unwrap_or(&NO_PARCELS).iter().cloned());
172+
all_members.extend(
173+
direct_memberships
174+
.get(dep_group)
175+
.unwrap_or(&NO_PARCELS)
176+
.iter()
177+
.cloned(),
178+
);
146179
}
147180
full_memberships.insert(group.to_owned(), Vec::from_iter(all_members));
148181
}
149182

150183
full_memberships
151184
}
152185

153-
fn group_to_group_direct_dependencies(direct_memberships: &HashMap<String, Vec<Parcel>>) -> HashMap<String, Vec<String>> {
186+
fn group_to_group_direct_dependencies(
187+
direct_memberships: &HashMap<String, Vec<Parcel>>,
188+
) -> HashMap<String, Vec<String>> {
154189
let mut ggd = HashMap::new();
155190
for (group, members) in direct_memberships {
156-
let mut directs: Vec<_> = members.iter().flat_map(|parcel| parcel.directly_requires()).collect();
191+
let mut directs: Vec<_> = members
192+
.iter()
193+
.flat_map(|parcel| parcel.directly_requires())
194+
.collect();
157195
directs.push(group.to_owned());
158196
ggd.insert(group.to_owned(), directs);
159197
}
160198
ggd
161199
}
162200

163-
fn direct_deps_not_already_in_list(list: &[String], direct_dep_map: &HashMap<String, Vec<String>>) -> Vec<String> {
201+
fn direct_deps_not_already_in_list(
202+
list: &[String],
203+
direct_dep_map: &HashMap<String, Vec<String>>,
204+
) -> Vec<String> {
164205
let mut new_dds = vec![];
165206
for group in list {
166207
if let Some(child_groups) = direct_dep_map.get(group) {
@@ -172,7 +213,9 @@ fn direct_deps_not_already_in_list(list: &[String], direct_dep_map: &HashMap<Str
172213
HashSet::<String>::from_iter(new_dds).into_iter().collect()
173214
}
174215

175-
fn group_to_group_full_dependencies(direct_memberships: &HashMap<String, Vec<Parcel>>) -> HashMap<String, Vec<String>> {
216+
fn group_to_group_full_dependencies(
217+
direct_memberships: &HashMap<String, Vec<Parcel>>,
218+
) -> HashMap<String, Vec<String>> {
176219
let mut ggd = HashMap::new();
177220
let direct_deps = group_to_group_direct_dependencies(direct_memberships);
178221
for (group, directs) in &direct_deps {
@@ -208,7 +251,67 @@ impl ParcelUtils for Parcel {
208251
}
209252

210253
fn parse_csv(text: &str) -> Vec<String> {
211-
text.split(',').map(|v| v.to_owned()).collect() // TODO: trim etc.?
254+
text.split(',').map(|v| v.to_owned()).collect() // TODO: trim etc.?
255+
}
256+
257+
// Bindle client/auth utils, derived from github.com/deislabs/hippo-cli
258+
259+
use std::sync::Arc;
260+
261+
use bindle::client::{
262+
tokens::{HttpBasic, NoToken, TokenManager},
263+
Client, ClientBuilder,
264+
};
265+
266+
#[derive(Clone)]
267+
pub struct BindleConnectionInfo {
268+
base_url: String,
269+
allow_insecure: bool,
270+
token_manager: AnyAuth,
271+
}
272+
273+
impl BindleConnectionInfo {
274+
pub fn new<I: Into<String>>(
275+
base_url: I,
276+
allow_insecure: bool,
277+
username: Option<String>,
278+
password: Option<String>,
279+
) -> Self {
280+
let token_manager: Box<dyn TokenManager + Send + Sync> = match (username, password) {
281+
(Some(u), Some(p)) => Box::new(HttpBasic::new(&u, &p)),
282+
_ => Box::new(NoToken::default()),
283+
};
284+
285+
Self {
286+
base_url: base_url.into(),
287+
allow_insecure,
288+
token_manager: AnyAuth {
289+
token_manager: Arc::new(token_manager),
290+
},
291+
}
292+
}
293+
294+
pub fn client(&self) -> bindle::client::Result<Client<AnyAuth>> {
295+
let builder = ClientBuilder::default()
296+
.http2_prior_knowledge(false)
297+
.danger_accept_invalid_certs(self.allow_insecure);
298+
builder.build(&self.base_url, self.token_manager.clone())
299+
}
300+
}
301+
302+
#[derive(Clone)]
303+
pub struct AnyAuth {
304+
token_manager: Arc<Box<dyn TokenManager + Send + Sync>>,
305+
}
306+
307+
#[async_trait::async_trait]
308+
impl TokenManager for AnyAuth {
309+
async fn apply_auth_header(
310+
&self,
311+
builder: reqwest::RequestBuilder,
312+
) -> bindle::client::Result<reqwest::RequestBuilder> {
313+
self.token_manager.apply_auth_header(builder).await
314+
}
212315
}
213316

214317
#[cfg(test)]
@@ -394,7 +497,9 @@ mod test {
394497
};
395498

396499
let membership_map = build_full_memberships(&inv);
397-
let members = membership_map.get("coffee").expect("there should have been a group called 'coffee'");
500+
let members = membership_map
501+
.get("coffee")
502+
.expect("there should have been a group called 'coffee'");
398503
assert_eq!(2, members.len());
399504
}
400505
}

0 commit comments

Comments
 (0)