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

Allow configuration of shell used for shell out #511

Merged
merged 2 commits into from
Apr 15, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/actor.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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(())
}