From 330db2eb3ecf72355d8354f909c36015de8da90a Mon Sep 17 00:00:00 2001 From: 353fc443 <353fc443@pm.me> Date: Fri, 2 Jul 2021 08:19:33 +0000 Subject: [PATCH 1/5] Added UResult for hostname --- src/uu/hostname/src/hostname.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/uu/hostname/src/hostname.rs b/src/uu/hostname/src/hostname.rs index fe477d7b5..69602fc0a 100644 --- a/src/uu/hostname/src/hostname.rs +++ b/src/uu/hostname/src/hostname.rs @@ -14,6 +14,7 @@ use clap::{crate_version, App, Arg, ArgMatches}; use std::collections::hash_set::HashSet; use std::net::ToSocketAddrs; use std::str; +use uucore::error::{UResult, USimpleError}; #[cfg(windows)] use winapi::shared::minwindef::MAKEWORD; @@ -28,15 +29,15 @@ static OPT_FQDN: &str = "fqdn"; static OPT_SHORT: &str = "short"; static OPT_HOST: &str = "host"; -pub fn uumain(args: impl uucore::Args) -> i32 { +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { #![allow(clippy::let_and_return)] #[cfg(windows)] unsafe { #[allow(deprecated)] let mut data = std::mem::uninitialized(); if WSAStartup(MAKEWORD(2, 2), &mut data as *mut _) != 0 { - eprintln!("Failed to start Winsock 2.2"); - return 1; + return Err(USimpleError::new(1, format!("Failed to start Winsock 2.2"))); } } let result = execute(args); @@ -50,7 +51,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 { fn get_usage() -> String { format!("{0} [OPTION]... [HOSTNAME]", executable!()) } -fn execute(args: impl uucore::Args) -> i32 { +fn execute(args: impl uucore::Args) -> UResult<()> { let usage = get_usage(); let matches = uu_app().usage(&usage[..]).get_matches_from(args); @@ -58,10 +59,9 @@ fn execute(args: impl uucore::Args) -> i32 { None => display_hostname(&matches), Some(host) => { if let Err(err) = hostname::set(host) { - show_error!("{}", err); - 1 + return Err(USimpleError::new(1, format!("{}", err))); } else { - 0 + Ok(()) } } } @@ -97,7 +97,7 @@ pub fn uu_app() -> App<'static, 'static> { .arg(Arg::with_name(OPT_HOST)) } -fn display_hostname(matches: &ArgMatches) -> i32 { +fn display_hostname(matches: &ArgMatches) -> UResult<()> { let hostname = hostname::get().unwrap().into_string().unwrap(); if matches.is_present(OPT_IP_ADDRESS) { @@ -127,12 +127,10 @@ fn display_hostname(matches: &ArgMatches) -> i32 { println!("{}", &output[0..len - 1]); } - 0 + Ok(()) } Err(f) => { - show_error!("{}", f); - - 1 + return Err(USimpleError::new(1, format!("{}", f))); } } } else { @@ -144,12 +142,12 @@ fn display_hostname(matches: &ArgMatches) -> i32 { } else { println!("{}", &hostname[ci.0 + 1..]); } - return 0; + return Ok(()); } } println!("{}", hostname); - 0 + Ok(()) } } From f66f10c4ede2a65a33ab7c4513377adb1ef69299 Mon Sep 17 00:00:00 2001 From: 353fc443 <353fc443@pm.me> Date: Fri, 2 Jul 2021 12:03:14 +0000 Subject: [PATCH 2/5] Added UResult for hostid --- src/uu/hostid/src/hostid.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/uu/hostid/src/hostid.rs b/src/uu/hostid/src/hostid.rs index e9fc08379..b0f68968d 100644 --- a/src/uu/hostid/src/hostid.rs +++ b/src/uu/hostid/src/hostid.rs @@ -12,6 +12,7 @@ extern crate uucore; use clap::{crate_version, App}; use libc::c_long; +use uucore::error::UResult; static SYNTAX: &str = "[options]"; @@ -20,10 +21,11 @@ extern "C" { pub fn gethostid() -> c_long; } -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); hostid(); - 0 + Ok(()) } pub fn uu_app() -> App<'static, 'static> { From 23f5f55560a493b2de6287451e07ede8ba5aa367 Mon Sep 17 00:00:00 2001 From: 353fc443 <353fc443@pm.me> Date: Fri, 2 Jul 2021 12:14:24 +0000 Subject: [PATCH 3/5] Added UResult for dirname --- src/uu/dirname/src/dirname.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/uu/dirname/src/dirname.rs b/src/uu/dirname/src/dirname.rs index 356f2e6b1..797e51808 100644 --- a/src/uu/dirname/src/dirname.rs +++ b/src/uu/dirname/src/dirname.rs @@ -10,6 +10,7 @@ extern crate uucore; use clap::{crate_version, App, Arg}; use std::path::Path; +use uucore::error::{UResult, USimpleError}; use uucore::InvalidEncodingHandling; static ABOUT: &str = "strip last component from file name"; @@ -30,7 +31,8 @@ fn get_long_usage() -> String { ) } -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(); @@ -77,11 +79,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { print!("{}", separator); } } else { - show_usage_error!("missing operand"); - return 1; + return Err(USimpleError::new(1, format!("missing operand"))); } - 0 + Ok(()) } pub fn uu_app() -> App<'static, 'static> { From 38c08809891f6d7bea2e0df3e9e08073825275f3 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 4 Jul 2021 12:03:36 +0200 Subject: [PATCH 4/5] fix the clippy warning --- src/uu/dirname/src/dirname.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/dirname/src/dirname.rs b/src/uu/dirname/src/dirname.rs index 797e51808..d84bd4b80 100644 --- a/src/uu/dirname/src/dirname.rs +++ b/src/uu/dirname/src/dirname.rs @@ -79,7 +79,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { print!("{}", separator); } } else { - return Err(USimpleError::new(1, format!("missing operand"))); + return Err(UUsageError::new(1, "missing operand".to_string())); } Ok(()) From d0805605e0110211f7a849101fd0669950c5c356 Mon Sep 17 00:00:00 2001 From: 353fc443 <85840256+353fc443@users.noreply.github.com> Date: Sun, 4 Jul 2021 16:09:55 +0530 Subject: [PATCH 5/5] imported UUsageError --- src/uu/dirname/src/dirname.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/dirname/src/dirname.rs b/src/uu/dirname/src/dirname.rs index d84bd4b80..63ee57272 100644 --- a/src/uu/dirname/src/dirname.rs +++ b/src/uu/dirname/src/dirname.rs @@ -10,7 +10,7 @@ extern crate uucore; use clap::{crate_version, App, Arg}; use std::path::Path; -use uucore::error::{UResult, USimpleError}; +use uucore::error::{UResult, UUsageError}; use uucore::InvalidEncodingHandling; static ABOUT: &str = "strip last component from file name";