Browse Source

Throw an error instead of panicking when can't infer shell (#306)

remotes/origin/add-with-shims
Gal Schlezinger 4 years ago committed by GitHub
parent
commit
d93bb4219f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      src/commands/completions.rs
  2. 24
      src/commands/env.rs
  3. 11
      src/shell/mod.rs

29
src/commands/completions.rs

@ -1,7 +1,8 @@
use super::command::Command; use super::command::Command;
use crate::cli::Cli; use crate::cli::Cli;
use crate::config::FnmConfig; use crate::config::FnmConfig;
use crate::shell::infer_shell; use crate::shell::{infer_shell, AVAILABLE_SHELLS};
use snafu::{OptionExt, Snafu};
use structopt::clap::Shell; use structopt::clap::Shell;
use structopt::StructOpt; use structopt::StructOpt;
@ -17,11 +18,31 @@ impl Command for Completions {
fn apply(self, _config: &FnmConfig) -> Result<(), Self::Error> { fn apply(self, _config: &FnmConfig) -> Result<(), Self::Error> {
let mut stdio = std::io::stdout(); let mut stdio = std::io::stdout();
let shell = self.shell.unwrap_or_else(|| infer_shell().into()); let shell = self
.shell
.or_else(|| infer_shell().map(Into::into))
.context(CantInferShell)?;
Cli::clap().gen_completions_to(env!("CARGO_PKG_NAME"), shell, &mut stdio); Cli::clap().gen_completions_to(env!("CARGO_PKG_NAME"), shell, &mut stdio);
Ok(()) Ok(())
} }
} }
#[derive(snafu::Snafu, Debug)] #[derive(Snafu, Debug)]
pub enum Error {} pub enum Error {
#[snafu(display(
"{}\n{}\n{}\n{}",
"Can't infer shell!",
"fnm can't infer your shell based on the process tree.",
"Maybe it is unsupported? we support the following shells:",
shells_as_string()
))]
CantInferShell,
}
fn shells_as_string() -> String {
AVAILABLE_SHELLS
.iter()
.map(|x| format!("* {}", x))
.collect::<Vec<_>>()
.join("\n")
}

24
src/commands/env.rs

@ -4,6 +4,7 @@ use crate::fs::symlink_dir;
use crate::outln; use crate::outln;
use crate::shell::{infer_shell, Shell, AVAILABLE_SHELLS}; use crate::shell::{infer_shell, Shell, AVAILABLE_SHELLS};
use colored::Colorize; use colored::Colorize;
use snafu::{OptionExt, Snafu};
use std::fmt::Debug; use std::fmt::Debug;
use structopt::StructOpt; use structopt::StructOpt;
@ -50,7 +51,7 @@ impl Command for Env {
outln!(config#Error, "{} {} is deprecated. This is now the default.", "warning:".yellow().bold(), "--multi".italic()); outln!(config#Error, "{} {} is deprecated. This is now the default.", "warning:".yellow().bold(), "--multi".italic());
} }
let shell: Box<dyn Shell> = self.shell.unwrap_or_else(&infer_shell); let shell: Box<dyn Shell> = self.shell.or_else(&infer_shell).context(CantInferShell)?;
let multishell_path = make_symlink(&config); let multishell_path = make_symlink(&config);
let binary_path = if cfg!(windows) { let binary_path = if cfg!(windows) {
multishell_path.clone() multishell_path.clone()
@ -81,8 +82,25 @@ impl Command for Env {
} }
} }
#[derive(Debug, snafu::Snafu)] #[derive(Debug, Snafu)]
pub enum Error {} pub enum Error {
#[snafu(display(
"{}\n{}\n{}\n{}",
"Can't infer shell!",
"fnm can't infer your shell based on the process tree.",
"Maybe it is unsupported? we support the following shells:",
shells_as_string()
))]
CantInferShell,
}
fn shells_as_string() -> String {
AVAILABLE_SHELLS
.iter()
.map(|x| format!("* {}", x))
.collect::<Vec<_>>()
.join("\n")
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

11
src/shell/mod.rs

@ -15,15 +15,12 @@ pub use shell::{Shell, AVAILABLE_SHELLS};
pub use windows_cmd::WindowsCmd; pub use windows_cmd::WindowsCmd;
pub use zsh::Zsh; pub use zsh::Zsh;
/// Always returns WindowsCmd (because this is what we support on Windows)
#[cfg(windows)] #[cfg(windows)]
pub fn infer_shell() -> Box<dyn Shell> { pub fn infer_shell() -> Option<Box<dyn Shell>> {
let inferred = self::infer::windows::infer_shell(); self::infer::windows::infer_shell()
inferred.expect("Can't infer shell")
} }
/// Tries to infer shell or dies trying
#[cfg(unix)] #[cfg(unix)]
pub fn infer_shell() -> Box<dyn Shell> { pub fn infer_shell() -> Option<Box<dyn Shell>> {
infer::unix::infer_shell().expect("Can't infer shell") infer::unix::infer_shell()
} }

Loading…
Cancel
Save