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

View file

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

View file

@ -1,10 +1,9 @@
use crate::handler; use crate::handler;
use crate::shell::BashSpawnError; use crate::shell::{self, ShellSpawnError};
use crate::structures::config; use crate::structures::config;
use crate::url; use crate::url;
use anyhow::Error; use anyhow::Error;
use std::io::{self, Read}; use std::io::{self, Read};
use std::process::Command;
#[derive(Debug)] #[derive(Debug)]
pub enum Func { pub enum Func {
@ -27,11 +26,11 @@ pub fn main(func: &Func, args: Vec<String>) -> Result<(), Error> {
fn map_expand() -> Result<(), Error> { fn map_expand() -> Result<(), Error> {
let cmd = r#"sed -e 's/^.*$/"&"/' | tr '\n' ' '"#; let cmd = r#"sed -e 's/^.*$/"&"/' | tr '\n' ' '"#;
Command::new("bash") shell::command()
.arg("-c") .arg("-c")
.arg(cmd) .arg(cmd)
.spawn() .spawn()
.map_err(|e| BashSpawnError::new(cmd, e))? .map_err(|e| ShellSpawnError::new(cmd, e))?
.wait()?; .wait()?;
Ok(()) 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 FZF_OVERRIDES_VAR: &str = "NAVI_FZF_OVERRIDES_VAR";
pub const FINDER: &str = "NAVI_FINDER"; pub const FINDER: &str = "NAVI_FINDER";
pub const SHELL: &str = "NAVI_SHELL";
pub fn parse<T: FromStr>(varname: &str) -> Option<T> { pub fn parse<T: FromStr>(varname: &str) -> Option<T> {
if let Ok(x) = env::var(varname) { if let Ok(x) = env::var(varname) {
x.parse::<T>().ok() x.parse::<T>().ok()

View file

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

View file

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

View file

@ -1,11 +1,13 @@
use crate::env_var; use crate::env_var;
use std::fmt::Debug; use std::fmt::Debug;
use std::process::Command;
use thiserror::Error; use thiserror::Error;
lazy_static! { lazy_static! {
pub static ref IS_FISH: bool = env_var::get("SHELL") pub static ref IS_FISH: bool = env_var::get("SHELL")
.unwrap_or_else(|_| "".to_string()) .unwrap_or_else(|_| "".to_string())
.contains(&"fish"); .contains(&"fish");
static ref SHELL: String = env_var::get(env_var::SHELL).unwrap_or_else(|_| "bash".to_string());
} }
#[derive(Debug)] #[derive(Debug)]
@ -17,20 +19,24 @@ pub enum Shell {
#[derive(Error, Debug)] #[derive(Error, Debug)]
#[error("Failed to spawn child process `bash` to execute `{command}`")] #[error("Failed to spawn child process `bash` to execute `{command}`")]
pub struct BashSpawnError { pub struct ShellSpawnError {
command: String, command: String,
#[source] #[source]
source: anyhow::Error, source: anyhow::Error,
} }
impl BashSpawnError { impl ShellSpawnError {
pub fn new<SourceError>(command: impl Into<String>, source: SourceError) -> Self pub fn new<SourceError>(command: impl Into<String>, source: SourceError) -> Self
where where
SourceError: std::error::Error + Sync + Send + 'static, SourceError: std::error::Error + Sync + Send + 'static,
{ {
BashSpawnError { ShellSpawnError {
command: command.into(), command: command.into(),
source: source.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 anyhow::Error;
use std::process::Command;
pub fn open(args: Vec<String>) -> Result<(), Error> { pub fn open(args: Vec<String>) -> Result<(), Error> {
let url = args let url = args
@ -32,11 +31,11 @@ NAVIEOF
_open_url "$url""#, _open_url "$url""#,
code, url code, url
); );
Command::new("bash") shell::command()
.arg("-c") .arg("-c")
.arg(cmd.as_str()) .arg(cmd.as_str())
.spawn() .spawn()
.map_err(|e| BashSpawnError::new(cmd, e))? .map_err(|e| ShellSpawnError::new(cmd, e))?
.wait()?; .wait()?;
Ok(()) Ok(())
} }