diff --git a/src/uu/link/src/link.rs b/src/uu/link/src/link.rs index 73e81b107..a54b71999 100644 --- a/src/uu/link/src/link.rs +++ b/src/uu/link/src/link.rs @@ -4,14 +4,11 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. - -#[macro_use] -extern crate uucore; - use clap::{crate_version, App, Arg}; use std::fs::hard_link; -use std::io::Error; use std::path::Path; +use uucore::display::Quotable; +use uucore::error::{FromIo, UResult}; static ABOUT: &str = "Call the link function to create a link named FILE2 to an existing FILE1."; @@ -23,14 +20,8 @@ fn usage() -> String { format!("{0} FILE1 FILE2", uucore::execution_phrase()) } -pub fn normalize_error_message(e: Error) -> String { - match e.raw_os_error() { - Some(2) => String::from("No such file or directory (os error 2)"), - _ => format!("{}", e), - } -} - -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); @@ -41,13 +32,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let old = Path::new(files[0]); let new = Path::new(files[1]); - match hard_link(old, new) { - Ok(_) => 0, - Err(err) => { - show_error!("{}", normalize_error_message(err)); - 1 - } - } + hard_link(old, new) + .map_err_context(|| format!("cannot create link {} to {}", new.quote(), old.quote())) } pub fn uu_app() -> App<'static, 'static> { diff --git a/tests/by-util/test_link.rs b/tests/by-util/test_link.rs index 6ac3f35cc..3219a6591 100644 --- a/tests/by-util/test_link.rs +++ b/tests/by-util/test_link.rs @@ -23,7 +23,7 @@ fn test_link_no_circular() { ucmd.args(&[link, link]) .fails() - .stderr_is("link: No such file or directory (os error 2)\n"); + .stderr_is("link: cannot create link 'test_link_no_circular' to 'test_link_no_circular': No such file or directory"); assert!(!at.file_exists(link)); } @@ -35,7 +35,7 @@ fn test_link_nonexistent_file() { ucmd.args(&[file, link]) .fails() - .stderr_is("link: No such file or directory (os error 2)\n"); + .stderr_only("link: cannot create link 'test_link_nonexistent_file_link' to 'test_link_nonexistent_file': No such file or directory"); assert!(!at.file_exists(file)); assert!(!at.file_exists(link)); }