use std::error::Error;

use argh::FromArgs;

use crate::parsing::HostsFile;

/// Block access to a host
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "block")]
pub struct Args {
    /// hostname to block
    #[argh(positional)]
    hostname: String,
}

pub fn main(args: &Args) -> Result<(), Box<dyn Error>> {
    let mut hosts_file = HostsFile::load();

    let result = hosts_file.add_field("127.0.0.1", &args.hostname);
    if result.is_err() {
        return Err(From::from(result.err().unwrap()));
    }

    Ok(())
}
