Skip to content

Commit

Permalink
Allow configuration of shell used for shell out (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro committed Apr 15, 2021
1 parent 5c23e25 commit 42ef14c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
18 changes: 8 additions & 10 deletions src/actor.rs
@@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")?;
}
Expand Down
7 changes: 3 additions & 4 deletions src/clipboard.rs
@@ -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#"
Expand All @@ -20,7 +19,7 @@ _copy() {
fi
}"#;

Command::new("bash")
shell::command()
.arg("-c")
.arg(
format!(
Expand All @@ -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(())
Expand Down
7 changes: 3 additions & 4 deletions src/cmds/func.rs
@@ -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 {
Expand All @@ -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(())
}
Expand Down
2 changes: 2 additions & 0 deletions src/env_var.rs
Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions src/finder/post.rs
@@ -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 {
Expand All @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions src/git.rs
@@ -1,12 +1,12 @@
use crate::shell::BashSpawnError;
use crate::shell::ShellSpawnError;
use anyhow::{Context, Error};
use std::process::Command;

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(())
Expand Down
12 changes: 9 additions & 3 deletions src/shell.rs
@@ -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)]
Expand All @@ -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)
}
7 changes: 3 additions & 4 deletions src/url.rs
@@ -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
Expand Down Expand Up @@ -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(())
}

0 comments on commit 42ef14c

Please sign in to comment.