use super::Cmd; use crate::Path; use clap::{Arg, ArgAction, ArgMatches, Command}; use std::{error::Error, io}; pub struct Hostname { name: &'static str, path: Option, } pub const HOSTNAME: Hostname = Hostname { name: "hostname", path: Some(Path::Bin), }; impl Cmd for Hostname { fn name(&self) -> &str { self.name } fn cli(&self) -> clap::Command { Command::new(self.name) .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.") .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: Option<&ArgMatches>) -> Result<(), Box> { let matches = matches.unwrap(); if let Some(name) = matches.get_one::("NAME") { hostname::set(name)?; Ok(()) } else { let hostname = hostname::get()?; if matches.get_flag("STRIP") { println!( "{}", if let Some(s) = hostname.to_string_lossy().split('.').next() { s } else { return Err(io::Error::new( io::ErrorKind::Other, "hostname: missing operand", ) .into()); } ); } else { println!("{}", hostname.to_string_lossy()); } Ok(()) } } fn path(&self) -> Option { self.path } }