From 0be02fb29c5a9f616caf775ab9023ee84ecdab71 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp <19+brynary@users.noreply.github.com> Date: Sun, 1 Dec 2024 12:20:05 -0500 Subject: [PATCH] Avoid panic for unknown shell (#1241) Downgrade this from a panic to a command error which will cause the CLI to exit non-zero. --- qlty-cli/src/commands/completions.rs | 26 ++++++++++++++++---------- qlty-cli/src/errors.rs | 1 - qlty-cli/src/shell.rs | 14 +++++++++----- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/qlty-cli/src/commands/completions.rs b/qlty-cli/src/commands/completions.rs index 4402cf11c..5986afa35 100644 --- a/qlty-cli/src/commands/completions.rs +++ b/qlty-cli/src/commands/completions.rs @@ -17,20 +17,26 @@ pub struct Completions { impl Completions { pub fn execute(&self, _args: &Arguments) -> Result { let bin_name = self.bin_name(); - let mut shell = crate::shell::build(self.shell); - let mut command = Arguments::command(); - let completions = shell.generate(&bin_name, &mut command)?; + if let Some(mut shell) = crate::shell::build(self.shell) { + let mut command = Arguments::command(); + let completions = shell.generate(&bin_name, &mut command)?; - if self.install { - let path = shell.install(&completions)?; - shell.update_rc(&path)?; - eprintln!("Completions installed in {}", path.display()); + if self.install { + let path = shell.install(&completions)?; + shell.update_rc(&path)?; + eprintln!("Completions installed in {}", path.display()); + } else { + println!("{}", completions.completions); + } + + CommandSuccess::ok() } else { - println!("{}", completions.completions); + eprintln!("Unsupported shell: {:?}", self.shell); + Err(CommandError::InvalidOptions { + message: "Unsupported shell".to_owned(), + }) } - - CommandSuccess::ok() } fn bin_name(&self) -> String { diff --git a/qlty-cli/src/errors.rs b/qlty-cli/src/errors.rs index 1c0dc3bcf..dca499f70 100644 --- a/qlty-cli/src/errors.rs +++ b/qlty-cli/src/errors.rs @@ -4,7 +4,6 @@ use crate::CommandSuccess; #[derive(Error, Debug)] pub enum CommandError { - // TODO: Start using CommandError::Config #[error("Config error")] Config, diff --git a/qlty-cli/src/shell.rs b/qlty-cli/src/shell.rs index 92fad35b2..45663dda7 100644 --- a/qlty-cli/src/shell.rs +++ b/qlty-cli/src/shell.rs @@ -13,6 +13,7 @@ mod zsh; pub use bash::Bash; pub use fish::Fish; +use tracing::warn; pub use zsh::Zsh; #[derive(Clone, Debug)] @@ -56,13 +57,16 @@ pub trait TabCompletingShell { fn clap_shell(&self) -> Shell; } -pub fn build(specified: Option) -> Box { +pub fn build(specified: Option) -> Option> { let shell = specified.unwrap_or_else(|| Shell::from_env().unwrap_or(Shell::Bash)); match shell { - Shell::Bash => Box::new(Bash), - Shell::Fish => Box::new(Fish), - Shell::Zsh => Box::new(Zsh), - _ => panic!("Unsupported shell: {}", shell), + Shell::Bash => Some(Box::new(Bash)), + Shell::Fish => Some(Box::new(Fish)), + Shell::Zsh => Some(Box::new(Zsh)), + _ => { + warn!("Unsupported shell: {:?}", shell); + None + } } }