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 @@ @@ -1,7 +1,8 @@
use super::command::Command;
use crate::cli::Cli;
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::StructOpt;
@ -17,11 +18,31 @@ impl Command for Completions { @@ -17,11 +18,31 @@ impl Command for Completions {
fn apply(self, _config: &FnmConfig) -> Result<(), Self::Error> {
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);
Ok(())
}
}
#[derive(snafu::Snafu, Debug)]
pub enum Error {}
#[derive(Snafu, Debug)]
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; @@ -4,6 +4,7 @@ use crate::fs::symlink_dir;
use crate::outln;
use crate::shell::{infer_shell, Shell, AVAILABLE_SHELLS};
use colored::Colorize;
use snafu::{OptionExt, Snafu};
use std::fmt::Debug;
use structopt::StructOpt;
@ -50,7 +51,7 @@ impl Command for Env { @@ -50,7 +51,7 @@ impl Command for Env {
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 binary_path = if cfg!(windows) {
multishell_path.clone()
@ -81,8 +82,25 @@ impl Command for Env { @@ -81,8 +82,25 @@ impl Command for Env {
}
}
#[derive(Debug, snafu::Snafu)]
pub enum Error {}
#[derive(Debug, Snafu)]
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)]
mod tests {

11
src/shell/mod.rs

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

Loading…
Cancel
Save