1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #2600 from 353fc443/env-uresult

env: added UResult
This commit is contained in:
Sylvestre Ledru 2021-08-28 12:38:32 +02:00 committed by GitHub
commit 2b9196d6fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

28
src/uu/env/src/env.rs vendored
View file

@ -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!("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)
}