shitbox/corebox/commands/hostname/mod.rs
Nathan Fisher 06b88d2c24 Refactoring:
- Split into workspace crates
- Split into two binaries `corebox` and `hashbox`
- Add `mount` crate to workspace to prepare for third binary `utilbox`
2023-02-05 23:50:59 -05:00

54 lines
1.6 KiB
Rust

use super::Cmd;
use clap::{Arg, ArgAction, ArgMatches, Command};
use std::{error::Error, io};
#[derive(Debug, Default)]
pub struct Hostname;
impl Cmd for Hostname {
fn cli(&self) -> clap::Command {
Command::new("hostname")
.author("Nathan Fisher")
.about("Prints the name of the current host. The super-user can set the host name by supplying an argument.")
.args([
Arg::new("NAME")
.help("name to set"),
Arg::new("STRIP")
.help("Removes any domain information from the printed name.")
.short('s')
.long("strip")
.action(ArgAction::SetTrue)
])
}
fn run(&self, matches: &ArgMatches) -> Result<(), Box<dyn Error>> {
if let Some(name) = matches.get_one::<String>("NAME") {
unistd::sethostname(name)?;
Ok(())
} else {
let hostname = unistd::gethostname()?;
if matches.get_flag("STRIP") {
println!(
"{}",
if let Some(s) = hostname.split('.').next() {
s
} else {
return Err(io::Error::new(
io::ErrorKind::Other,
"hostname: missing operand",
)
.into());
}
);
} else {
println!("{hostname}");
}
Ok(())
}
}
fn path(&self) -> Option<shitbox::Path> {
Some(shitbox::Path::Bin)
}
}