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