Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling for cheatsh and tldr #766

Merged
merged 3 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 27 additions & 53 deletions src/clients/cheatsh.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
use crate::parser::Parser;
use crate::prelude::*;

use crate::structures::fetcher;
use std::process::{self, Command};
use std::process::Command;

fn map_line(line: &str) -> String {
line.trim().trim_end_matches(':').to_string()
}

fn lines(query: &str, markdown: &str) -> impl Iterator<Item = Result<String>> {
fn as_lines(query: &str, markdown: &str) -> Vec<String> {
format!(
"% {}, cheat.sh
{}",
query, markdown
)
.lines()
.map(|line| Ok(map_line(line)))
.collect::<Vec<Result<String>>>()
.into_iter()
.map(map_line)
.collect()
}

pub fn fetch(query: &str) -> Result<String> {
pub fn call(query: &str) -> Result<Vec<String>> {
let args = ["-qO-", &format!("cheat.sh/{}", query)];

let child = Command::new("wget")
Expand All @@ -32,19 +28,34 @@ pub fn fetch(query: &str) -> Result<String> {
let child = match child {
Ok(x) => x,
Err(_) => {
eprintln!(
"navi was unable to call wget.
Make sure wget is correctly installed."
);
process::exit(34)
let msg = "navi was unable to call wget.
Make sure wget is correctly installed.";
return Err(anyhow!(msg));
}
};

let out = child.wait_with_output().context("Failed to wait for wget")?;

if let Some(0) = out.status.code() {
let stdout = out.stdout;
let plain_bytes = strip_ansi_escapes::strip(&stdout)?;

let markdown = String::from_utf8(plain_bytes).context("Output is invalid utf8")?;
if markdown.starts_with("Unknown topic.") {
let msg = format!(
"`{}` not found in cheatsh.
Output:
{}
",
&query, markdown,
);
return Err(anyhow!(msg));
}

let lines = as_lines(query, &markdown);
Ok(lines)
} else {
eprintln!(
let msg = format!(
"Failed to call:
wget {}

Expand All @@ -58,43 +69,6 @@ Error:
String::from_utf8(out.stdout).unwrap_or_else(|_e| "Unable to get output message".to_string()),
String::from_utf8(out.stderr).unwrap_or_else(|_e| "Unable to get error message".to_string())
);
process::exit(35)
}

let stdout = out.stdout;
let plain_bytes = strip_ansi_escapes::strip(&stdout)?;

String::from_utf8(plain_bytes).context("Output is invalid utf8")
}

pub struct Fetcher {
query: String,
}

impl Fetcher {
pub fn new(query: String) -> Self {
Self { query }
}
}

impl fetcher::Fetcher for Fetcher {
fn fetch(&self, parser: &mut Parser) -> Result<bool> {
let cheat = &fetch(&self.query)?;

if cheat.starts_with("Unknown topic.") {
eprintln!(
"`{}` not found in cheatsh.

Output:
{}
",
&self.query, cheat
);
process::exit(35)
}

parser.read_lines(lines(&self.query, cheat), "cheat.sh", None)?;

Ok(true)
Err(anyhow!(msg))
}
}
48 changes: 14 additions & 34 deletions src/clients/tldr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::parser::Parser;
use crate::prelude::*;
use crate::structures::fetcher;
use std::process::{self, Command, Stdio};

lazy_static! {
Expand Down Expand Up @@ -42,19 +40,18 @@ fn convert_tldr(line: &str) -> String {
}
}

fn markdown_lines(query: &str, markdown: &str) -> impl Iterator<Item = Result<String>> {
fn markdown_lines(query: &str, markdown: &str) -> Vec<String> {
format!(
"% {}, tldr
{}",
query, markdown
)
.lines()
.map(|line| Ok(convert_tldr(line)))
.collect::<Vec<Result<String>>>()
.into_iter()
.map(convert_tldr)
.collect()
}

pub fn fetch(query: &str) -> Result<String> {
pub fn call(query: &str) -> Result<Vec<String>> {
let args = [query, "--markdown"];

let child = Command::new("tldr")
Expand All @@ -67,7 +64,7 @@ pub fn fetch(query: &str) -> Result<String> {
let child = match child {
Ok(x) => x,
Err(_) => {
eprintln!(
let msg = format!(
"navi was unable to call tldr.
Make sure tldr is correctly installed.
Refer to https://github.com/tldr-pages/tldr for more info.
Expand All @@ -77,15 +74,20 @@ Note:
",
VERSION_DISCLAIMER
);
process::exit(34)
return Err(anyhow!(msg));
}
};

let out = child.wait_with_output().context("Failed to wait for tldr")?;

if let Some(0) = out.status.code() {
let stdout = out.stdout;

let markdown = String::from_utf8(stdout).context("Output is invalid utf8")?;
let lines = markdown_lines(query, &markdown);
Ok(lines)
} else {
eprintln!(
let msg = format!(
"Failed to call:
tldr {}

Expand All @@ -103,30 +105,8 @@ If you are already using a supported version you can ignore this message.
args.join(" "),
String::from_utf8(out.stdout).unwrap_or_else(|_e| "Unable to get output message".to_string()),
String::from_utf8(out.stderr).unwrap_or_else(|_e| "Unable to get error message".to_string()),
VERSION_DISCLAIMER
VERSION_DISCLAIMER,
);
process::exit(35)
}

let stdout = out.stdout;

String::from_utf8(stdout).context("Output is invalid utf8")
}

pub struct Fetcher {
query: String,
}

impl Fetcher {
pub fn new(query: String) -> Self {
Self { query }
}
}

impl fetcher::Fetcher for Fetcher {
fn fetch(&self, parser: &mut Parser) -> Result<bool> {
let markdown = fetch(&self.query)?;
parser.read_lines(markdown_lines(&self.query, &markdown), "markdown", None)?;
Ok(true)
Err(anyhow!(msg))
}
}
39 changes: 35 additions & 4 deletions src/commands/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
mod actor;
mod extractor;

use crate::clients::cheatsh;
use crate::config::Source;
use crate::filesystem;
use crate::finder::structures::Opts as FinderOpts;
use crate::parser::Parser;
use crate::prelude::*;
use crate::structures::fetcher::{Fetcher, StaticFetcher};
use crate::welcome;

pub fn main() -> Result<()> {
pub fn init(fetcher: Box<dyn Fetcher>) -> Result<()> {
let config = &CONFIG;
let opts = FinderOpts::snippet_default();
// let fetcher = config.fetcher();

let (raw_selection, (variables, files)) = config
.finder()
.call(opts, |writer| {
let fetcher = config.fetcher();

let mut parser = Parser::new(writer, true);

let found_something = fetcher
Expand All @@ -32,10 +35,38 @@ pub fn main() -> Result<()> {
let extractions = extractor::extract_from_selections(&raw_selection, config.best_match());

if extractions.is_err() {
return main();
return init(fetcher);
}

actor::act(extractions, files, variables)?;

Ok(())
}

pub fn get_fetcher() -> Result<Box<dyn Fetcher>> {
match CONFIG.source() {
Source::Cheats(query) => {
let lines = cheatsh::call(&query)?;
let fetcher = Box::new(StaticFetcher::new(lines));
Ok(fetcher)
}
Source::Tldr(query) => {
let lines = cheatsh::call(&query)?;
let fetcher = Box::new(StaticFetcher::new(lines));
Ok(fetcher)
}
Source::Filesystem(path) => {
let fetcher = Box::new(filesystem::Fetcher::new(path));
Ok(fetcher)
}
Source::Welcome => {
let fetcher = Box::new(welcome::Fetcher::new());
Ok(fetcher)
}
}
}

pub fn main() -> Result<()> {
let fetcher = get_fetcher()?;
init(fetcher)
}
5 changes: 3 additions & 2 deletions src/commands/temp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::commands::core::get_fetcher;
use crate::common::shell::{self, ShellSpawnError};
use crate::finder::structures::Opts as FinderOpts;
use crate::parser::Parser;
Expand All @@ -8,7 +9,7 @@ pub fn main() -> Result<()> {
let config = &CONFIG;
let _opts = FinderOpts::snippet_default();

let fetcher = config.fetcher();
let fetcher = get_fetcher()?;
let hash: u64 = 2087294461664323320;

let mut buf = vec![];
Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn main() -> Result<()> {
pub fn _main0() -> Result<()> {
let config = &CONFIG;

let fetcher = config.fetcher();
let fetcher = get_fetcher()?;

let mut stdout = io::stdout();
let mut writer: Box<&mut dyn Write> = Box::new(&mut stdout);
Expand Down
16 changes: 0 additions & 16 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ mod cli;
mod env;
mod yaml;

use crate::clients::cheatsh;
use crate::clients::tldr;

use crate::commands::func::Func;
use crate::config::Source;
use crate::filesystem;
use crate::finder::FinderChoice;
use crate::structures::fetcher::Fetcher;
use crate::welcome;
pub use cli::*;
use crossterm::style::Color;
use env::EnvConfig;
Expand Down Expand Up @@ -62,15 +55,6 @@ impl Config {
}
}

pub fn fetcher(&self) -> Box<dyn Fetcher> {
match self.source() {
Source::Cheats(query) => Box::new(cheatsh::Fetcher::new(query)),
Source::Tldr(query) => Box::new(tldr::Fetcher::new(query)),
Source::Filesystem(path) => Box::new(filesystem::Fetcher::new(path)),
Source::Welcome => Box::new(welcome::Fetcher::new()),
}
}

pub fn path(&self) -> Option<String> {
self.clap
.path
Expand Down
17 changes: 17 additions & 0 deletions src/structures/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,20 @@ pub trait Fetcher {
vec![]
}
}

pub struct StaticFetcher {
lines: Vec<String>,
}

impl StaticFetcher {
pub fn new(lines: Vec<String>) -> Self {
Self { lines }
}
}

impl Fetcher for StaticFetcher {
fn fetch(&self, parser: &mut Parser) -> Result<bool> {
parser.read_lines(self.lines.clone().into_iter().map(Ok), "static", None)?;
Ok(true)
}
}
7 changes: 2 additions & 5 deletions src/welcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use crate::structures::fetcher;

pub fn populate_cheatsheet(parser: &mut Parser) -> Result<()> {
let cheatsheet = include_str!("../docs/navi.cheat");
let lines = cheatsheet.split('\n').into_iter().map(|s| Ok(s.to_string()));

parser.read_lines(
cheatsheet.split('\n').into_iter().map(|s| Ok(s.to_string())),
"welcome",
None,
)?;
parser.read_lines(lines, "welcome", None)?;

Ok(())
}
Expand Down