Skip to content

Commit 7cc3d65

Browse files
wrap all errors in compress command with eyre
1 parent 21ec823 commit 7cc3d65

File tree

3 files changed

+88
-35
lines changed

3 files changed

+88
-35
lines changed

cli/clite/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ members = ["."]
2222
name = "zip-clite"
2323

2424
[dependencies]
25-
eyre = "0.6"
25+
color-eyre = "0.6"
2626

2727
[dependencies.zip-cli]
2828
path = ".."

cli/clite/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use std::env;
22
use std::io;
33

4-
use eyre::Report;
4+
use color_eyre::eyre::{self, Report};
55

66
use zip_cli::args::*;
77
use zip_cli::compress::execute_compress;
88
use zip_cli::ErrHandle;
99

1010
fn main() -> Result<(), Report> {
11+
color_eyre::install()?;
12+
1113
let ZipCli { verbose, command } = ZipCli::parse_argv(env::args_os())?;
1214
let mut err = if verbose {
1315
ErrHandle::Output(io::stderr())

cli/src/compress.rs

Lines changed: 84 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
path::Path,
55
};
66

7-
use color_eyre::eyre::{self, Report};
7+
use color_eyre::eyre::{eyre, Report, WrapErr};
88

99
use zip::{
1010
unstable::path_to_string,
@@ -59,9 +59,14 @@ fn enter_recursive_dir_entries(
5959
err,
6060
"writing top-level directory entry for {base_dirname:?}"
6161
)?;
62-
writer.add_directory(&base_dirname, options)?;
62+
writer
63+
.add_directory(&base_dirname, options)
64+
.wrap_err_with(|| eyre!("{base_dirname}"))?;
6365

64-
let mut readdir_stack: Vec<(fs::ReadDir, String)> = vec![(fs::read_dir(root)?, base_dirname)];
66+
let mut readdir_stack: Vec<(fs::ReadDir, String)> = vec![(
67+
fs::read_dir(root).wrap_err_with(|| eyre!("{}", root.display()))?,
68+
base_dirname,
69+
)];
6570
while let Some((mut readdir, top_component)) = readdir_stack.pop() {
6671
if let Some(dir_entry) = readdir.next().transpose()? {
6772
let mut components: Vec<&str> = readdir_stack.iter().map(|(_, s)| s.as_ref()).collect();
@@ -70,36 +75,51 @@ fn enter_recursive_dir_entries(
7075
let entry_basename: String = dir_entry
7176
.file_name()
7277
.into_string()
73-
.map_err(|name| eyre::eyre!("failed to decode basename {name:?}"))?;
78+
.map_err(|name| eyre!("failed to decode basename {name:?}"))?;
7479
components.push(&entry_basename);
7580
let full_path: String = components.join("/");
7681
readdir_stack.push((readdir, top_component));
7782

78-
let file_type = dir_entry.file_type()?;
83+
let file_type = dir_entry
84+
.file_type()
85+
.wrap_err_with(|| eyre!("{}", dir_entry.path().display()))?;
7986
if file_type.is_symlink() {
80-
let target: String = path_to_string(fs::read_link(dir_entry.path())?).into();
87+
let target: String = path_to_string(
88+
fs::read_link(dir_entry.path())
89+
.wrap_err_with(|| eyre!("{}", dir_entry.path().display()))?,
90+
)
91+
.into();
8192
writeln!(
8293
err,
8394
"writing recursive symlink entry with name {full_path:?} and target {target:?}"
8495
)?;
85-
writer.add_symlink(full_path, target, options)?;
96+
writer
97+
.add_symlink(&full_path, &target, options)
98+
.wrap_err_with(|| eyre!("{full_path}->{target}"))?;
8699
} else if file_type.is_file() {
87100
writeln!(err, "writing recursive file entry with name {full_path:?}")?;
88-
writer.start_file(full_path, options)?;
89-
let mut f = fs::File::open(dir_entry.path())?;
90-
io::copy(&mut f, writer)?;
101+
writer
102+
.start_file(&full_path, options)
103+
.wrap_err_with(|| eyre!("{full_path}"))?;
104+
let mut f = fs::File::open(dir_entry.path())
105+
.wrap_err_with(|| eyre!("{}", dir_entry.path().display()))?;
106+
io::copy(&mut f, writer)
107+
.wrap_err_with(|| eyre!("{}", dir_entry.path().display()))?;
91108
} else {
92109
assert!(file_type.is_dir());
93110
writeln!(
94111
err,
95112
"writing recursive directory entry with name {full_path:?}"
96113
)?;
97-
writer.add_directory(full_path, options)?;
114+
writer
115+
.add_directory(&full_path, options)
116+
.wrap_err_with(|| eyre!("{full_path}"))?;
98117
writeln!(
99118
err,
100119
"adding subdirectories depth-first for recursive directory entry {entry_basename:?}"
101120
)?;
102-
let new_readdir = fs::read_dir(dir_entry.path())?;
121+
let new_readdir = fs::read_dir(dir_entry.path())
122+
.wrap_err_with(|| eyre!("{}", dir_entry.path().display()))?;
103123
readdir_stack.push((new_readdir, entry_basename));
104124
}
105125
}
@@ -121,7 +141,9 @@ pub fn execute_compress(
121141
let out = match output_path {
122142
Some(path) => {
123143
writeln!(err, "writing compressed zip to output file path {path:?}")?;
124-
OutputHandle::File(fs::File::create(path)?)
144+
OutputHandle::File(
145+
fs::File::create(&path).wrap_err_with(|| eyre!("{}", path.display()))?,
146+
)
125147
}
126148
None => {
127149
writeln!(
@@ -189,7 +211,9 @@ pub fn execute_compress(
189211
let dirname = last_name.take().unwrap_or_else(|| {
190212
Compress::exit_arg_invalid("no name provided before dir entry")
191213
});
192-
writer.add_directory(dirname, options)?;
214+
writer
215+
.add_directory(&dirname, options)
216+
.wrap_err_with(|| eyre!("{dirname}"))?;
193217
}
194218
CompressionArg::Symlink => {
195219
writeln!(err, "setting symlink flag for next entry")?;
@@ -215,7 +239,9 @@ pub fn execute_compress(
215239
"writing immediate symlink entry with name {name:?} and target {target:?}"
216240
)?;
217241
/* TODO: .add_symlink() should support OsString targets! */
218-
writer.add_symlink(name, target, options)?;
242+
writer
243+
.add_symlink(&name, &target, options)
244+
.wrap_err_with(|| eyre!("{name}->{target}"))?;
219245
symlink_flag = false;
220246
} else {
221247
/* This is a file entry. */
@@ -224,8 +250,12 @@ pub fn execute_compress(
224250
"writing immediate file entry with name {name:?} and data {data:?}"
225251
)?;
226252
let data = data.into_encoded_bytes();
227-
writer.start_file(name, options)?;
228-
writer.write_all(data.as_ref())?;
253+
writer
254+
.start_file(&name, options)
255+
.wrap_err_with(|| eyre!("{name}"))?;
256+
writer
257+
.write_all(data.as_ref())
258+
.wrap_err_with(|| eyre!("{name}"))?;
229259
}
230260
}
231261
CompressionArg::FilePath(path) => {
@@ -234,19 +264,27 @@ pub fn execute_compress(
234264
.unwrap_or_else(|| path_to_string(&path).into());
235265
if symlink_flag {
236266
/* This is a symlink entry. */
237-
let target: String = path_to_string(fs::read_link(&path)?).into();
267+
let target: String = path_to_string(
268+
fs::read_link(&path).wrap_err_with(|| eyre!("{}", path.display()))?,
269+
)
270+
.into();
238271
writeln!(err, "writing symlink entry from path {path:?} with name {name:?} and target {target:?}")?;
239-
writer.add_symlink(name, target, options)?;
272+
writer
273+
.add_symlink(&name, &target, options)
274+
.wrap_err_with(|| eyre!("{name}->{target}"))?;
240275
symlink_flag = false;
241276
} else {
242277
/* This is a file entry. */
243278
writeln!(
244279
err,
245280
"writing file entry from path {path:?} with name {name:?}"
246281
)?;
247-
writer.start_file(name, options)?;
248-
let mut f = fs::File::open(path)?;
249-
io::copy(&mut f, &mut writer)?;
282+
writer
283+
.start_file(&name, options)
284+
.wrap_err_with(|| eyre!("{name}"))?;
285+
let mut f =
286+
fs::File::open(&path).wrap_err_with(|| eyre!("{}", path.display()))?;
287+
io::copy(&mut f, &mut writer).wrap_err_with(|| eyre!("{}", path.display()))?;
250288
}
251289
}
252290
CompressionArg::RecursiveDirPath(r) => {
@@ -257,7 +295,8 @@ pub fn execute_compress(
257295
err,
258296
"writing recursive dir entries for path {r:?} with name {last_name:?}"
259297
)?;
260-
enter_recursive_dir_entries(err, last_name.take(), &r, &mut writer, options)?;
298+
enter_recursive_dir_entries(err, last_name.take(), &r, &mut writer, options)
299+
.wrap_err_with(|| eyre!("{}", r.display()))?;
261300
}
262301
}
263302
}
@@ -270,36 +309,48 @@ pub fn execute_compress(
270309
))
271310
}
272311
for pos_arg in positional_paths.into_iter() {
273-
let file_type = fs::symlink_metadata(&pos_arg)?.file_type();
312+
let file_type = fs::symlink_metadata(&pos_arg)
313+
.wrap_err_with(|| eyre!("{}", pos_arg.display()))?
314+
.file_type();
274315
if file_type.is_symlink() {
275-
let target = fs::read_link(&pos_arg)?;
316+
let target =
317+
fs::read_link(&pos_arg).wrap_err_with(|| eyre!("{}", pos_arg.display()))?;
276318
writeln!(
277319
err,
278320
"writing positional symlink entry with path {pos_arg:?} and target {target:?}"
279321
)?;
280-
writer.add_symlink_from_path(pos_arg, target, options)?;
322+
writer
323+
.add_symlink_from_path(&pos_arg, &target, options)
324+
.wrap_err_with(|| eyre!("{}->{}", pos_arg.display(), target.display()))?;
281325
} else if file_type.is_file() {
282326
writeln!(err, "writing positional file entry with path {pos_arg:?}")?;
283-
writer.start_file_from_path(&pos_arg, options)?;
284-
let mut f = fs::File::open(pos_arg)?;
285-
io::copy(&mut f, &mut writer)?;
327+
writer
328+
.start_file_from_path(&pos_arg, options)
329+
.wrap_err_with(|| eyre!("{}", pos_arg.display()))?;
330+
let mut f =
331+
fs::File::open(&pos_arg).wrap_err_with(|| eyre!("{}", pos_arg.display()))?;
332+
io::copy(&mut f, &mut writer).wrap_err_with(|| eyre!("{}", pos_arg.display()))?;
286333
} else {
287334
assert!(file_type.is_dir());
288335
writeln!(
289336
err,
290337
"writing positional recursive dir entry for {pos_arg:?}"
291338
)?;
292-
enter_recursive_dir_entries(err, None, &pos_arg, &mut writer, options)?;
339+
enter_recursive_dir_entries(err, None, &pos_arg, &mut writer, options)
340+
.wrap_err_with(|| eyre!("{}", pos_arg.display()))?;
293341
}
294342
}
295343

296-
let handle = writer.finish()?;
344+
let handle = writer
345+
.finish()
346+
.wrap_err("failed to write zip to output handle")?;
297347
match handle {
298348
OutputHandle::File(_) => (),
299349
OutputHandle::InMem(mut cursor) => {
300-
cursor.rewind()?;
350+
cursor.rewind().wrap_err("failed to rewind cursor")?;
301351
let mut stdout = io::stdout().lock();
302-
io::copy(&mut cursor, &mut stdout)?;
352+
io::copy(&mut cursor, &mut stdout)
353+
.wrap_err("failed to copy contents of cursor to stdout")?;
303354
}
304355
}
305356

0 commit comments

Comments
 (0)