Skip to content

Commit

Permalink
Implement verbose mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptondereau committed Sep 22, 2022
1 parent f622ee4 commit 43378c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion xtask/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Lint {
cargo_runner.lint()?;
let npm_runner = NpmRunner::new(verbose)?;
npm_runner.lint()?;
let lychee_runner = LycheeRunner::new()?;
let lychee_runner = LycheeRunner::new(verbose)?;
lychee_runner.lint()?;
Ok(())
}
Expand Down
31 changes: 23 additions & 8 deletions xtask/src/tools/lychee.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use reqwest::StatusCode;
use std::{collections::HashSet, fs, path::PathBuf, time::Duration};
use std::{borrow::Borrow, collections::HashSet, fs, path::PathBuf, time::Duration};
use tokio::runtime::Runtime;
use tokio_stream::StreamExt;

Expand All @@ -12,10 +12,11 @@ use crate::utils::PKG_PROJECT_ROOT;

pub(crate) struct LycheeRunner {
client: Client,
verbose: bool,
}

impl LycheeRunner {
pub(crate) fn new() -> Result<Self> {
pub(crate) fn new(verbose: bool) -> Result<Self> {
let accepted = Some(HashSet::from_iter(vec![
StatusCode::OK,
StatusCode::TOO_MANY_REQUESTS,
Expand All @@ -29,10 +30,14 @@ impl LycheeRunner {
.build()
.client()?;

Ok(Self { client })
Ok(Self { client, verbose })
}

pub(crate) fn lint(&self) -> Result<()> {
if self.verbose {
println!("Checking links in documentation");
}

let inputs: Vec<Input> = get_md_files()
.iter()
.map(|file| Input {
Expand All @@ -53,17 +58,27 @@ impl LycheeRunner {
.collect::<LycheeResult<Vec<_>>>()
.await?;

// PoC is validated
// TODO: gather only failed requests into a vec instead of fail at first.
let mut has_failures = false;
let links_size = links.len();

for link in links {
let uri = link.clone().uri;
let response = lychee_client.check(link).await?;

if response.status().is_failure() {
return Err(anyhow!("Link down: {}", uri.as_str()));
has_failures = true;
if self.verbose {
println!("[x] {}", response.1.uri.as_str());
}
} else if response.status().is_success() {
println!("[✓] {}", response.1.uri.as_str());
}
}

println!("{} links checked.", links_size);

if has_failures {
return Err(anyhow!("Some links in markdown documentation are down."));
}

Ok::<(), Error>(())
})?;

Expand Down

0 comments on commit 43378c4

Please sign in to comment.