Skip to content

Commit 6486a00

Browse files
refactor: improve error message, use anyhow::ensure
1 parent b34419e commit 6486a00

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

crates/wkg/src/main.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::{io::Seek, path::PathBuf};
22

3-
use anyhow::{ensure, Context};
3+
use anyhow::{Context, ensure};
44
use clap::{Args, Parser, Subcommand, ValueEnum};
55
use futures_util::TryStreamExt;
66
use tokio::io::AsyncWriteExt;
77
use tracing::level_filters::LevelFilter;
88
use wasm_pkg_client::{
9-
caching::{CachingClient, FileCache},
109
Client, PublishOpts,
10+
caching::{CachingClient, FileCache},
1111
};
1212
use wasm_pkg_common::{
1313
self,
@@ -358,28 +358,34 @@ impl GetArgs {
358358
tracing::debug!(?release, "Fetched release details");
359359

360360
let output_trailing_slash = self.output.as_os_str().to_string_lossy().ends_with('/');
361-
let parent_dir = if output_trailing_slash {
362-
let parent_dir = self.output.as_path();
363-
if !tokio::fs::try_exists(parent_dir).await? {
364-
tokio::fs::create_dir_all(parent_dir).await
365-
.context("Failed to create output dir")?
361+
let parent_dir_path = if output_trailing_slash {
362+
// TODO(fix): TOCTOU on directory, requires platform open dir flags
363+
let parent_dir_path = self.output.as_path();
364+
if !tokio::fs::try_exists(parent_dir_path).await? {
365+
tokio::fs::create_dir_all(parent_dir_path)
366+
.await
367+
.with_context(|| {
368+
format!(
369+
"failed to create output dir @ [{}]",
370+
parent_dir_path.display()
371+
)
372+
})?;
366373
}
367-
parent_dir
374+
parent_dir_path
368375
} else {
369376
self.output
370377
.parent()
371-
.context("Failed to resolve output parent dir")?
378+
.context("Failed to resolve non-trailing-slash output parent dir")?
372379
};
373380

374-
if !parent_dir.exists() {
375-
anyhow::bail!(
376-
"output directory {:?} does not exist; create it first or choose a different path",
377-
parent_dir
378-
);
379-
}
381+
anyhow::ensure!(
382+
parent_dir_path.exists(),
383+
"output directory {:?} does not exist; create it first or choose a different path",
384+
parent_dir_path
385+
);
380386

381387
let (tmp_file, tmp_path) =
382-
tempfile::NamedTempFile::with_prefix_in(".wkg-get", parent_dir)?.into_parts();
388+
tempfile::NamedTempFile::with_prefix_in(".wkg-get", parent_dir_path)?.into_parts();
383389
tracing::debug!(?tmp_path, "Created temporary file");
384390

385391
let mut content_stream = client.get_content(&package, &release).await?;

0 commit comments

Comments
 (0)