Skip to content

Commit 24d02be

Browse files
committed
refactor: user printer module
1 parent 4969414 commit 24d02be

File tree

3 files changed

+70
-51
lines changed

3 files changed

+70
-51
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ pub mod cli;
22
pub mod config;
33
pub mod dns;
44
pub mod error;
5+
pub mod output;
56
pub mod utils;

src/main.rs

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use std::process;
22
use trust_dns_client::rr::RecordType;
33

44
use clap::Parser;
5-
use colored::*;
6-
use log::trace;
75
use miette::Result;
86

9-
use digs::{cli, cli::Opts, config, dns, utils};
7+
use digs::{cli, cli::Opts, output::Printer, utils};
108

119
fn run() -> Result<()> {
1210
let opts = Opts::parse();
@@ -21,55 +19,9 @@ fn run() -> Result<()> {
2119
cli::RecordType::SOA => RecordType::SOA,
2220
cli::RecordType::TXT => RecordType::TXT,
2321
};
24-
let config = config::read(&opts.config)?;
25-
26-
for server in config.servers {
27-
let response = dns::query(&domain, record_type, &server.ip);
28-
trace!("Response -> {:?}", response);
29-
30-
println!("{}", server.name);
31-
match response {
32-
Err(e) => {
33-
println!(" {}", e.to_string().red());
34-
}
35-
Ok(res) => {
36-
let print_output = |rr_type: String, name: String, rdata: String| {
37-
println!(
38-
" {} {} {}",
39-
rr_type.green().bold(),
40-
name.blue(),
41-
rdata.bold()
42-
);
43-
};
44-
45-
// FIXME
46-
if !res.answers().is_empty() {
47-
for res in res.answers() {
48-
let rr_type = res.rr_type().to_string();
49-
print_output(
50-
rr_type,
51-
res.name().to_string(),
52-
res.data().expect("no `rdata` found").to_string(),
53-
);
54-
}
55-
} else if res.answers().is_empty() && !res.name_servers().is_empty() {
56-
// if answers is empty, print default record (SOA)
57-
for res in res.name_servers() {
58-
let rr_type = res.rr_type().to_string();
59-
print_output(
60-
rr_type,
61-
res.name().to_string(),
62-
res.data().expect("no `rdata` found").to_string(),
63-
);
64-
}
65-
} else {
66-
// if default doesn't exist
67-
println!("{}", " No zone found".to_string().red());
68-
}
69-
}
70-
}
71-
}
7222

23+
let printer = Printer::new(domain, record_type, opts.config);
24+
printer.print()?;
7325
Ok(())
7426
}
7527

src/output.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::path::PathBuf;
2+
3+
use colored::Colorize;
4+
use log::trace;
5+
use trust_dns_client::rr::{Record, RecordType};
6+
7+
use crate::error::Error;
8+
use crate::{config, dns};
9+
10+
pub struct Printer {
11+
domain: String,
12+
record_type: RecordType,
13+
config: PathBuf,
14+
}
15+
16+
impl Printer {
17+
pub fn new(domain: String, record_type: RecordType, config: PathBuf) -> Self {
18+
Self {
19+
domain,
20+
record_type,
21+
config,
22+
}
23+
}
24+
pub fn print(&self) -> Result<(), Error> {
25+
let config = config::read(&self.config)?;
26+
27+
for server in config.servers {
28+
let response = dns::query(&self.domain, self.record_type, &server.ip);
29+
trace!("Response -> {:?}", response);
30+
31+
println!("{}", server.name);
32+
match response {
33+
Err(e) => {
34+
println!(" {}", e.to_string().red());
35+
}
36+
Ok(res) => {
37+
if !res.answers().is_empty() {
38+
for record in res.answers() {
39+
Self::print_record(record);
40+
}
41+
} else if res.answers().is_empty() && !res.name_servers().is_empty() {
42+
// if answers is empty, print default record (SOA)
43+
for record in res.name_servers() {
44+
Self::print_record(record);
45+
}
46+
} else {
47+
// if default doesn't exist
48+
println!("{}", " No zone found".to_string().red());
49+
}
50+
}
51+
}
52+
}
53+
Ok(())
54+
}
55+
fn print_record(record: &Record) {
56+
let record_type = record.record_type().to_string();
57+
let record_type = record_type.green().bold();
58+
let name = record.name().to_string().blue();
59+
let rdata = match record.data() {
60+
Some(rdata) => rdata.to_string(),
61+
None => "".to_string(),
62+
};
63+
let rdata = rdata.bold();
64+
println!(" {} {} {}", record_type, name, rdata);
65+
}
66+
}

0 commit comments

Comments
 (0)