Skip to content

Commit

Permalink
make clippy::pedantic happy
Browse files Browse the repository at this point in the history
  • Loading branch information
davepmiller committed Dec 16, 2023
1 parent bf8dcb8 commit 5aadcb4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 67 deletions.
24 changes: 11 additions & 13 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ impl CloudflareClient {
.headers(headers)
.send()
.unwrap();
match path.contains("export") {
true => parse_text(response),
false => parse_response(response),
if path.contains("export") {
parse_text(response)
} else {
parse_response(response)
}
}

Expand Down Expand Up @@ -69,7 +70,7 @@ impl CloudflareClient {
parse_response(response)
}

pub(crate) fn delete(&self, path: String) -> Response {
pub(crate) fn delete(&self, path: &str) -> Response {
let headers = build_headers();
let client = reqwest::blocking::Client::new();
let response = client
Expand All @@ -89,16 +90,16 @@ fn parse_text(response: reqwest::blocking::Response) -> Response {
errors: vec![],
messages: vec![],
text: Some(response.text().unwrap_or_else(|e| {
println!("{:?}", e);
"".to_string()
println!("{e:?}");
String::new()
})),
}
}

fn build_headers() -> HeaderMap {
let mut headers = HeaderMap::new();
let cloudflare_token = get_env("CLOUDFLARE_TOKEN");
let auth = format!("Bearer {}", cloudflare_token);
let auth = format!("Bearer {cloudflare_token}");
headers.insert(
AUTHORIZATION,
HeaderValue::from_bytes(auth.as_bytes()).unwrap(),
Expand All @@ -108,22 +109,19 @@ fn build_headers() -> HeaderMap {
}

fn get_env(key: &str) -> String {
match env::var(key) {
Ok(t) => t,
Err(_) => panic!("{} is not set", key),
}
env::var(key).unwrap_or_else(|_| panic!("{key} should be set"))
}

fn parse_response(response: reqwest::blocking::Response) -> Response {
response.json::<Response>().unwrap_or_else(|e| {
println!("{:?}", e);
println!("{e:?}");
Response {
result: Value::String("{}".to_string()),
result_info: None,
success: false,
errors: vec![],
messages: vec![],
text: Some("".to_string()),
text: Some(String::new()),
}
})
}
Expand Down
59 changes: 25 additions & 34 deletions src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const PATH: &str = "zones";
pub(crate) struct Dns;

impl Dns {
pub(crate) fn export(id: String) {
pub(crate) fn export(id: &str) {
let path = format!(
"{}/{}/dns_records/export?per_page=100",
PATH,
id.as_str().replace('\"', "")
id.replace('\"', "")
);
let response = CloudflareClient {
endpoint: ENDPOINT.to_string(),
Expand All @@ -19,36 +19,31 @@ impl Dns {
}

pub(crate) fn get_id(zone_name: Option<String>, zone_id: Option<String>) -> String {
let matched_id = match zone_name.is_none() {
true => zone_id.unwrap(),
false => {
let name_arg = zone_name.unwrap().clone();
let response = CloudflareClient {
endpoint: ENDPOINT.to_string(),
}
.get(format!("{}/?per_page=100", PATH).as_str());
match response
.result
.as_array()
.unwrap()
.iter()
.find(|v| v["name"] == name_arg)
{
Some(v) => v["id"].to_string(),
_ => panic!("No zone matching name: {}", name_arg),
}
let matched_id = if zone_name.is_none() {
zone_id.unwrap()
} else {
let name_arg = zone_name.unwrap().clone();
let response = CloudflareClient {
endpoint: ENDPOINT.to_string(),
}
.get(format!("{PATH}/?per_page=100").as_str());
match response
.result
.as_array()
.unwrap()
.iter()
.find(|v| v["name"] == name_arg)
{
Some(v) => v["id"].to_string(),
_ => panic!("No zone matching name: {name_arg}"),
}
};

matched_id
}

pub(crate) fn import(id: String, file: String, proxy: bool) {
let path = format!(
"{}/{}/dns_records/import",
PATH,
id.as_str().replace('\"', "")
);
pub(crate) fn import(id: &str, file: String, proxy: bool) {
let path = format!("{}/{}/dns_records/import", PATH, id.replace('\"', ""));

// let file_contents = fs::read_to_string(file).unwrap();
let mut body = RequestBody::default();
Expand All @@ -58,19 +53,15 @@ impl Dns {
endpoint: ENDPOINT.to_string(),
}
.post_form(path.as_str(), body);
println!("{:?}", response);
println!("{response:?}");
}

pub(crate) fn list(id: String) {
let path = format!(
"{}/{}/dns_records?per_page=100",
PATH,
id.as_str().replace('\"', "")
);
pub(crate) fn list(id: &str) {
let path = format!("{}/{}/dns_records?per_page=100", PATH, id.replace('\"', ""));
let response = CloudflareClient {
endpoint: ENDPOINT.to_string(),
}
.get(path.as_str());
println!("{:?}", response);
println!("{response:?}");
}
}
23 changes: 12 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,34 @@ mod response;
mod token;
mod zone;

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
let args = Cli::parse();
match args.command {
Commands::Token(token) => match token.command {
TokenCommands::Verify => Token::verify(),
},
Commands::Zone(zone) => match zone.command.unwrap() {
ZoneCommands::List(args) => match args.domains {
true => Zone::list_domains(),
false => Zone::list(),
},
ZoneCommands::List(args) => {
if args.domains {
Zone::list_domains();
} else {
Zone::list();
}
}
ZoneCommands::Create { domain } => Zone::create(domain),
ZoneCommands::Delete { zone_id, zone_name } => {
let id = Dns::get_id(zone_name, zone_id);
Zone::delete(id);
Zone::delete(id.as_str());
}
},
Commands::Dns(dns) => match dns.command {
DnsCommands::List { zone_name, zone_id } => {
let id = Dns::get_id(zone_name, zone_id);
Dns::list(id)
Dns::list(id.as_str());
}
DnsCommands::Export { zone_name, zone_id } => {
let id = Dns::get_id(zone_name, zone_id);
Dns::export(id);
Dns::export(id.as_str());
}
DnsCommands::Import {
zone_name,
Expand All @@ -44,10 +47,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
proxy,
} => {
let id = Dns::get_id(zone_name, zone_id);
Dns::import(id, file, proxy);
Dns::import(id.as_str(), file, proxy);
}
},
}

Ok(())
}
2 changes: 1 addition & 1 deletion src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ impl Token {
endpoint: ENDPOINT.to_string(),
}
.get(PATH);
println!("{:?}", response);
println!("{response:?}");
}
}
16 changes: 8 additions & 8 deletions src/zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ impl Zone {
let response = CloudflareClient {
endpoint: ENDPOINT.to_string(),
}
.get(format!("{}/?per_page=100", PATH).as_str());
println!("{:?}", response);
.get(format!("{PATH}/?per_page=100").as_str());
println!("{response:?}");
}

pub(crate) fn list_domains() {
let response = CloudflareClient {
endpoint: ENDPOINT.to_string(),
}
.get(format!("{}/?per_page=100", PATH).as_str());
.get(format!("{PATH}/?per_page=100").as_str());
response
.result
.as_array()
.unwrap()
.iter()
.for_each(|zone| println!("{}", zone["name"].as_str().unwrap()))
.for_each(|zone| println!("{}", zone["name"].as_str().unwrap()));
}

pub(crate) fn create(name: String) {
Expand All @@ -33,14 +33,14 @@ impl Zone {
endpoint: ENDPOINT.to_string(),
}
.post_json(PATH, body);
println!("{:?}", response);
println!("{response:?}");
}

pub(crate) fn delete(id: String) {
pub(crate) fn delete(id: &str) {
let response = CloudflareClient {
endpoint: ENDPOINT.to_string(),
}
.delete(format!("{}/{}", PATH, id.as_str().replace('\"', "")));
println!("{:?}", response);
.delete(format!("{}/{}", PATH, id.replace('\"', "")).as_str());
println!("{response:?}");
}
}

0 comments on commit 5aadcb4

Please sign in to comment.