diff options
author | Terts Diepraam <terts.diepraam@gmail.com> | 2021-07-13 17:29:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-13 17:29:26 +0200 |
commit | f5ba8b181133392b4073daa9e936535b10edb7ff (patch) | |
tree | cb1fa58a1c960941f87646444a5af37fd3cd35f4 | |
parent | 92dafbd8a308f61ae1fbceda0479e619c88579c7 (diff) | |
parent | a6b0cf29e047934ad50382d8259cadf4e12b4b17 (diff) |
Merge pull request #2499 from 353fc443/uresult-kill
Add Uresult for false, echo, pwd, true and kill
-rw-r--r-- | src/uu/echo/src/echo.rs | 15 | ||||
-rw-r--r-- | src/uu/false/src/false.rs | 12 | ||||
-rw-r--r-- | src/uu/kill/src/kill.rs | 63 | ||||
-rw-r--r-- | src/uu/pwd/src/pwd.rs | 24 | ||||
-rw-r--r-- | src/uu/true/src/true.rs | 12 |
5 files changed, 84 insertions, 42 deletions
diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 8c976c2b4..acdd22948 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -6,6 +6,9 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +// clippy bug https://github.com/rust-lang/rust-clippy/issues/7422 +#![allow(clippy::nonstandard_macro_braces)] + #[macro_use] extern crate uucore; @@ -13,6 +16,7 @@ use clap::{crate_version, App, Arg}; use std::io::{self, Write}; use std::iter::Peekable; use std::str::Chars; +use uucore::error::{FromIo, UResult}; use uucore::InvalidEncodingHandling; const NAME: &str = "echo"; @@ -113,7 +117,8 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result<bool> { Ok(should_stop) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = args .collect_str(InvalidEncodingHandling::ConvertLossy) .accept_any(); @@ -126,13 +131,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { None => vec!["".to_string()], }; - match execute(no_newline, escaped, values) { - Ok(_) => 0, - Err(f) => { - show_error!("{}", f); - 1 - } - } + execute(no_newline, escaped, values).map_err_context(|| "could not write to stdout".to_string()) } pub fn uu_app() -> App<'static, 'static> { diff --git a/src/uu/false/src/false.rs b/src/uu/false/src/false.rs index 17c681129..232431142 100644 --- a/src/uu/false/src/false.rs +++ b/src/uu/false/src/false.rs @@ -5,12 +5,20 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. +// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 +#![allow(clippy::nonstandard_macro_braces)] + +#[macro_use] +extern crate uucore; + use clap::App; +use uucore::error::{UError, UResult}; use uucore::executable; -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { uu_app().get_matches_from(args); - 1 + Err(UError::from(1)) } pub fn uu_app() -> App<'static, 'static> { diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index 92868efdb..88eb08fa7 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -7,20 +7,21 @@ // spell-checker:ignore (ToDO) signalname pids +// clippy bug https://github.com/rust-lang/rust-clippy/issues/7422 +#![allow(clippy::nonstandard_macro_braces)] + #[macro_use] extern crate uucore; use clap::{crate_version, App, Arg}; use libc::{c_int, pid_t}; use std::io::Error; +use uucore::error::{UResult, USimpleError}; use uucore::signals::ALL_SIGNALS; use uucore::InvalidEncodingHandling; static ABOUT: &str = "Send signal to processes or list information about signals."; -static EXIT_OK: i32 = 0; -static EXIT_ERR: i32 = 1; - pub mod options { pub static PIDS_OR_SIGNALS: &str = "pids_of_signals"; pub static LIST: &str = "list"; @@ -36,7 +37,8 @@ pub enum Mode { List, } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = args .collect_str(InvalidEncodingHandling::Ignore) .accept_any(); @@ -66,13 +68,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 { (None, Some(s)) => s.to_owned(), (None, None) => "TERM".to_owned(), }; - return kill(&sig, &pids_or_signals); + kill(&sig, &pids_or_signals) + } + Mode::Table => { + table(); + Ok(()) } - Mode::Table => table(), Mode::List => list(pids_or_signals.get(0).cloned()), } - - EXIT_OK } pub fn uu_app() -> App<'static, 'static> { @@ -139,20 +142,23 @@ fn table() { println!(); } } - println!() + println!(); } -fn print_signal(signal_name_or_value: &str) { +fn print_signal(signal_name_or_value: &str) -> UResult<()> { for (value, &signal) in ALL_SIGNALS.iter().enumerate() { if signal == signal_name_or_value || (format!("SIG{}", signal)) == signal_name_or_value { println!("{}", value); - exit!(EXIT_OK as i32) + return Ok(()); } else if signal_name_or_value == value.to_string() { println!("{}", signal); - exit!(EXIT_OK as i32) + return Ok(()); } } - crash!(EXIT_ERR, "unknown signal name {}", signal_name_or_value) + Err(USimpleError::new( + 1, + format!("unknown signal name {}", signal_name_or_value), + )) } fn print_signals() { @@ -170,30 +176,41 @@ fn print_signals() { } } -fn list(arg: Option<String>) { +fn list(arg: Option<String>) -> UResult<()> { match arg { Some(ref x) => print_signal(x), - None => print_signals(), - }; + None => { + print_signals(); + Ok(()) + } + } } -fn kill(signalname: &str, pids: &[String]) -> i32 { - let mut status = 0; +fn kill(signalname: &str, pids: &[String]) -> UResult<()> { let optional_signal_value = uucore::signals::signal_by_name_or_value(signalname); let signal_value = match optional_signal_value { Some(x) => x, - None => crash!(EXIT_ERR, "unknown signal name {}", signalname), + None => { + return Err(USimpleError::new( + 1, + format!("unknown signal name {}", signalname), + )); + } }; for pid in pids { match pid.parse::<usize>() { Ok(x) => { if unsafe { libc::kill(x as pid_t, signal_value as c_int) } != 0 { - show_error!("{}", Error::last_os_error()); - status = 1; + show!(USimpleError::new(1, format!("{}", Error::last_os_error()))); } } - Err(e) => crash!(EXIT_ERR, "failed to parse argument {}: {}", pid, e), + Err(e) => { + return Err(USimpleError::new( + 1, + format!("failed to parse argument {}: {}", pid, e), + )); + } }; } - status + Ok(()) } diff --git a/src/uu/pwd/src/pwd.rs b/src/uu/pwd/src/pwd.rs index 764a63a88..c72cc64e2 100644 --- a/src/uu/pwd/src/pwd.rs +++ b/src/uu/pwd/src/pwd.rs @@ -5,6 +5,9 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. +// clippy bug https://github.com/rust-lang/rust-clippy/issues/7422 +#![allow(clippy::nonstandard_macro_braces)] + #[macro_use] extern crate uucore; @@ -13,6 +16,8 @@ use std::env; use std::io; use std::path::{Path, PathBuf}; +use uucore::error::{FromIo, UResult, USimpleError}; + static ABOUT: &str = "Display the full filename of the current working directory."; static OPT_LOGICAL: &str = "logical"; static OPT_PHYSICAL: &str = "physical"; @@ -36,7 +41,8 @@ fn get_usage() -> String { format!("{0} [OPTION]... FILE...", executable!()) } -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { let usage = get_usage(); let matches = uu_app().usage(&usage[..]).get_matches_from(args); @@ -46,16 +52,20 @@ pub fn uumain(args: impl uucore::Args) -> i32 { if matches.is_present(OPT_LOGICAL) { println!("{}", logical_path.display()); } else { - match absolute_path(&logical_path) { - Ok(physical_path) => println!("{}", physical_path.display()), - Err(e) => crash!(1, "failed to get absolute path {}", e), - }; + let physical_path = absolute_path(&logical_path) + .map_err_context(|| "failed to get absolute path".to_string())?; + println!("{}", physical_path.display()); } } - Err(e) => crash!(1, "failed to get current directory {}", e), + Err(e) => { + return Err(USimpleError::new( + 1, + format!("failed to get current directory {}", e), + )) + } }; - 0 + Ok(()) } pub fn uu_app() -> App<'static, 'static> { diff --git a/src/uu/true/src/true.rs b/src/uu/true/src/true.rs index ea53b0075..e6b7b9025 100644 --- a/src/uu/true/src/true.rs +++ b/src/uu/true/src/true.rs @@ -5,12 +5,20 @@ // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. +// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 +#![allow(clippy::nonstandard_macro_braces)] + +#[macro_use] +extern crate uucore; + use clap::App; +use uucore::error::UResult; use uucore::executable; -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { uu_app().get_matches_from(args); - 0 + Ok(()) } pub fn uu_app() -> App<'static, 'static> { |