Fix path error on initial run (#313)

Fixes #312 

This isn't idiomatic but it'll do for the time being
This commit is contained in:
Denis Isidoro 2020-03-24 11:42:18 -03:00 committed by GitHub
parent 6ed7cd12b5
commit 65154663db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 19 deletions

2
Cargo.lock generated
View file

@ -250,7 +250,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "navi" name = "navi"
version = "2.4.0" version = "2.4.1"
dependencies = [ dependencies = [
"anyhow 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "anyhow 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "navi" name = "navi"
version = "2.4.0" version = "2.4.1"
authors = ["Denis Isidoro <denis_isidoro@live.com>"] authors = ["Denis Isidoro <denis_isidoro@live.com>"]
edition = "2018" edition = "2018"
description = "An interactive cheatsheet tool for the command-line" description = "An interactive cheatsheet tool for the command-line"

View file

@ -5,7 +5,11 @@ export NAVI_HOME="$(cd "$(dirname "$0")/.." && pwd)"
cd "$NAVI_HOME" cd "$NAVI_HOME"
./scripts/action release x86_64-unknown-linux-musl
docker run \ docker run \
-e HOMEBREW_NO_AUTO_UPDATE=1 \ -e HOMEBREW_NO_AUTO_UPDATE=1 \
-it linuxbrew/alpine \ -e HOMEBREW_NO_INSTALL_CLEANUP=1 \
bash -c 'brew install denisidoro/tools/navirs; bash' -v "$(pwd):/navi" \
-it 'bashell/alpine-bash' \
bash -c 'apk add git; apk add curl; git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && yes | ln -s /navi/target/debug/navi /usr/local/bin/navi; source $HOME/.bashrc; bash'

View file

@ -50,7 +50,7 @@ pub fn add(uri: String) -> Result<(), Error> {
let tmp_path_str = filesystem::tmp_path_str()?; let tmp_path_str = filesystem::tmp_path_str()?;
let tmp_path_str_with_trailing_slash = format!("{}/", &tmp_path_str); let tmp_path_str_with_trailing_slash = format!("{}/", &tmp_path_str);
filesystem::remove_dir(&tmp_path_str)?; let _ = filesystem::remove_dir(&tmp_path_str);
filesystem::create_dir(&tmp_path_str)?; filesystem::create_dir(&tmp_path_str)?;
eprintln!("Cloning {} into {}...\n", &actual_uri, &tmp_path_str); eprintln!("Cloning {} into {}...\n", &actual_uri, &tmp_path_str);

View file

@ -3,10 +3,7 @@ use crate::filesystem;
use crate::structures::cheat::VariableMap; use crate::structures::cheat::VariableMap;
use crate::structures::fnv::HashLine; use crate::structures::fnv::HashLine;
use crate::structures::fzf::{Opts as FzfOpts, SuggestionType}; use crate::structures::fzf::{Opts as FzfOpts, SuggestionType};
use crate::structures::{ use crate::structures::{error::filesystem::InvalidPath, option::Config};
error::filesystem::{InvalidPath, UnreadableDir},
option::Config,
};
use crate::welcome; use crate::welcome;
use anyhow::{Context, Error}; use anyhow::{Context, Error};
use regex::Regex; use regex::Regex;
@ -226,21 +223,30 @@ pub fn read_all(
let mut variables = VariableMap::new(); let mut variables = VariableMap::new();
let mut found_something = false; let mut found_something = false;
let mut visited_lines = HashSet::new(); let mut visited_lines = HashSet::new();
let paths = filesystem::cheat_paths(config)?; let paths = filesystem::cheat_paths(config);
if paths.is_err() {
welcome::cheatsheet(stdin);
return Ok(variables);
}
let paths = paths.expect("Unable to get paths");
let folders = paths_from_path_param(&paths); let folders = paths_from_path_param(&paths);
for folder in folders { for folder in folders {
if let Ok(dir_entries) = fs::read_dir(folder) { if let Ok(dir_entries) = fs::read_dir(folder) {
for entry in dir_entries { for entry in dir_entries {
let path = entry.map_err(|e| UnreadableDir::new(folder, e))?.path(); if entry.is_ok() {
let path_str = path let path = entry.expect("Impossible to read an invalid entry").path();
.to_str() let path_str = path
.ok_or_else(|| InvalidPath(path.to_path_buf()))?; .to_str()
if path_str.ends_with(".cheat") .ok_or_else(|| InvalidPath(path.to_path_buf()))?;
&& read_file(path_str, &mut variables, &mut visited_lines, stdin).is_ok() if path_str.ends_with(".cheat")
&& !found_something && read_file(path_str, &mut variables, &mut visited_lines, stdin).is_ok()
{ && !found_something
found_something = true; {
found_something = true;
}
} }
} }
} }