2022-12-20 12:05:21 -05:00
|
|
|
use crate::Path;
|
2022-12-20 18:35:45 -05:00
|
|
|
use clap::{Arg, ArgAction, ArgMatches, Command};
|
|
|
|
use std::process;
|
2022-12-20 12:05:21 -05:00
|
|
|
|
|
|
|
pub const PATH: Path = Path::Bin;
|
|
|
|
|
2022-12-20 18:35:45 -05:00
|
|
|
#[must_use]
|
2022-12-20 12:05:21 -05:00
|
|
|
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!(
|
|
|
|
"{}",
|
2022-12-20 18:35:45 -05:00
|
|
|
if let Some(s) = hostname.to_string_lossy().split('.').next() {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
eprintln!("hostname: missing operand");
|
|
|
|
process::exit(1);
|
2022-12-20 12:05:21 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
println!("{}", hostname.to_string_lossy());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("{e}");
|
|
|
|
process::exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|