shitbox/src/cmd/hostname/mod.rs

56 lines
1.7 KiB
Rust
Raw Normal View History

2022-12-20 12:05:21 -05:00
use clap::{Arg, Command, ArgMatches, ArgAction};
use crate::Path;
use std::{io, process};
pub const PATH: Path = Path::Bin;
pub fn cli() -> Command {
Command::new("hostname")
.version(env!("CARGO_PKG_VERSION"))
.author("The JeanG3nie <jeang3nie@hitchhiker-linux.org>")
.about("Prints the name of the current host. The super-user can set the host name by supplying an argument.")
.arg(
Arg::new("NAME")
.help("name to set")
)
.arg(
Arg::new("STRIP")
.help("Removes any domain information from the printed name.")
.short('s')
.long("strip")
.action(ArgAction::SetTrue)
)
}
pub fn run(matches: &ArgMatches) {
if let Some(name) = matches.get_one::<String>("NAME") {
if let Err(e) = hostname::set(name) {
eprintln!("{e}");
process::exit(1);
}
} else {
match hostname::get() {
Ok(hostname) => {
if matches.get_flag("STRIP") {
println!(
"{}",
match hostname.to_string_lossy().split('.').next() {
Some(c) => c,
None => {
eprintln!("hostname: missing operand");
process::exit(1);
}
}
);
} else {
println!("{}", hostname.to_string_lossy());
}
}
Err(e) => {
eprintln!("{e}");
process::exit(1);
}
}
}
}