From d4697d98835834e2ea76bd2443c2bbcb5c56e734 Mon Sep 17 00:00:00 2001 From: 353fc443 <85840256+353fc443@users.noreply.github.com> Date: Wed, 25 Aug 2021 22:02:35 +0530 Subject: [PATCH 1/2] env: added UResult --- src/uu/env/src/env.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 9fcdf84ea..423fc676b 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -12,6 +12,9 @@ #[macro_use] extern crate clap; +#[macro_use] +extern crate uucore; + use clap::{App, AppSettings, Arg}; use ini::Ini; use std::borrow::Cow; @@ -19,6 +22,7 @@ use std::env; use std::io::{self, Write}; use std::iter::Iterator; use std::process::Command; +use uucore::error::{UResult, USimpleError}; const USAGE: &str = "env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]"; const AFTER_HELP: &str = "\ @@ -70,7 +74,7 @@ fn parse_program_opt<'a>(opts: &mut Options<'a>, opt: &'a str) -> Result<(), i32 } } -fn load_config_file(opts: &mut Options) -> Result<(), i32> { +fn load_config_file(opts: &mut Options) -> UResult<()> { // NOTE: config files are parsed using an INI parser b/c it's available and compatible with ".env"-style files // ... * but support for actual INI files, although working, is not intended, nor claimed for &file in &opts.files { @@ -157,7 +161,7 @@ pub fn uu_app() -> App<'static, 'static> { .help("remove variable from the environment")) } -fn run_env(args: impl uucore::Args) -> Result<(), i32> { +fn run_env(args: impl uucore::Args) -> UResult<()> { let app = uu_app(); let matches = app.get_matches_from(args); @@ -188,8 +192,10 @@ fn run_env(args: impl uucore::Args) -> Result<(), i32> { match env::set_current_dir(d) { Ok(()) => d, Err(error) => { - eprintln!("env: cannot change directory to \"{}\": {}", d, error); - return Err(125); + return Err(USimpleError::new( + 125, + format!("env: cannot change directory to \"{}\": {}", d, error), + )); } }; } @@ -253,9 +259,9 @@ fn run_env(args: impl uucore::Args) -> Result<(), i32> { // FIXME: this should just use execvp() (no fork()) on Unix-like systems match Command::new(&*prog).args(args).status() { - Ok(exit) if !exit.success() => return Err(exit.code().unwrap()), - Err(ref err) if err.kind() == io::ErrorKind::NotFound => return Err(127), - Err(_) => return Err(126), + Ok(exit) if !exit.success() => return Err(exit.code().unwrap().into()), + Err(ref err) if err.kind() == io::ErrorKind::NotFound => return Err(127.into()), + Err(_) => return Err(126.into()), Ok(_) => (), } } else { @@ -266,9 +272,7 @@ fn run_env(args: impl uucore::Args) -> Result<(), i32> { Ok(()) } -pub fn uumain(args: impl uucore::Args) -> i32 { - match run_env(args) { - Ok(()) => 0, - Err(code) => code, - } +#[uucore_procs::gen_uumain] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { + run_env(args) } From 1493027f27b1a89d769ac1eaccee208beced9d10 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 28 Aug 2021 09:55:15 +0200 Subject: [PATCH 2/2] remove the env: --- src/uu/env/src/env.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 423fc676b..baa3ca86c 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -194,7 +194,7 @@ fn run_env(args: impl uucore::Args) -> UResult<()> { Err(error) => { return Err(USimpleError::new( 125, - format!("env: cannot change directory to \"{}\": {}", d, error), + format!("cannot change directory to \"{}\": {}", d, error), )); } };