Allow configuration of shell used for shell out (#511)

This commit is contained in:
Denis Isidoro 2021-04-15 13:56:01 -03:00 committed by GitHub
parent 5c23e25fac
commit 42ef14c06a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 31 deletions

View file

@ -1,21 +1,19 @@
use crate::clipboard;
use crate::env_var;
use crate::extractor;
use crate::writer;
use crate::finder::structures::{Opts as FinderOpts, SuggestionType};
use crate::finder::Finder;
use crate::shell::{BashSpawnError, IS_FISH};
use crate::shell;
use crate::shell::{ShellSpawnError, IS_FISH};
use crate::structures::cheat::{Suggestion, VariableMap};
use crate::structures::config::Action;
use crate::structures::config::Config;
use crate::writer;
use anyhow::Context;
use anyhow::Error;
use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};
use std::process::Stdio;
fn prompt_finder(
variable_name: &str,
@ -47,12 +45,12 @@ fn prompt_finder(
}
}
let child = Command::new("bash")
let child = shell::command()
.stdout(Stdio::piped())
.arg("-c")
.arg(&suggestion_command)
.spawn()
.map_err(|e| BashSpawnError::new(suggestion_command, e))?;
.map_err(|e| ShellSpawnError::new(suggestion_command, e))?;
let text = String::from_utf8(
child
@ -211,11 +209,11 @@ pub fn act(
clipboard::copy(interpolated_snippet)?;
}
_ => {
Command::new("bash")
shell::command()
.arg("-c")
.arg(&interpolated_snippet[..])
.spawn()
.map_err(|e| BashSpawnError::new(&interpolated_snippet[..], e))?
.map_err(|e| ShellSpawnError::new(&interpolated_snippet[..], e))?
.wait()
.context("bash was not running")?;
}

View file

@ -1,6 +1,5 @@
use crate::shell::BashSpawnError;
use crate::shell::{self, ShellSpawnError};
use anyhow::Error;
use std::process::Command;
pub fn copy(text: String) -> Result<(), Error> {
let cmd = r#"
@ -20,7 +19,7 @@ _copy() {
fi
}"#;
Command::new("bash")
shell::command()
.arg("-c")
.arg(
format!(
@ -35,7 +34,7 @@ echo -n "$x" | _copy"#,
.as_str(),
)
.spawn()
.map_err(|e| BashSpawnError::new(cmd, e))?
.map_err(|e| ShellSpawnError::new(cmd, e))?
.wait()?;
Ok(())

View file

@ -1,10 +1,9 @@
use crate::handler;
use crate::shell::BashSpawnError;
use crate::shell::{self, ShellSpawnError};
use crate::structures::config;
use crate::url;
use anyhow::Error;
use std::io::{self, Read};
use std::process::Command;
#[derive(Debug)]
pub enum Func {
@ -27,11 +26,11 @@ pub fn main(func: &Func, args: Vec<String>) -> Result<(), Error> {
fn map_expand() -> Result<(), Error> {
let cmd = r#"sed -e 's/^.*$/"&"/' | tr '\n' ' '"#;
Command::new("bash")
shell::command()
.arg("-c")
.arg(cmd)
.spawn()
.map_err(|e| BashSpawnError::new(cmd, e))?
.map_err(|e| ShellSpawnError::new(cmd, e))?
.wait()?;
Ok(())
}

View file

@ -23,6 +23,8 @@ pub const FZF_OVERRIDES: &str = "NAVI_FZF_OVERRIDES";
pub const FZF_OVERRIDES_VAR: &str = "NAVI_FZF_OVERRIDES_VAR";
pub const FINDER: &str = "NAVI_FINDER";
pub const SHELL: &str = "NAVI_SHELL";
pub fn parse<T: FromStr>(varname: &str) -> Option<T> {
if let Ok(x) = env::var(varname) {
x.parse::<T>().ok()

View file

@ -1,9 +1,8 @@
use crate::finder::structures::SuggestionType;
use crate::shell;
use anyhow::Context;
use anyhow::Error;
use std::process::{Command, Stdio};
use std::process::Stdio;
fn apply_map(text: String, map_fn: Option<String>) -> Result<String, Error> {
if let Some(m) = map_fn {
@ -21,7 +20,7 @@ echo "$_navi_input" | _navi_map_fn"#,
m, text
);
let output = Command::new("bash")
let output = shell::command()
.arg("-c")
.arg(cmd.as_str())
.stderr(Stdio::inherit())

View file

@ -1,4 +1,4 @@
use crate::shell::BashSpawnError;
use crate::shell::ShellSpawnError;
use anyhow::{Context, Error};
use std::process::Command;
@ -6,7 +6,7 @@ pub fn shallow_clone(uri: &str, target: &str) -> Result<(), Error> {
Command::new("git")
.args(&["clone", uri, target, "--depth", "1"])
.spawn()
.map_err(|e| BashSpawnError::new("git clone", e))?
.map_err(|e| ShellSpawnError::new("git clone", e))?
.wait()
.context("Unable to git clone")?;
Ok(())

View file

@ -1,11 +1,13 @@
use crate::env_var;
use std::fmt::Debug;
use std::process::Command;
use thiserror::Error;
lazy_static! {
pub static ref IS_FISH: bool = env_var::get("SHELL")
.unwrap_or_else(|_| "".to_string())
.contains(&"fish");
static ref SHELL: String = env_var::get(env_var::SHELL).unwrap_or_else(|_| "bash".to_string());
}
#[derive(Debug)]
@ -17,20 +19,24 @@ pub enum Shell {
#[derive(Error, Debug)]
#[error("Failed to spawn child process `bash` to execute `{command}`")]
pub struct BashSpawnError {
pub struct ShellSpawnError {
command: String,
#[source]
source: anyhow::Error,
}
impl BashSpawnError {
impl ShellSpawnError {
pub fn new<SourceError>(command: impl Into<String>, source: SourceError) -> Self
where
SourceError: std::error::Error + Sync + Send + 'static,
{
BashSpawnError {
ShellSpawnError {
command: command.into(),
source: source.into(),
}
}
}
pub fn command() -> Command {
Command::new(&*SHELL)
}

View file

@ -1,6 +1,5 @@
use crate::shell::BashSpawnError;
use crate::shell::{self, ShellSpawnError};
use anyhow::Error;
use std::process::Command;
pub fn open(args: Vec<String>) -> Result<(), Error> {
let url = args
@ -32,11 +31,11 @@ NAVIEOF
_open_url "$url""#,
code, url
);
Command::new("bash")
shell::command()
.arg("-c")
.arg(cmd.as_str())
.spawn()
.map_err(|e| BashSpawnError::new(cmd, e))?
.map_err(|e| ShellSpawnError::new(cmd, e))?
.wait()?;
Ok(())
}