Skip to content

Commit

Permalink
Avoid panic for unknown shell (#1241)
Browse files Browse the repository at this point in the history
Downgrade this from a panic to a command error which will cause the CLI
to exit non-zero.
  • Loading branch information
brynary authored Dec 1, 2024
1 parent 9435037 commit 0be02fb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
26 changes: 16 additions & 10 deletions qlty-cli/src/commands/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ pub struct Completions {
impl Completions {
pub fn execute(&self, _args: &Arguments) -> Result<CommandSuccess, CommandError> {
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 {
Expand Down
1 change: 0 additions & 1 deletion qlty-cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::CommandSuccess;

#[derive(Error, Debug)]
pub enum CommandError {
// TODO: Start using CommandError::Config
#[error("Config error")]
Config,

Expand Down
14 changes: 9 additions & 5 deletions qlty-cli/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod zsh;

pub use bash::Bash;
pub use fish::Fish;
use tracing::warn;
pub use zsh::Zsh;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -56,13 +57,16 @@ pub trait TabCompletingShell {
fn clap_shell(&self) -> Shell;
}

pub fn build(specified: Option<Shell>) -> Box<dyn TabCompletingShell> {
pub fn build(specified: Option<Shell>) -> Option<Box<dyn TabCompletingShell>> {
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
}
}
}

0 comments on commit 0be02fb

Please sign in to comment.