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::io::{stdout, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
|
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
|
||||||
use uucore::fs::{canonicalize, MissingHandling, ResolveMode};
|
use uucore::fs::{canonicalize, MissingHandling, ResolveMode};
|
||||||
|
|
||||||
const ABOUT: &str = "Print value of a symbolic link or canonical file name.";
|
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())
|
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 usage = usage();
|
||||||
let matches = uu_app().usage(&usage[..]).get_matches_from(args);
|
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())
|
.map(|v| v.map(ToString::to_string).collect())
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
crash!(
|
return Err(UUsageError::new(1, "missing operand"));
|
||||||
1,
|
|
||||||
"missing operand\nTry '{} --help' for more information",
|
|
||||||
uucore::execution_phrase()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if no_newline && files.len() > 1 && !silent {
|
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)
|
canonicalize(&p, can_mode, res_mode)
|
||||||
};
|
};
|
||||||
match path_result {
|
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) => {
|
Err(err) => {
|
||||||
if verbose {
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uu_app() -> App<'static, 'static> {
|
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))
|
.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();
|
let path = path.to_str().unwrap();
|
||||||
if use_zero {
|
if use_zero {
|
||||||
print!("{}\0", path);
|
print!("{}\0", path);
|
||||||
|
@ -170,5 +171,5 @@ fn show(path: &Path, no_newline: bool, use_zero: bool) {
|
||||||
} else {
|
} else {
|
||||||
println!("{}", path);
|
println!("{}", path);
|
||||||
}
|
}
|
||||||
crash_if_err!(1, stdout().flush());
|
stdout().flush()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue