use std::{error::Error, fmt::Debug};

use argh::FromArgs;

use crate::parsing::HostsFile;

/// Check if a hostname exists
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "check")]
pub struct Args {
    /// hostname to check
    #[argh(positional)]
    field: String,
}

pub fn main(args: &Args) -> Result<(), Box<dyn Error>> {
    let hosts_file = HostsFile::load();
    if hosts_file.field_exists(&args.field) {
        return Ok(());
    }

    Err(From::from(format!("Failed to find field {} in /etc/hosts", args.field)))
}
