Skip to content

Commit

Permalink
Refactor to use options struct and make it public for Nushell
Browse files Browse the repository at this point in the history
  • Loading branch information
dmatos2012 committed Jan 30, 2024
1 parent c439f81 commit 038a549
Showing 1 changed file with 77 additions and 65 deletions.
142 changes: 77 additions & 65 deletions src/uu/uname/src/uname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,167 +15,179 @@ 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!())
.about(ABOUT)
.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).",
)
.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.")

Check failure on line 169 in src/uu/uname/src/uname.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: Unknown word (thec) (file:'src/uu/uname/src/uname.rs', line:169)
.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),
Expand Down

0 comments on commit 038a549

Please sign in to comment.