mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
link: return UResult from uumain() function
This commit is contained in:
parent
f7584cb755
commit
a882b0cf3e
2 changed files with 8 additions and 22 deletions
|
@ -4,14 +4,11 @@
|
||||||
// *
|
// *
|
||||||
// * For the full copyright and license information, please view the LICENSE
|
// * For the full copyright and license information, please view the LICENSE
|
||||||
// * file that was distributed with this source code.
|
// * file that was distributed with this source code.
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
extern crate uucore;
|
|
||||||
|
|
||||||
use clap::{crate_version, App, Arg};
|
use clap::{crate_version, App, Arg};
|
||||||
use std::fs::hard_link;
|
use std::fs::hard_link;
|
||||||
use std::io::Error;
|
|
||||||
use std::path::Path;
|
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.";
|
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())
|
format!("{0} FILE1 FILE2", uucore::execution_phrase())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn normalize_error_message(e: Error) -> String {
|
#[uucore_procs::gen_uumain]
|
||||||
match e.raw_os_error() {
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
Some(2) => String::from("No such file or directory (os error 2)"),
|
|
||||||
_ => format!("{}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
|
||||||
let usage = usage();
|
let usage = usage();
|
||||||
let matches = uu_app().usage(&usage[..]).get_matches_from(args);
|
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 old = Path::new(files[0]);
|
||||||
let new = Path::new(files[1]);
|
let new = Path::new(files[1]);
|
||||||
|
|
||||||
match hard_link(old, new) {
|
hard_link(old, new)
|
||||||
Ok(_) => 0,
|
.map_err_context(|| format!("cannot create link {} to {}", new.quote(), old.quote()))
|
||||||
Err(err) => {
|
|
||||||
show_error!("{}", normalize_error_message(err));
|
|
||||||
1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> App<'static, 'static> {
|
pub fn uu_app() -> App<'static, 'static> {
|
||||||
|
|
|
@ -23,7 +23,7 @@ fn test_link_no_circular() {
|
||||||
|
|
||||||
ucmd.args(&[link, link])
|
ucmd.args(&[link, link])
|
||||||
.fails()
|
.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));
|
assert!(!at.file_exists(link));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ fn test_link_nonexistent_file() {
|
||||||
|
|
||||||
ucmd.args(&[file, link])
|
ucmd.args(&[file, link])
|
||||||
.fails()
|
.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(file));
|
||||||
assert!(!at.file_exists(link));
|
assert!(!at.file_exists(link));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue