From 980708cdee69f3695dc9c465293e8c2fc00eb34b Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Wed, 29 Dec 2021 14:37:30 -0500 Subject: [PATCH] readlink: return UResult from uumain() function --- src/uu/readlink/src/readlink.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/uu/readlink/src/readlink.rs b/src/uu/readlink/src/readlink.rs index 4dc2104cc..85b436be1 100644 --- a/src/uu/readlink/src/readlink.rs +++ b/src/uu/readlink/src/readlink.rs @@ -15,6 +15,7 @@ use std::fs; use std::io::{stdout, Write}; use std::path::{Path, PathBuf}; use uucore::display::Quotable; +use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; use uucore::fs::{canonicalize, MissingHandling, ResolveMode}; const ABOUT: &str = "Print value of a symbolic link or canonical file name."; @@ -33,7 +34,8 @@ fn usage() -> String { format!("{0} [OPTION]... [FILE]...", uucore::execution_phrase()) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let usage = usage(); let matches = uu_app().usage(&usage[..]).get_matches_from(args); @@ -64,11 +66,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); if files.is_empty() { - crash!( - 1, - "missing operand\nTry '{} --help' for more information", - uucore::execution_phrase() - ); + return Err(UUsageError::new(1, "missing operand")); } if no_newline && files.len() > 1 && !silent { @@ -84,17 +82,20 @@ pub fn uumain(args: impl uucore::Args) -> i32 { canonicalize(&p, can_mode, res_mode) }; match path_result { - Ok(path) => show(&path, no_newline, use_zero), + Ok(path) => show(&path, no_newline, use_zero).map_err_context(String::new)?, Err(err) => { if verbose { - show_error!("{}: errno {}", f.maybe_quote(), err.raw_os_error().unwrap()); + return Err(USimpleError::new( + 1, + format!("{}: errno {}", f.maybe_quote(), err.raw_os_error().unwrap()), + )); + } else { + return Err(1.into()); } - return 1; } } } - - 0 + Ok(()) } pub fn uu_app() -> App<'static, 'static> { @@ -161,7 +162,7 @@ pub fn uu_app() -> App<'static, 'static> { .arg(Arg::with_name(ARG_FILES).multiple(true).takes_value(true)) } -fn show(path: &Path, no_newline: bool, use_zero: bool) { +fn show(path: &Path, no_newline: bool, use_zero: bool) -> std::io::Result<()> { let path = path.to_str().unwrap(); if use_zero { print!("{}\0", path); @@ -170,5 +171,5 @@ fn show(path: &Path, no_newline: bool, use_zero: bool) { } else { println!("{}", path); } - crash_if_err!(1, stdout().flush()); + stdout().flush() }