use clap::{crate_version, Parser};
use std::env;
use std::io::{self, Write};
use trust_dns_client::op::{Message, Query};
use trust_dns_client::rr::{Name, RecordType};
use trust_dns_client::serialize::binary::BinEncodable;
#[derive(Debug, Parser)]
#[clap(name = "donut", version = crate_version!())]
struct Dns2BinApplication {
#[clap(long = "raw", short = 'r')]
raw: bool,
#[clap(long = "type", short = 't', default_value_t = RecordType::A)]
type_: RecordType,
name: Name,
}
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let opts = Dns2BinApplication::parse();
let bytes = Message::new()
.add_query(Query::query(opts.name.clone(), opts.type_))
.to_bytes()
.map(|b| {
if !opts.raw {
base64::encode_config(&b, base64::URL_SAFE_NO_PAD).into_bytes()
} else {
b
}
})?;
let mut stdout = io::stdout();
stdout.write_all(&bytes)?;
Ok(())
}