use crate::Path; use clap::{Arg, ArgAction, ArgMatches, Command}; use std::process; pub const PATH: Path = Path::Bin; #[must_use] pub fn cli() -> Command { Command::new("hostname") .version(env!("CARGO_PKG_VERSION")) .author("The JeanG3nie ") .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::("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!( "{}", if let Some(s) = hostname.to_string_lossy().split('.').next() { s } else { eprintln!("hostname: missing operand"); process::exit(1); } ); } else { println!("{}", hostname.to_string_lossy()); } } Err(e) => { eprintln!("{e}"); process::exit(1); } } } }