Skip to content

Commit

Permalink
refactor: move domain validation to cli module
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Jan 4, 2023
1 parent bb4a00d commit 669ea34
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use clap::{Parser, ValueEnum};
)]
pub struct Opts {
/// Domain name to query
#[arg(value_parser = is_domain)]
pub domain: String,

/// Record Type
Expand All @@ -33,3 +34,11 @@ pub enum RecordType {
SOA,
TXT,
}

fn is_domain(domain: &str) -> Result<String, String> {
if domain.contains('.') {
Ok(domain.to_string())
} else {
Err(format!("{} isn't a valid domain", domain))
}
}
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub enum Error {
#[error("{0}")]
InvalidArgument(String),

#[error("Invalid domain {0:?}")]
InvalidDomain(String),

#[error("Configuration file is not found `{path}`")]
#[diagnostic(
code(digs::no_config),
Expand Down
13 changes: 2 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use trust_dns_client::rr::RecordType;
use clap::Parser;
use miette::Result;

use digs::{cli, cli::Opts, error::Error, output::Printer};
use digs::{cli, cli::Opts, output::Printer};

fn run() -> Result<()> {
let opts = Opts::parse();

let domain = is_domain(&opts.domain)?;
let record_type = match opts.rtype {
cli::RecordType::A => RecordType::A,
cli::RecordType::AAAA => RecordType::AAAA,
Expand All @@ -21,19 +20,11 @@ fn run() -> Result<()> {
};

let config_file = opts.config;
let printer = Printer::new(domain, record_type, config_file);
let printer = Printer::new(opts.domain, record_type, config_file);
printer.print()?;
Ok(())
}

fn is_domain(domain: &str) -> Result<String, Error> {
if domain.contains('.') {
Ok(domain.to_string())
} else {
Err(Error::InvalidDomain(domain.to_string()))
}
}

fn main() {
if let Err(err) = run() {
eprintln!("Error: {:?}", err);
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn domain_invalid() -> Result<(), Box<dyn Error>> {
cmd.arg("example");
cmd.assert()
.failure()
.stderr(predicate::str::contains(r#"Invalid domain "example""#));
.stderr(predicate::str::contains(r#"example isn't a valid domain"#));
Ok(())
}

Expand Down

0 comments on commit 669ea34

Please sign in to comment.