mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
readlink: return UResult from uumain() function
This commit is contained in:
parent
2a7831bd94
commit
980708cdee
1 changed files with 14 additions and 13 deletions
|
@ -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()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue