1use clap::Parser;
2use rustutils_runnable::Runnable;
3use std::error::Error;
4use std::io::Write;
5use std::os::unix::ffi::OsStrExt;
6
7#[derive(Parser, Clone, Debug)]
9#[clap(author, version, about, long_about = None)]
10pub struct Uname {
11 #[clap(short, long)]
13 all: bool,
14 #[clap(short = 's', long)]
16 kernel_name: bool,
17 #[clap(short, long, alias = "nodename")]
19 node_name: bool,
20 #[clap(short = 'r', long)]
22 kernel_release: bool,
23 #[clap(short = 'v', long)]
25 kernel_version: bool,
26 #[clap(short, long)]
28 machine: bool,
29 #[clap(short, long)]
31 processor: bool,
32 #[clap(short = 'i', long)]
34 hardware_platform: bool,
35 #[clap(short, long)]
37 operating_system: bool,
38 #[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}