mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
uname: Refactor into public fns for Nushell (#5921)
* Refactor to use options struct and make it public for Nushell * Return the output for use in nushell * wip:opt1 * Add UNameOutput struct instead * Apply req changes * change back to mod options * uname: add empty line & fix position of comment --------- Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
This commit is contained in:
parent
c9677abeed
commit
de74f707e9
1 changed files with 109 additions and 70 deletions
|
@ -27,80 +27,119 @@ pub mod options {
|
||||||
pub static OS: &str = "operating-system";
|
pub static OS: &str = "operating-system";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct UNameOutput {
|
||||||
|
pub kernel_name: Option<String>,
|
||||||
|
pub nodename: Option<String>,
|
||||||
|
pub kernel_release: Option<String>,
|
||||||
|
pub kernel_version: Option<String>,
|
||||||
|
pub machine: Option<String>,
|
||||||
|
pub os: Option<String>,
|
||||||
|
pub processor: Option<String>,
|
||||||
|
pub hardware_platform: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UNameOutput {
|
||||||
|
fn display(&self) -> String {
|
||||||
|
let mut output = String::new();
|
||||||
|
for name in [
|
||||||
|
self.kernel_name.as_ref(),
|
||||||
|
self.nodename.as_ref(),
|
||||||
|
self.kernel_release.as_ref(),
|
||||||
|
self.kernel_version.as_ref(),
|
||||||
|
self.machine.as_ref(),
|
||||||
|
self.os.as_ref(),
|
||||||
|
self.processor.as_ref(),
|
||||||
|
self.hardware_platform.as_ref(),
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
{
|
||||||
|
output.push_str(name);
|
||||||
|
output.push(' ');
|
||||||
|
}
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(opts: &Options) -> UResult<Self> {
|
||||||
|
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);
|
||||||
|
|
||||||
|
let kernel_name = (opts.kernel_name || opts.all || none)
|
||||||
|
.then(|| uname.sysname().to_string_lossy().to_string());
|
||||||
|
|
||||||
|
let nodename =
|
||||||
|
(opts.nodename || opts.all).then(|| uname.nodename().to_string_lossy().to_string());
|
||||||
|
|
||||||
|
let kernel_release = (opts.kernel_release || opts.all)
|
||||||
|
.then(|| uname.release().to_string_lossy().to_string());
|
||||||
|
|
||||||
|
let kernel_version = (opts.kernel_version || opts.all)
|
||||||
|
.then(|| uname.version().to_string_lossy().to_string());
|
||||||
|
|
||||||
|
let machine =
|
||||||
|
(opts.machine || opts.all).then(|| uname.machine().to_string_lossy().to_string());
|
||||||
|
|
||||||
|
let os = (opts.os || opts.all).then(|| uname.osname().to_string_lossy().to_string());
|
||||||
|
|
||||||
|
// This option is unsupported on modern Linux systems
|
||||||
|
// See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
|
||||||
|
let processor = opts.processor.then(|| "unknown".to_string());
|
||||||
|
|
||||||
|
// This option is unsupported on modern Linux systems
|
||||||
|
// See: https://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html
|
||||||
|
let hardware_platform = opts.hardware_platform.then(|| "unknown".to_string());
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
kernel_name,
|
||||||
|
nodename,
|
||||||
|
kernel_release,
|
||||||
|
kernel_version,
|
||||||
|
machine,
|
||||||
|
os,
|
||||||
|
processor,
|
||||||
|
hardware_platform,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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]
|
#[uucore::main]
|
||||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
let matches = uu_app().try_get_matches_from(args)?;
|
let matches = uu_app().try_get_matches_from(args)?;
|
||||||
|
|
||||||
let uname = PlatformInfo::new().map_err(|_e| USimpleError::new(1, "cannot get system name"))?;
|
let options = Options {
|
||||||
|
all: matches.get_flag(options::ALL),
|
||||||
let mut output = String::new();
|
kernel_name: matches.get_flag(options::KERNEL_NAME),
|
||||||
|
nodename: matches.get_flag(options::NODENAME),
|
||||||
let all = matches.get_flag(options::ALL);
|
kernel_release: matches.get_flag(options::KERNEL_RELEASE),
|
||||||
let kernel_name = matches.get_flag(options::KERNEL_NAME);
|
kernel_version: matches.get_flag(options::KERNEL_VERSION),
|
||||||
let nodename = matches.get_flag(options::NODENAME);
|
machine: matches.get_flag(options::MACHINE),
|
||||||
let kernel_release = matches.get_flag(options::KERNEL_RELEASE);
|
processor: matches.get_flag(options::PROCESSOR),
|
||||||
let kernel_version = matches.get_flag(options::KERNEL_VERSION);
|
hardware_platform: matches.get_flag(options::HARDWARE_PLATFORM),
|
||||||
let machine = matches.get_flag(options::MACHINE);
|
os: matches.get_flag(options::OS),
|
||||||
let processor = matches.get_flag(options::PROCESSOR);
|
};
|
||||||
let hardware_platform = matches.get_flag(options::HARDWARE_PLATFORM);
|
let output = UNameOutput::new(&options)?;
|
||||||
let os = matches.get_flag(options::OS);
|
println!("{}", output.display().trim_end());
|
||||||
|
|
||||||
let none = !(all
|
|
||||||
|| kernel_name
|
|
||||||
|| nodename
|
|
||||||
|| kernel_release
|
|
||||||
|| kernel_version
|
|
||||||
|| machine
|
|
||||||
|| os
|
|
||||||
|| processor
|
|
||||||
|| hardware_platform);
|
|
||||||
|
|
||||||
if kernel_name || all || none {
|
|
||||||
output.push_str(&uname.sysname().to_string_lossy());
|
|
||||||
output.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if nodename || all {
|
|
||||||
output.push_str(&uname.nodename().to_string_lossy());
|
|
||||||
output.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if kernel_release || all {
|
|
||||||
output.push_str(&uname.release().to_string_lossy());
|
|
||||||
output.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if kernel_version || all {
|
|
||||||
output.push_str(&uname.version().to_string_lossy());
|
|
||||||
output.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if machine || all {
|
|
||||||
output.push_str(&uname.machine().to_string_lossy());
|
|
||||||
output.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if os || 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 {
|
|
||||||
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 {
|
|
||||||
output.push_str("unknown");
|
|
||||||
output.push(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("{}", output.trim_end());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue