Skip to content

Commit

Permalink
Improve display of findings
Browse files Browse the repository at this point in the history
  • Loading branch information
pips.linux authored and pips.linux committed Dec 6, 2017
1 parent 71fa9c9 commit f0057be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
36 changes: 25 additions & 11 deletions src/check.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
//! Provides functionality for checking the availablility of URLs.
use url::Url;
use std::path::Path;
use MainArgs;

/// Checks multiple URLs for availability. Returns `false` if at least one ULR is unavailable.
pub fn check_urls(urls: &[Url]) -> bool {
pub fn check_urls(urls: &[Url], root: &Path, file: &Path, args: &MainArgs) -> bool {
let mut result = true;

for url in urls {
result &= check_url(url);
result &= check_url(url, root, file, args);
}

result
}

/// Check a single URL for availability. Returns `false` if it is unavailable.
fn check_url(url: &Url) -> bool {
fn check_url(url: &Url, root: &Path, file: &Path, args: &MainArgs) -> bool {
match &*url.scheme {
"file" => {
check_file_url(url)
check_file_url(url, &root, file, args)
},
"http" | "https" => {
check_http_url(url)
Expand All @@ -33,15 +35,27 @@ 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, base_dir: &Path, file: &Path, args: &MainArgs) -> bool {
let path = url.to_file_path().unwrap();

if path.exists() {
debug!("Linked file at path {} does exist.", path.display());
true

if args.flag_debug {
if path.exists() {
debug!("{}: Linked file {} exists.", file.display(), path.display());
true
} else {
error!("{}: Linked file {} does not exist!", file.display(), path.display());
false
}
} else {
error!("Linked file at path {} does not exist!", path.display());
false
let shortpath = path.strip_prefix(base_dir).unwrap_or_else(|_| &path);
let shortfile = file.strip_prefix(base_dir).unwrap_or_else(|_| &file);
if path.exists() {
debug!("{}: Linked file {} exists.", shortfile.display(), shortpath.display());
true
} else {
error!("{}: Linked file {} does not exist!", shortfile.display(), shortpath.display());
false
}
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Options:
";

#[derive(Debug, RustcDecodable)]
struct MainArgs {
pub struct MainArgs {
arg_directory: Option<String>,
flag_verbose: bool,
flag_debug: bool,
Expand All @@ -56,9 +56,9 @@ fn main() {

init_logger(&args);

let dir = args.arg_directory.map_or_else(determine_dir, |dir| PathBuf::from(dir));
let dir = args.arg_directory.clone().map_or_else(determine_dir, |dir| PathBuf::from(dir));
let dir = dir.canonicalize().unwrap();
if !walk_dir(&dir) {
if !walk_dir(&dir, &args) {
process::exit(1);
}
}
Expand Down Expand Up @@ -101,7 +101,7 @@ fn determine_dir() -> PathBuf {
}

/// Traverses a given path recursively, checking all *.html files found.
fn walk_dir(dir_path: &Path) -> bool {
fn walk_dir(dir_path: &Path, args: &MainArgs) -> bool {
let mut result = true;

match dir_path.read_dir() {
Expand All @@ -114,10 +114,11 @@ fn walk_dir(dir_path: &Path) -> bool {
let extension = entry_path.extension();
if extension == Some("html".as_ref()) {
let urls = parse_html_file(&entry.path());
result &= check_urls(&urls);
result &= check_urls(&urls, dir_path,
&entry.path(), args);
}
} else {
result &= walk_dir(&entry.path());
result &= walk_dir(&entry.path(), args);
}
},
Err(err) => {
Expand Down

0 comments on commit f0057be

Please sign in to comment.