Implement new --show-dirs command

The command prints out the different directories (config, cache, etc)
used by tealdeer.
This commit is contained in:
Danilo Bargen 2021-01-30 21:31:10 +01:00
commit 978debe6ae
8 changed files with 61 additions and 12 deletions

View file

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

View file

@ -8,7 +8,7 @@ the config file can be done manually or with the help of `tldr`:
The configuration file path follows OS conventions. It can be queried with the
following command:
$ tldr --config-path
$ tldr --show-paths
On Linux, this will usually be `~/.config/tealdeer/config.toml`.

View file

@ -13,7 +13,7 @@ complete -c tldr -s c -l clear-cache -d 'Clear the local cache.' -f
complete -c tldr -s p -l pager -d 'Use a pager to page output.' -f
complete -c tldr -s m -l markdown -d 'Display the raw markdown instead of rendering it.' -f
complete -c tldr -s q -l quiet -d 'Suppress informational messages.' -f
complete -c tldr -l config-path -d 'Show config file path.' -f
complete -c tldr -l show-paths -d 'Show file and directory paths used by tealdeer.' -f
complete -c tldr -l seed-config -d 'Create a basic config.' -f
complete -c tldr -l color -d 'Controls when to use color.' -xa 'always auto never'

View file

@ -33,7 +33,7 @@ impl Cache {
}
/// Return the path to the cache directory.
fn get_cache_dir() -> Result<PathBuf, TealdeerError> {
pub fn get_cache_dir() -> Result<PathBuf, TealdeerError> {
// Allow overriding the cache directory by setting the
// $TEALDEER_CACHE_DIR env variable.
if let Ok(value) = env::var("TEALDEER_CACHE_DIR") {

View file

@ -42,7 +42,7 @@ mod tokenizer;
mod types;
use crate::cache::Cache;
use crate::config::{get_config_path, make_default_config, Config, MAX_CACHE_AGE};
use crate::config::{get_config_dir, get_config_path, make_default_config, Config, MAX_CACHE_AGE};
use crate::dedup::Dedup;
use crate::error::TealdeerError::{CacheError, ConfigError, UpdateError};
use crate::formatter::print_lines;
@ -73,6 +73,7 @@ struct Args {
flag_clear_cache: bool,
flag_pager: bool,
flag_quiet: bool,
flag_show_paths: bool,
flag_config_path: bool,
flag_seed_config: bool,
flag_markdown: bool,
@ -179,7 +180,7 @@ fn update_cache(cache: &Cache, quietly: bool) {
}
}
/// Show the config path
/// Show the config path (DEPRECATED)
fn show_config_path() {
match get_config_path() {
Ok(config_file_path) => {
@ -196,6 +197,36 @@ fn show_config_path() {
}
}
/// Show file paths
fn show_paths() {
let config_dir = get_config_dir()
.map(|mut path| {
path.push(""); // Trailing path separator
path.to_str().unwrap_or("[Invalid]").to_string()
})
.unwrap_or_else(|e| format!("[Error: {}]", e));
let config_path = get_config_path()
.map(|path| path.to_str().unwrap_or("[Invalid]").to_string())
.unwrap_or_else(|e| format!("[Error: {}]", e));
let cache_dir = Cache::get_cache_dir()
.map(|mut path| {
path.push(""); // Trailing path separator
path.to_str().unwrap_or("[Invalid]").to_string()
})
.unwrap_or_else(|e| format!("[Error: {}]", e));
let pages_dir = Cache::get_cache_dir()
.map(|path| path.join("tldr-master"))
.map(|mut path| {
path.push(""); // Trailing path separator
path.to_str().unwrap_or("[Invalid]").to_string()
})
.unwrap_or_else(|e| format!("[Error: {}]", e));
println!("Config dir: {}", config_dir);
println!("Config path: {}", config_path);
println!("Cache dir: {}", cache_dir);
println!("Pages dir: {}", pages_dir);
}
/// Create seed config file and exit
fn create_config_and_exit() {
match make_default_config() {
@ -315,8 +346,12 @@ fn main() {
// Show config file and path, pass through
if args.flag_config_path {
eprintln!("Warning: The --config-path flag is deprecated, use --show-paths instead");
show_config_path();
}
if args.flag_show_paths {
show_paths();
}
// Create a basic config and exit
if args.flag_seed_config {
@ -450,7 +485,8 @@ fn main() {
}
// Some flags can be run without a command.
if !(args.flag_update || args.flag_clear_cache || args.flag_config_path) {
if !(args.flag_update || args.flag_clear_cache || args.flag_config_path || args.flag_show_paths)
{
eprintln!("{}", USAGE);
process::exit(1);
}

View file

@ -16,7 +16,8 @@ Options:
-p --pager Use a pager to page output
-m --markdown Display the raw markdown instead of rendering it
-q --quiet Suppress informational messages
--config-path Show config file path
--show-paths Show file and directory paths used by tealdeer
--config-path Show config file path (deprecated)
--seed-config Create a basic config
--color <when> Control when to use color [always, auto, never] [default: auto]

View file

@ -202,22 +202,34 @@ fn test_setup_seed_config() {
}
#[test]
fn test_show_config_path() {
fn test_show_paths() {
let testenv = TestEnv::new();
testenv
.command()
.args(&["--config-path"])
.args(&["--show-paths"])
.assert()
.success()
.stdout(contains(format!(
"Config path is: {}",
"Config dir: {}",
testenv.config_dir.path().to_str().unwrap(),
)))
.stdout(contains(format!(
"Config path: {}",
testenv
.config_dir
.path()
.join("config.toml")
.to_str()
.unwrap(),
)))
.stdout(contains(format!(
"Cache dir: {}",
testenv.cache_dir.path().to_str().unwrap(),
)))
.stdout(contains(format!(
"Pages dir: {}",
testenv.cache_dir.path().join("tldr-master").to_str().unwrap(),
)));
}

View file

@ -23,7 +23,7 @@ _tealdeer() {
"($I -p --pager)"{-p,--pager}"[Use a pager to page output]"
"($I -m --markdown)"{-m,--markdown}"[Display the raw markdown instead of rendering it]"
"($I -q --quiet)"{-q,--quiet}"[Suppress informational messages]"
"($I)--config-path[Show config file path]"
"($I)--show-paths[Show file and directory paths used by tealdeer]"
"($I)--seed-config[Create a basic config]"
"($I)--color[Controls when to use color]:when:((
always