mirror of
https://github.com/denisidoro/navi
synced 2024-11-14 15:57:13 +00:00
Add context to possible errors for the repo
subcommand.
This commit is contained in:
parent
1d33211c45
commit
f1182279a9
4 changed files with 27 additions and 23 deletions
|
@ -1,4 +1,5 @@
|
|||
use crate::structures::option::Config;
|
||||
use anyhow::Error;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead, BufReader, Lines};
|
||||
|
@ -16,15 +17,14 @@ pub fn pathbuf_to_string(pathbuf: PathBuf) -> String {
|
|||
pathbuf.as_os_str().to_str().unwrap().to_string()
|
||||
}
|
||||
|
||||
pub fn cheat_pathbuf() -> Option<PathBuf> {
|
||||
match dirs::data_dir() {
|
||||
Some(mut d) => {
|
||||
d.push("navi");
|
||||
d.push("cheats");
|
||||
Some(d)
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
pub fn cheat_pathbuf() -> Result<PathBuf, Error> {
|
||||
dirs::data_dir()
|
||||
.map(|mut dir| {
|
||||
dir.push("navi");
|
||||
dir.push("cheats");
|
||||
dir
|
||||
})
|
||||
.ok_or_else(|| anyhow!("Unable to acquire user data directory for cheatsheets."))
|
||||
}
|
||||
|
||||
fn follow_symlink(pathbuf: PathBuf) -> PathBuf {
|
||||
|
@ -57,7 +57,7 @@ pub fn exe_string() -> String {
|
|||
fn cheat_paths_from_config_dir() -> String {
|
||||
let mut paths_str = String::from("");
|
||||
|
||||
if let Some(f) = cheat_pathbuf() {
|
||||
if let Ok(f) = cheat_pathbuf() {
|
||||
if let Ok(paths) = fs::read_dir(pathbuf_to_string(f)) {
|
||||
for path in paths {
|
||||
paths_str.push_str(path.unwrap().path().into_os_string().to_str().unwrap());
|
||||
|
|
|
@ -3,13 +3,13 @@ use crate::fzf;
|
|||
use crate::git;
|
||||
use crate::structures::fzf::{Opts as FzfOpts, SuggestionType};
|
||||
use anyhow::Context;
|
||||
use anyhow::Error;
|
||||
use git2::Repository;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
pub fn browse() -> Result<(), Box<dyn Error>> {
|
||||
pub fn browse() -> Result<(), Error> {
|
||||
let repo_path_str = format!("{}/featured", filesystem::tmp_path_str());
|
||||
|
||||
filesystem::remove_dir(&repo_path_str);
|
||||
|
@ -17,10 +17,10 @@ pub fn browse() -> Result<(), Box<dyn Error>> {
|
|||
|
||||
let repo_url = "https://github.com/denisidoro/cheats";
|
||||
Repository::clone(repo_url, &repo_path_str)
|
||||
.with_context(|| format!("Failed to clone {}.", repo_url))?;
|
||||
.with_context(|| format!("Failed to clone {}", repo_url))?;
|
||||
|
||||
let repos = fs::read_to_string(format!("{}/featured_repos.txt", &repo_path_str))
|
||||
.context("Unable to fetch featured repos.")?;
|
||||
.context("Unable to fetch featured repositories")?;
|
||||
|
||||
let opts = FzfOpts {
|
||||
column: Some(1),
|
||||
|
@ -30,7 +30,7 @@ pub fn browse() -> Result<(), Box<dyn Error>> {
|
|||
let (repo, _) = fzf::call(opts, |stdin| {
|
||||
stdin
|
||||
.write_all(repos.as_bytes())
|
||||
.expect("Unable to prompt featured repos.");
|
||||
.expect("Unable to prompt featured repositories");
|
||||
None
|
||||
});
|
||||
|
||||
|
@ -39,10 +39,10 @@ pub fn browse() -> Result<(), Box<dyn Error>> {
|
|||
add(repo)
|
||||
}
|
||||
|
||||
pub fn add(uri: String) -> Result<(), Box<dyn Error>> {
|
||||
pub fn add(uri: String) -> Result<(), Error> {
|
||||
let (actual_uri, user, repo) = git::meta(uri.as_str());
|
||||
|
||||
let cheat_path_str = filesystem::pathbuf_to_string(filesystem::cheat_pathbuf().unwrap());
|
||||
let cheat_path_str = filesystem::pathbuf_to_string(filesystem::cheat_pathbuf()?);
|
||||
let tmp_path_str = filesystem::tmp_path_str();
|
||||
let tmp_path_str_with_trailing_slash = format!("{}/", &tmp_path_str);
|
||||
|
||||
|
@ -51,10 +51,8 @@ pub fn add(uri: String) -> Result<(), Box<dyn Error>> {
|
|||
|
||||
eprintln!("Cloning {} into {}...\n", &actual_uri, &tmp_path_str);
|
||||
|
||||
match Repository::clone(actual_uri.as_str(), &tmp_path_str) {
|
||||
Ok(r) => r,
|
||||
Err(e) => panic!("failed to clone: {}", e),
|
||||
};
|
||||
Repository::clone(actual_uri.as_str(), &tmp_path_str)
|
||||
.with_context(|| format!("Failed to clone {}", actual_uri))?;
|
||||
|
||||
let all_files = WalkDir::new(&tmp_path_str)
|
||||
.into_iter()
|
||||
|
|
|
@ -2,6 +2,7 @@ use crate::flows;
|
|||
use crate::flows::core::Variant;
|
||||
use crate::structures::option::Command::{Best, Fn, Preview, Query, Repo, Search, Widget};
|
||||
use crate::structures::option::{Config, RepoCommand};
|
||||
use anyhow::Context;
|
||||
use std::error::Error;
|
||||
|
||||
pub fn handle_config(mut config: Config) -> Result<(), Box<dyn Error>> {
|
||||
|
@ -15,8 +16,11 @@ pub fn handle_config(mut config: Config) -> Result<(), Box<dyn Error>> {
|
|||
Widget { shell } => flows::shell::main(&shell[..]),
|
||||
Fn { func, args } => flows::func::main(func.clone(), args.to_vec()),
|
||||
Repo { cmd } => match cmd {
|
||||
RepoCommand::Add { uri } => flows::repo::add(uri.clone()),
|
||||
RepoCommand::Browse => flows::repo::browse(),
|
||||
RepoCommand::Add { uri } => Ok(flows::repo::add(uri.clone())
|
||||
.with_context(|| format!("Failed to import cheatsheets from {}", uri))?),
|
||||
RepoCommand::Browse => {
|
||||
Ok(flows::repo::browse().context("Failed to browse featured cheatsheets")?)
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate anyhow;
|
||||
|
||||
mod display;
|
||||
mod filesystem;
|
||||
|
|
Loading…
Reference in a new issue