Skip to content

Commit

Permalink
Merge pull request #28 from deadlinks/feat/skip-http
Browse files Browse the repository at this point in the history
feat(http): add --skip-http flag
  • Loading branch information
hobofan authored Jan 9, 2019
2 parents 096dbc0 + fcfb956 commit f98d967
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/check.rs
Original file line number Diff line number Diff line change
@@ -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<Url>) -> bool {
pub fn check_urls(urls: &HashSet<Url>, 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
Expand All @@ -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() {
Expand All @@ -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,
Expand Down
23 changes: 20 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Usage:
Options:
-h --help Print this message
--dir Specify a directory to check (default is target/doc/<package>)
--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.
Expand All @@ -52,6 +53,20 @@ struct MainArgs {
arg_directory: Option<String>,
flag_verbose: bool,
flag_debug: bool,
flag_skip_http: bool,
}

#[derive(Debug)]
pub struct CheckContext {
check_http: bool,
}

impl Into<CheckContext> for MainArgs {
fn into(self) -> CheckContext {
CheckContext {
check_http: !self.flag_skip_http,
}
}
}

fn main() {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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;
}
}
Expand Down

0 comments on commit f98d967

Please sign in to comment.