Skip to content

Commit

Permalink
Improve platform detection error messages and the help message
Browse files Browse the repository at this point in the history
  • Loading branch information
acuteenvy committed Nov 18, 2023
1 parent 04c3eb8 commit 7e72c21
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub struct Cli {
#[arg(long, group = "operations")]
pub config_path: bool,

/// Specify the platform to use.
/// Specify the platform to use (linux, osx, windows, etc.).
#[arg(short, long, default_value = DEFAULT_PLATFORM)]
pub platform: String,

Expand Down
33 changes: 15 additions & 18 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ impl<'a> Cache<'a> {
let lang_dirs = util::languages_to_langdirs(&languages);

for lang_dir in &lang_dirs {
dirs_npages.insert(lang_dir.to_string(), self.list_all_vec(lang_dir)?.len());
// `list_all_vec` can fail when `pages.en` is empty, hence the default of 0.
let n = self.list_all_vec(lang_dir).map(|v| v.len()).unwrap_or(0);
dirs_npages.insert(lang_dir.to_string(), n);
}

let archives = Self::download_and_verify(&languages)?;
Expand Down Expand Up @@ -219,37 +221,32 @@ impl<'a> Cache<'a> {
/// Find out what platforms are available.
fn get_platforms(&self) -> Result<Vec<OsString>> {
let mut result = vec![];
let read_dir = fs::read_dir(self.0.join(ENGLISH_DIR));

match &read_dir {
// Return an empty Vec if the English dir does not exist
// in order not to fail.
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(vec![]),
_ => {}
}

for entry in read_dir? {
for entry in fs::read_dir(self.0.join(ENGLISH_DIR))? {
let entry = entry?;
let path = entry.path();
let platform = path.file_name().unwrap();

result.push(platform.to_os_string());
}

// read_dir() order can differ across runs, so it's
// better to sort the Vec for consistency.
result.sort_unstable();

Ok(result)
if result.is_empty() {
Err(Error::new(
"'pages.en' contains no platform directories. Please run 'tldr --update'.",
))
} else {
// read_dir() order can differ across runs, so it's
// better to sort the Vec for consistency.
result.sort_unstable();
Ok(result)
}
}

/// Find out what platforms are available and check if the provided platform exists.
fn get_platforms_and_check(&self, platform: &str) -> Result<Vec<OsString>> {
let platforms = self.get_platforms()?;

// The !platforms.is_empty() check is here to get a "page not found" error instead of "invalid platform"
// when `pages.en` does not exist (because the dir was named `pages` in previous versions).
if platforms.iter().all(|x| x != platform) && !platforms.is_empty() {
if platforms.iter().all(|x| x != platform) {
Err(Error::new(format!(
"platform '{platform}' does not exist.\n{} {}.",
Paint::new("Possible values:").bold(),
Expand Down

0 comments on commit 7e72c21

Please sign in to comment.