From 0128e267ccf8ac94395f3ef85cdf6d943d32486b Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Mon, 22 Jul 2019 22:26:37 +0200 Subject: [PATCH] Use async client to check if a repo exists --- src/main.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9f63d4b..ee911fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,8 +137,12 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String, u64) Ok((cache.count, head, commits)) } -fn remote_exists(url: &str) -> Result { - Ok(CLIENT.head(url).send()?.status() == reqwest::StatusCode::OK) +fn remote_exists(url: &str) -> impl Future { + CLIENT + .head(url) + .send() + .map(|resp| resp.status() == reqwest::StatusCode::OK) + .from_err() } enum HocResult { @@ -163,15 +167,15 @@ where T: Service, F: Fn(HocResult) -> Result, { - futures::future::result(Ok(())) - .and_then(move |_| { - let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase()); - let service_path = format!("{}/{}", T::domain(), repo); - let path = format!("{}/{}", state.repos, service_path); + let repo = format!("{}/{}", data.0.to_lowercase(), data.1.to_lowercase()); + let service_path = format!("{}/{}", T::domain(), repo); + let path = format!("{}/{}", state.repos, service_path); + let url = format!("https://{}", service_path); + remote_exists(&url) + .and_then(move |remote_exists| { let file = Path::new(&path); - let url = format!("https://{}", service_path); if !file.exists() { - if !remote_exists(&url)? { + if !remote_exists { warn!("Repository does not exist: {}", url); return Ok(HocResult::NotFound); }