Add `hostid` applet

This commit is contained in:
Nathan Fisher 2023-01-23 10:17:17 -05:00
parent 3ef3a8b3aa
commit 5076dcc350
3 changed files with 33 additions and 1 deletions

View File

@ -34,6 +34,7 @@ code between applets, making for an overall smaller binary.
- fold
- groups
- head
- hostid
- hostname
- link
- logname

27
src/cmd/hostid/mod.rs Normal file
View File

@ -0,0 +1,27 @@
use super::Cmd;
use clap::Command;
#[derive(Debug, Default)]
pub struct Hostid;
impl Cmd for Hostid {
fn cli(&self) -> clap::Command {
Command::new("hostid")
.about("print the numeric identifier for the current host")
.author("Nathan Fisher")
.version(env!("CARGO_PKG_VERSION"))
}
fn run(&self, _matches: Option<&clap::ArgMatches>) -> Result<(), Box<dyn std::error::Error>> {
let hostid = unsafe {
libc::gethostid()
};
let hostid: String = format!("{hostid:x}").chars().skip(8).collect();
println!("{}", hostid);
Ok(())
}
fn path(&self) -> Option<crate::Path> {
Some(crate::Path::UsrBin)
}
}

View File

@ -22,6 +22,7 @@ mod fold;
mod getty;
mod groups;
mod head;
mod hostid;
mod hostname;
mod link;
mod ln;
@ -86,6 +87,8 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
"fold" => Some(Box::new(fold::Fold::default())),
"groups" => Some(Box::new(groups::Groups::default())),
"head" => Some(Box::new(head::Head::default())),
"hostid" => Some(Box::new(hostid::Hostid::default())),
"hostname" => Some(Box::new(hostname::Hostname::default())),
"link" => Some(Box::new(link::Link::default())),
"logname" => Some(Box::new(logname::Logname::default())),
"mkfifo" => Some(Box::new(mkfifo::MkFifo::default())),
@ -113,7 +116,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
}
}
pub static COMMANDS: [&str; 41] = [
pub static COMMANDS: [&str; 42] = [
"base32",
"base64",
"basename",
@ -131,6 +134,7 @@ pub static COMMANDS: [&str; 41] = [
"fold",
"groups",
"head",
"hostid",
"hostname",
"link",
"logname",