mirror of
https://github.com/Hopiu/lychee.git
synced 2026-03-16 20:50:25 +00:00
Rewrite link checking into iterator
This is a preparation for easier async execution
This commit is contained in:
parent
8588d2eade
commit
262b18e6b8
2 changed files with 27 additions and 17 deletions
|
|
@ -11,11 +11,12 @@ use url::Url;
|
|||
pub(crate) struct Checker {
|
||||
reqwest_client: reqwest::blocking::Client,
|
||||
gh_client: Github,
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
impl Checker {
|
||||
/// Creates a new link checker
|
||||
pub fn try_new(token: String) -> Result<Self> {
|
||||
pub fn try_new(token: String, verbose: bool) -> Result<Self> {
|
||||
let mut headers = header::HeaderMap::new();
|
||||
// Faking the user agent is necessary for some websites, unfortunately.
|
||||
// Otherwise we get a 403 from the firewall (e.g. Sucuri/Cloudproxy on ldra.com).
|
||||
|
|
@ -31,6 +32,7 @@ impl Checker {
|
|||
Ok(Checker {
|
||||
reqwest_client,
|
||||
gh_client,
|
||||
verbose,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +75,7 @@ impl Checker {
|
|||
Ok((owner.as_str().into(), repo.as_str().into()))
|
||||
}
|
||||
|
||||
pub fn check(&self, url: &Url) -> bool {
|
||||
pub fn check_real(&self, url: &Url) -> bool {
|
||||
if self.check_normal(&url) {
|
||||
return true;
|
||||
}
|
||||
|
|
@ -84,6 +86,21 @@ impl Checker {
|
|||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn check(&self, url: &Url) -> bool {
|
||||
let ret = self.check_real(&url);
|
||||
match ret {
|
||||
true => {
|
||||
if self.verbose {
|
||||
println!("✅{}", &url);
|
||||
}
|
||||
}
|
||||
false => {
|
||||
println!("❌{}", &url);
|
||||
}
|
||||
};
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
23
src/main.rs
23
src/main.rs
|
|
@ -25,23 +25,16 @@ fn main() -> Result<()> {
|
|||
input: args.opt_value_from_str(["-i", "--input"])?,
|
||||
};
|
||||
|
||||
let checker = Checker::try_new(env::var("GITHUB_TOKEN")?)?;
|
||||
let checker = Checker::try_new(env::var("GITHUB_TOKEN")?, args.verbose)?;
|
||||
let md = fs::read_to_string(args.input.unwrap_or_else(|| "README.md".into()))?;
|
||||
let links = extract_links(&md);
|
||||
|
||||
let mut errorcode = 0;
|
||||
for link in links {
|
||||
match checker.check(&link) {
|
||||
true => {
|
||||
if args.verbose {
|
||||
println!("✅{}", link);
|
||||
}
|
||||
}
|
||||
false => {
|
||||
println!("❌{}", link);
|
||||
errorcode = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
let results: Vec<bool> = links.iter().map(|l| checker.check(&l)).collect();
|
||||
|
||||
let errorcode = if results.iter().all(|r| r == &true) {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
};
|
||||
std::process::exit(errorcode)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue