Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 2.0 #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ futures = "0.3.21"
grep = "0.2.8"
linkify = "0.8.0"
num_cpus = "1.13.1"
spinners = "3.0.1"
spinners = "3.1.0"
term = "0.7.0"
serde_json = "1.0.79"
json_value_merge = "1.1"

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ This project is a slim version of
USAGE:
urlsup [OPTIONS] <FILES>...

FLAGS:
--allow-timeout URLs that time out are allowed

OPTIONS:
-a, --allow <status codes> Comma separated status code errors to allow
--threads <thread count> Thread count for making requests (default: CPU core count)
-t, --timeout <seconds> Connection timeout in seconds (default: 30)
-w, --white-list <urls> Comma separated URLs to white list

ARGS:
<FILES>... Files to check

OPTIONS:
--allow-list <urls> Comma separated URLs to allow being non-OK
--allow-status <status codes> Comma separated status codes to allow
--allow-timeout URLs that time out are allowed
-h, --help Print help information
--threads <thread count> Thread count for making requests (default: CPU core count)
--timeout <seconds> Connection timeout per URL in seconds (default: 30)
-V, --version Print version information
```

## Examples
Expand Down Expand Up @@ -71,8 +71,8 @@ $ urlsup `find . -name "*.md"`
```

```bash
$ urlsup README.md --white-list rust,crates
# white list all links starting with rust or crates
$ urlsup README.md --allow-list rust,crates
# allow list all links starting with rust or crates

$ urlsup README.md,README-zh.md
# check links in 2 files
Expand Down
72 changes: 39 additions & 33 deletions src/bin/urlsup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ extern crate clap;
extern crate async_trait;
extern crate futures;
extern crate grep;
extern crate json_value_merge;
extern crate linkify;
extern crate num_cpus;
extern crate reqwest;
extern crate spinners;
extern crate term;

use clap::{Arg, Command};
use urlsup::finder::Finder;
use urlsup::validator::Validator;
use urlsup::finder::UrlFinder;
use urlsup::validator::UrlValidator;
use urlsup::{UrlsUp, UrlsUpOptions};

use std::ffi::OsStr;
use std::path::Path;
use std::time::Duration;
use urlsup::formatter::JsonFormatter;
use urlsup::printer::Writer;

const OPT_FILES: &str = "FILES";
const OPT_WHITE_LIST: &str = "white-list";
const OPT_ALLOW_LIST: &str = "allow-list";
const OPT_TIMEOUT: &str = "timeout";
const OPT_ALLOW: &str = "allow";
const OPT_ALLOW_STATUS: &str = "allow-status";
const OPT_THREADS: &str = "threads";
const OPT_ALLOW_TIMEOUT: &str = "allow-timeout";
const OPT_OUTPUT_TO_FILE: &str = "output-to-file";

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

Expand All @@ -38,26 +42,23 @@ async fn main() {
.required(true)
.index(1);

let opt_white_list = Arg::new(OPT_WHITE_LIST)
.help("Comma separated URLs to white list")
.short('w')
.long(OPT_WHITE_LIST)
let opt_allow_list = Arg::new(OPT_ALLOW_LIST)
.help("Comma separated URLs to allow being non-OK")
.long(OPT_ALLOW_LIST)
.value_name("urls")
.takes_value(true)
.required(false);

let opt_timeout = Arg::new(OPT_TIMEOUT)
.help("Connection timeout in seconds (default: 30)")
.short('t')
.help("Connection timeout per URL in seconds (default: 30)")
.long(OPT_TIMEOUT)
.value_name("seconds")
.takes_value(true)
.required(false);

let opt_allow = Arg::new(OPT_ALLOW)
.help("Comma separated status code errors to allow")
.short('a')
.long(OPT_ALLOW)
let opt_allow = Arg::new(OPT_ALLOW_STATUS)
.help("Comma separated status codes to allow")
.long(OPT_ALLOW_STATUS)
.value_name("status codes")
.takes_value(true)
.required(false);
Expand All @@ -75,36 +76,49 @@ async fn main() {
.takes_value(false)
.required(false);

let opt_output_to_file = Arg::new(OPT_OUTPUT_TO_FILE)
.help("Output results to file (urlsup-result.json)")
.long(OPT_OUTPUT_TO_FILE)
.takes_value(false)
.required(false);

let matches = Command::new("urls_up")
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(opt_word)
.arg(opt_white_list)
.arg(opt_allow_list)
.arg(opt_timeout)
.arg(opt_allow)
.arg(opt_threads)
.arg(opt_allow_timeout)
.arg(opt_output_to_file)
.get_matches();

let urls_up = UrlsUp::new(Finder::default(), Validator::default());
let urls_up = UrlsUp::new(
UrlFinder::default(),
UrlValidator::default(),
Writer::default(),
JsonFormatter::default(),
);
let mut opts = UrlsUpOptions {
white_list: None,
allow_list: None,
timeout: DEFAULT_TIMEOUT,
allowed_status_codes: None,
thread_count: num_cpus::get(),
allow_timeout: matches.is_present(OPT_ALLOW_TIMEOUT),
output_to_file: matches.is_present(OPT_OUTPUT_TO_FILE),
};

if let Some(white_list_urls) = matches.value_of(OPT_WHITE_LIST) {
let white_list: Vec<String> = white_list_urls
if let Some(allow_list_urls) = matches.value_of(OPT_ALLOW_LIST) {
let allow_list: Vec<String> = allow_list_urls
.split(',')
.filter_map(|s| match s.is_empty() {
true => None,
false => Some(s.to_string()),
})
.collect();
opts.white_list = Some(white_list);
opts.allow_list = Some(allow_list);
}

if let Some(str_timeout) = matches.value_of(OPT_TIMEOUT) {
Expand All @@ -115,7 +129,7 @@ async fn main() {
opts.timeout = timeout;
}

if let Some(allowed_status_codes) = matches.value_of(OPT_ALLOW) {
if let Some(allowed_status_codes) = matches.value_of(OPT_ALLOW_STATUS) {
let allowed: Vec<u16> = allowed_status_codes
.split(',')
.filter_map(|s| match s.is_empty() {
Expand All @@ -139,19 +153,11 @@ async fn main() {
let paths = files.map(Path::new).collect::<Vec<&Path>>();

match urls_up.run(paths, opts).await {
Ok(result) => {
if result.is_empty() {
println!("\n\n> No issues!");
} else {
println!("\n\n> Issues");
for (i, validation_result) in result.iter().enumerate() {
println!("{:4}. {}", i + 1, validation_result);
}

std::process::exit(1)
}
Ok(_) => std::process::exit(0),
Err(e) => {
println!("{}", e);
std::process::exit(1)
}
Err(e) => panic!("{}", e),
}
}
}
Expand Down
Loading