rustutils_uname/
lib.rs

1use clap::Parser;
2use rustutils_runnable::Runnable;
3use std::error::Error;
4use std::io::Write;
5use std::os::unix::ffi::OsStrExt;
6
7/// Print system information.
8#[derive(Parser, Clone, Debug)]
9#[clap(author, version, about, long_about = None)]
10pub struct Uname {
11    /// Print all information
12    #[clap(short, long)]
13    all: bool,
14    /// Print the kernel name
15    #[clap(short = 's', long)]
16    kernel_name: bool,
17    /// Print the network node hostname
18    #[clap(short, long, alias = "nodename")]
19    node_name: bool,
20    /// Print the kernel release
21    #[clap(short = 'r', long)]
22    kernel_release: bool,
23    /// Print the kernel version
24    #[clap(short = 'v', long)]
25    kernel_version: bool,
26    /// Print the machine hardware name
27    #[clap(short, long)]
28    machine: bool,
29    /// Print the processor type
30    #[clap(short, long)]
31    processor: bool,
32    /// Print the hardware platform
33    #[clap(short = 'i', long)]
34    hardware_platform: bool,
35    /// Print the operating system
36    #[clap(short, long)]
37    operating_system: bool,
38    /// Format the output as JSON
39    #[clap(short, long)]
40    json: bool,
41}
42
43impl Runnable for Uname {
44    fn run(&self) -> Result<(), Box<dyn Error>> {
45        let uname = nix::sys::utsname::uname()?;
46        let mut output = vec![];
47        if self.kernel_name || self.all {
48            output.push(uname.sysname());
49        }
50
51        if self.node_name || self.all {
52            output.push(uname.nodename());
53        }
54
55        if self.kernel_release || self.all {
56            output.push(uname.release());
57        }
58
59        if self.kernel_version || self.all {
60            output.push(uname.version());
61        }
62
63        if self.machine || self.all {
64            output.push(uname.machine());
65        }
66
67        let mut stdout = std::io::stdout();
68        for string in &output {
69            stdout.write_all(b" ")?;
70            stdout.write_all(string.as_bytes())?;
71        }
72        stdout.write_all(b"\n")?;
73
74        Ok(())
75    }
76}