Add --quiet flag to surpress non-error output (#48)

This commit is contained in:
Jonathan Dahan 2018-09-17 01:54:55 -04:00 committed by Danilo Bargen
commit 8784b55ba8
7 changed files with 98 additions and 6 deletions

View file

@ -11,6 +11,9 @@ Possible log types:
- `[fixed]` for any bug fixes.
- `[security]` to invite users to upgrade in case of vulnerabilities.
### Unreleased
- [added] New --quiet / -q option to suppress most non-error messages
### v1.0.0 (2018-02-11)

12
Cargo.lock generated
View file

@ -674,6 +674,7 @@ dependencies = [
"tempdir 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"xdg 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -773,6 +774,16 @@ name = "utf8-ranges"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utime"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "vcpkg"
version = "0.2.2"
@ -946,6 +957,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
"checksum url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fa35e768d4daf1d85733418a49fb42e10d7f633e394fccab4ab7aba897053fe2"
"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122"
"checksum utime 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4a9c0ddf7a5a39cd0c316dac124303d71fa197f8607027546c3be3e1c6f7bd9b"
"checksum vcpkg 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9e0a7d8bed3178a8fb112199d466eeca9ed09a14ba8ad67718179b4fd5487d0b"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"

View file

@ -32,6 +32,7 @@ xdg = "2.1.0"
[dev-dependencies]
tempdir = "^0.3"
assert_cli = "^0.5"
utime = "0.2.0"
[features]
dev = ["clippy"]

View file

@ -70,6 +70,7 @@ These are the clients I tried but failed to compile or run:
-o --os <type> Override the operating system [linux, osx, sunos]
-u --update Update the local cache
-c --clear-cache Clear the local cache
-q --quiet Suppress informational messages
--config-path Show config file path
--seed-config Create a basic config

View file

@ -6,7 +6,7 @@ _tealdeer()
_init_completion || return
case $prev in
-h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|--config-path|--seed-config)
-h|--help|-v|--version|-l|--list|-u|--update|-c|--clear-cache|--config-path|--seed-config|-q|--quiet)
return
;;
-f|--render)

View file

@ -75,6 +75,7 @@ Options:
-o --os <type> Override the operating system [linux, osx, sunos]
-u --update Update the local cache
-c --clear-cache Clear the local cache
-q --quiet Suppress informational messages
--config-path Show config file path
--seed-config Create a basic config
@ -105,6 +106,7 @@ struct Args {
flag_os: Option<OsType>,
flag_update: bool,
flag_clear_cache: bool,
flag_quiet: bool,
flag_config_path: bool,
flag_seed_config: bool,
}
@ -142,6 +144,7 @@ fn check_cache(args: &Args, cache: &Cache) {
if !args.flag_update {
match cache.last_update() {
Some(ago) if ago > MAX_CACHE_AGE => {
if args.flag_quiet { return; }
println!("{}", Color::Red.paint(format!(
"Cache wasn't updated in {} days.\n\
You should probably run `tldr --update` soon.",
@ -207,7 +210,9 @@ fn main() {
};
process::exit(1);
});
println!("Successfully deleted cache.");
if !args.flag_quiet {
println!("Successfully deleted cache.");
}
}
// Update cache, pass through
@ -219,7 +224,9 @@ fn main() {
};
process::exit(1);
});
println!("Successfully updated cache.");
if !args.flag_quiet {
println!("Successfully updated cache.");
}
}
// Render local file and exit
@ -266,9 +273,11 @@ fn main() {
process::exit(0);
}
} else {
println!("Page {} not found in cache", &command);
println!("Try updating with `tldr --update`, or submit a pull request to:");
eprintln!("https://github.com/tldr-pages/tldr");
if !args.flag_quiet {
println!("Page {} not found in cache", &command);
println!("Try updating with `tldr --update`, or submit a pull request to:");
println!("https://github.com/tldr-pages/tldr");
}
process::exit(1);
}
}

View file

@ -2,6 +2,7 @@
extern crate assert_cli;
extern crate tempdir;
extern crate utime;
use std::fs::File;
use std::io::Write;
@ -67,6 +68,71 @@ fn test_update_cache() {
.unwrap();
}
#[test]
fn test_quiet_cache() {
let testenv = TestEnv::new();
testenv
.assert()
.with_args(&["--update", "--quiet"])
.succeeds()
.stdout().is("")
.unwrap();
testenv
.assert()
.with_args(&["--clear-cache", "--quiet"])
.succeeds()
.stdout().is("")
.unwrap();
}
#[test]
fn test_quiet_failures() {
let testenv = TestEnv::new();
testenv
.assert()
.with_args(&["--update", "-q"])
.succeeds()
.stdout().is("")
.unwrap();
testenv
.assert()
.with_args(&["fakeprogram", "-q"])
.fails()
.stdout().is("")
.unwrap();
}
#[test]
fn test_quiet_old_cache() {
let testenv = TestEnv::new();
testenv
.assert()
.with_args(&["--update", "-q"])
.succeeds()
.stdout().is("")
.unwrap();
let _ = utime::set_file_times(testenv.cache_dir.path().join("tldr-master"), 1, 1).unwrap();
testenv
.assert()
.with_args(&["tldr"])
.succeeds()
.stdout().contains("Cache wasn't updated in ")
.unwrap();
testenv
.assert()
.with_args(&["tldr", "--quiet"])
.succeeds()
.stdout().doesnt_contain("Cache wasn't updated in ")
.unwrap();
}
#[test]
fn test_setup_seed_config() {
let testenv = TestEnv::new();