From 038a549089e9c8a9c12e80d76269f137f01caf05 Mon Sep 17 00:00:00 2001 From: David Matos Date: Mon, 29 Jan 2024 20:34:07 +0100 Subject: [PATCH] Refactor to use options struct and make it public for Nushell --- src/uu/uname/src/uname.rs | 142 +++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 65 deletions(-) diff --git a/src/uu/uname/src/uname.rs b/src/uu/uname/src/uname.rs index e6d5c3a0a32..e10a8fc2393 100644 --- a/src/uu/uname/src/uname.rs +++ b/src/uu/uname/src/uname.rs @@ -15,95 +15,107 @@ use uucore::{ const ABOUT: &str = help_about!("uname.md"); const USAGE: &str = help_usage!("uname.md"); -pub mod options { - pub static ALL: &str = "all"; - pub static KERNEL_NAME: &str = "kernel-name"; - pub static NODENAME: &str = "nodename"; - pub static KERNEL_VERSION: &str = "kernel-version"; - pub static KERNEL_RELEASE: &str = "kernel-release"; - pub static MACHINE: &str = "machine"; - pub static PROCESSOR: &str = "processor"; - pub static HARDWARE_PLATFORM: &str = "hardware-platform"; - pub static OS: &str = "operating-system"; +static ALL: &str = "all"; +static KERNEL_NAME: &str = "kernel-name"; +static NODENAME: &str = "nodename"; +static KERNEL_VERSION: &str = "kernel-version"; +static KERNEL_RELEASE: &str = "kernel-release"; +static MACHINE: &str = "machine"; +static PROCESSOR: &str = "processor"; +static HARDWARE_PLATFORM: &str = "hardware-platform"; +static OS: &str = "operating-system"; + +pub struct Options { + pub all: bool, + pub kernel_name: bool, + pub nodename: bool, + pub kernel_version: bool, + pub kernel_release: bool, + pub machine: bool, + pub processor: bool, + pub hardware_platform: bool, + pub os: bool, } -#[uucore::main] -pub fn uumain(args: impl uucore::Args) -> UResult<()> { - let matches = uu_app().try_get_matches_from(args)?; - - let uname = PlatformInfo::new().map_err(|_e| USimpleError::new(1, "cannot get system name"))?; - +pub fn uu_uname(opts: &Options) -> UResult<()> { let mut output = String::new(); - - let all = matches.get_flag(options::ALL); - let kernel_name = matches.get_flag(options::KERNEL_NAME); - let nodename = matches.get_flag(options::NODENAME); - let kernel_release = matches.get_flag(options::KERNEL_RELEASE); - let kernel_version = matches.get_flag(options::KERNEL_VERSION); - let machine = matches.get_flag(options::MACHINE); - let processor = matches.get_flag(options::PROCESSOR); - let hardware_platform = matches.get_flag(options::HARDWARE_PLATFORM); - let os = matches.get_flag(options::OS); - - let none = !(all - || kernel_name - || nodename - || kernel_release - || kernel_version - || machine - || os - || processor - || hardware_platform); - - if kernel_name || all || none { + let uname = PlatformInfo::new().map_err(|_e| USimpleError::new(1, "cannot get system name"))?; + let none = !(opts.all + || opts.kernel_name + || opts.nodename + || opts.kernel_release + || opts.kernel_version + || opts.machine + || opts.os + || opts.processor + || opts.hardware_platform); + + if opts.kernel_name || opts.all || none { output.push_str(&uname.sysname().to_string_lossy()); output.push(' '); } - if nodename || all { + if opts.nodename || opts.all { output.push_str(&uname.nodename().to_string_lossy()); output.push(' '); } - if kernel_release || all { + if opts.kernel_release || opts.all { output.push_str(&uname.release().to_string_lossy()); output.push(' '); } - if kernel_version || all { + if opts.kernel_version || opts.all { output.push_str(&uname.version().to_string_lossy()); output.push(' '); } - if machine || all { + if opts.machine || opts.all { output.push_str(&uname.machine().to_string_lossy()); output.push(' '); } - if os || all { + if opts.os || opts.all { output.push_str(&uname.osname().to_string_lossy()); output.push(' '); } // This option is unsupported on modern Linux systems // See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html - if processor { + if opts.processor { output.push_str("unknown"); output.push(' '); } // This option is unsupported on modern Linux systems // See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html - if hardware_platform { + if opts.hardware_platform { output.push_str("unknown"); output.push(' '); } println!("{}", output.trim_end()); - Ok(()) } +#[uucore::main] +pub fn uumain(args: impl uucore::Args) -> UResult<()> { + let matches = uu_app().try_get_matches_from(args)?; + + let options = Options { + all: matches.get_flag(ALL), + kernel_name: matches.get_flag(KERNEL_NAME), + nodename: matches.get_flag(NODENAME), + kernel_release: matches.get_flag(KERNEL_RELEASE), + kernel_version: matches.get_flag(KERNEL_VERSION), + machine: matches.get_flag(MACHINE), + processor: matches.get_flag(PROCESSOR), + hardware_platform: matches.get_flag(HARDWARE_PLATFORM), + os: matches.get_flag(OS), + }; + uu_uname(&options) +} + pub fn uu_app() -> Command { Command::new(uucore::util_name()) .version(crate_version!()) @@ -111,24 +123,24 @@ pub fn uu_app() -> Command { .override_usage(format_usage(USAGE)) .infer_long_args(true) .arg( - Arg::new(options::ALL) + Arg::new(ALL) .short('a') - .long(options::ALL) + .long(ALL) .help("Behave as though all of the options -mnrsvo were specified.") .action(ArgAction::SetTrue), ) .arg( - Arg::new(options::KERNEL_NAME) + Arg::new(KERNEL_NAME) .short('s') - .long(options::KERNEL_NAME) + .long(KERNEL_NAME) .alias("sysname") // Obsolescent option in GNU uname .help("print the kernel name.") .action(ArgAction::SetTrue), ) .arg( - Arg::new(options::NODENAME) + Arg::new(NODENAME) .short('n') - .long(options::NODENAME) + .long(NODENAME) .help( "print the nodename (the nodename may be a name that the system \ is known by to a communications network).", @@ -136,46 +148,46 @@ pub fn uu_app() -> Command { .action(ArgAction::SetTrue), ) .arg( - Arg::new(options::KERNEL_RELEASE) + Arg::new(KERNEL_RELEASE) .short('r') - .long(options::KERNEL_RELEASE) + .long(KERNEL_RELEASE) .alias("release") // Obsolescent option in GNU uname .help("print the operating system release.") .action(ArgAction::SetTrue), ) .arg( - Arg::new(options::KERNEL_VERSION) + Arg::new(KERNEL_VERSION) .short('v') - .long(options::KERNEL_VERSION) + .long(KERNEL_VERSION) .help("print the operating system version.") .action(ArgAction::SetTrue), ) .arg( - Arg::new(options::MACHINE) + Arg::new(MACHINE) .short('m') - .long(options::MACHINE) - .help("print the machine hardware name.") + .long(MACHINE) + .help("print thec machine hardware name.") .action(ArgAction::SetTrue), ) .arg( - Arg::new(options::OS) + Arg::new(OS) .short('o') - .long(options::OS) + .long(OS) .help("print the operating system name.") .action(ArgAction::SetTrue), ) .arg( - Arg::new(options::PROCESSOR) + Arg::new(PROCESSOR) .short('p') - .long(options::PROCESSOR) + .long(PROCESSOR) .help("print the processor type (non-portable)") .action(ArgAction::SetTrue) .hide(true), ) .arg( - Arg::new(options::HARDWARE_PLATFORM) + Arg::new(HARDWARE_PLATFORM) .short('i') - .long(options::HARDWARE_PLATFORM) + .long(HARDWARE_PLATFORM) .help("print the hardware platform (non-portable)") .action(ArgAction::SetTrue) .hide(true),