From fcfb956494c4c1f7e7aa563a29d116c5bbe966d6 Mon Sep 17 00:00:00 2001 From: Maximilian Goisser Date: Wed, 9 Jan 2019 21:17:42 +0100 Subject: [PATCH] feat(http): add --skip-http flag CLOSES #2 --- src/check.rs | 25 +++++++++++++++++-------- src/main.rs | 23 ++++++++++++++++++++--- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/check.rs b/src/check.rs index 38eeb97..c549d79 100644 --- a/src/check.rs +++ b/src/check.rs @@ -1,25 +1,27 @@ //! Provides functionality for checking the availablility of URLs. -use std::collections::HashSet; use reqwest::{self, StatusCode}; +use std::collections::HashSet; use url::Url; +use super::CheckContext; + /// Checks multiple URLs for availability. Returns `false` if at least one ULR is unavailable. -pub fn check_urls(urls: &HashSet) -> bool { +pub fn check_urls(urls: &HashSet, ctx: &CheckContext) -> bool { let mut result = true; for url in urls { - result &= check_url(url); + result &= check_url(url, ctx); } result } /// Check a single URL for availability. Returns `false` if it is unavailable. -fn check_url(url: &Url) -> bool { +fn check_url(url: &Url, ctx: &CheckContext) -> bool { match url.scheme() { - "file" => check_file_url(url), - "http" | "https" => check_http_url(url), + "file" => check_file_url(url, ctx), + "http" | "https" => check_http_url(url, ctx), scheme @ "javascript" => { debug!("Not checking URL scheme {:?}", scheme); true @@ -32,7 +34,7 @@ fn check_url(url: &Url) -> bool { } /// Check a URL with the "file" scheme for availability. Returns `false` if it is unavailable. -fn check_file_url(url: &Url) -> bool { +fn check_file_url(url: &Url, _ctx: &CheckContext) -> bool { let path = url.to_file_path().unwrap(); if path.exists() { @@ -45,7 +47,14 @@ fn check_file_url(url: &Url) -> bool { } /// Check a URL with "http" or "https" scheme for availability. Returns `false` if it is unavailable. -fn check_http_url(url: &Url) -> bool { +fn check_http_url(url: &Url, ctx: &CheckContext) -> bool { + if !ctx.check_http { + debug!( + "Skip checking {} as checking of http URLs is turned off", + url + ); + return true; + } let resp = reqwest::get(url.as_str()); match resp { Ok(r) => r.status() == StatusCode::Ok, diff --git a/src/main.rs b/src/main.rs index 1b941ee..979c730 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ Usage: Options: -h --help Print this message --dir Specify a directory to check (default is target/doc/) + --skip-http Skip checking of 'http' and 'https' scheme links --debug Use debug output -v --verbose Use verbose output -V --version Print version info and exit. @@ -52,6 +53,20 @@ struct MainArgs { arg_directory: Option, flag_verbose: bool, flag_debug: bool, + flag_skip_http: bool, +} + +#[derive(Debug)] +pub struct CheckContext { + check_http: bool, +} + +impl Into for MainArgs { + fn into(self) -> CheckContext { + CheckContext { + check_http: !self.flag_skip_http, + } + } } fn main() { @@ -66,9 +81,11 @@ fn main() { let dir = args .arg_directory + .clone() .map_or_else(determine_dir, |dir| PathBuf::from(dir)); let dir = dir.canonicalize().unwrap(); - if !walk_dir(&dir) { + let ctx: CheckContext = args.into(); + if !walk_dir(&dir, &ctx) { process::exit(1); } } @@ -136,7 +153,7 @@ fn is_html_file(entry: &DirEntry) -> bool { } /// Traverses a given path recursively, checking all *.html files found. -fn walk_dir(dir_path: &Path) -> bool { +fn walk_dir(dir_path: &Path, ctx: &CheckContext) -> bool { let pool = ThreadPoolBuilder::new() .num_threads(num_cpus::get()) .build() @@ -146,7 +163,7 @@ fn walk_dir(dir_path: &Path) -> bool { for entry in WalkDir::new(dir_path).into_iter().filter_map(|e| e.ok()) { if entry.file_type().is_file() && is_html_file(&entry) { let urls = parse_html_file(entry.path()); - let success = pool.install(|| check_urls(&urls)); + let success = pool.install(|| check_urls(&urls, &ctx)); result &= success; } }