Skip to content

Commit f9d0ac1

Browse files
committed
example: bugfix, clean up output
1 parent 4360d16 commit f9d0ac1

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

aws/examples/s3.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ async fn main() -> Result<()> {
4141
let client = Client::new(&config);
4242

4343
match opts.command.as_ref().unwrap_or(&Command::List) {
44-
Command::List => list(&opts, &client).await,
44+
Command::List => {
45+
let output = list(&opts, &client).await?;
46+
print!("{}", output);
47+
}
4548
Command::Get { key, out } => {
4649
let contents = get(&opts, &client, key).await?;
4750
let output: &str = if let Some(out) = out {
@@ -50,24 +53,25 @@ async fn main() -> Result<()> {
5053
key.as_str()
5154
};
5255
std::fs::write(output, contents)?;
53-
Ok(())
5456
}
5557
}
58+
Ok(())
5659
}
5760

58-
async fn list(opts: &Opts, client: &Client) -> Result<()> {
61+
async fn list(opts: &Opts, client: &Client) -> Result<String> {
5962
let mut listing = client
6063
.list_objects_v2()
6164
.bucket(opts.bucket.clone())
6265
.into_paginator()
6366
.send();
6467

65-
println!("key\tetag\tlast_modified\tstorage_class");
68+
let mut output = String::new();
69+
output += "key\tetag\tlast_modified\tstorage_class\n";
6670
while let Some(res) = listing.next().await {
6771
let object = res?;
6872
for item in object.contents() {
69-
println!(
70-
"{}\t{}\t{}\t{}",
73+
output += &format!(
74+
"{}\t{}\t{}\t{}\n",
7175
item.key().unwrap_or_default(),
7276
item.e_tag().unwrap_or_default(),
7377
item.last_modified()
@@ -79,7 +83,7 @@ async fn list(opts: &Opts, client: &Client) -> Result<()> {
7983
);
8084
}
8185
}
82-
Ok(())
86+
Ok(output)
8387
}
8488

8589
async fn get(opts: &Opts, client: &Client, key: &str) -> Result<Vec<u8>> {

test-programs/tests/aws_s3.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ use anyhow::Result;
22
use std::path::Path;
33
use std::process::Command;
44

5+
fn run_s3_example() -> Command {
6+
let mut command = Command::new("wasmtime");
7+
command.arg("run");
8+
command.arg("-Shttp");
9+
command.args(["--env", "AWS_ACCESS_KEY_ID"]);
10+
command.args(["--env", "AWS_SECRET_ACCESS_KEY"]);
11+
command.args(["--env", "AWS_SESSION_TOKEN"]);
12+
command.args(["--dir", ".::."]);
13+
command.arg(test_programs::aws::S3);
14+
command
15+
}
16+
517
#[test_log::test]
618
fn aws_s3() -> Result<()> {
719
// bucket list command
8-
let output = Command::new("wasmtime")
9-
.arg("run")
10-
.arg("-Shttp")
11-
.args(["--env", "AWS_ACCESS_KEY_ID"])
12-
.args(["--env", "AWS_SECRET_ACCESS_KEY"])
13-
.args(["--env", "AWS_SESSION_TOKEN"])
14-
.arg(test_programs::aws::S3)
20+
let output = run_s3_example()
1521
.arg(format!(
1622
"--region={}",
1723
std::env::var("AWS_REGION").unwrap_or_else(|_| "us-west-2".to_owned())
@@ -30,14 +36,7 @@ fn aws_s3() -> Result<()> {
3036
assert!(stdout.contains("shoug.jpg"));
3137

3238
// bucket get command
33-
let output = Command::new("wasmtime")
34-
.arg("run")
35-
.arg("-Shttp")
36-
.args(["--env", "AWS_ACCESS_KEY_ID"])
37-
.args(["--env", "AWS_SECRET_ACCESS_KEY"])
38-
.args(["--env", "AWS_ACCESS_KEY_ID"])
39-
.args(["--dir", ".::."])
40-
.arg(test_programs::aws::S3)
39+
let output = run_s3_example()
4140
.arg(format!(
4241
"--region={}",
4342
std::env::var("AWS_REGION").unwrap_or_else(|_| "us-west-2".to_owned())

0 commit comments

Comments
 (0)