Skip to content

Commit

Permalink
Since other people are actually on zsh!
Browse files Browse the repository at this point in the history
Signed-off-by: itowlson <[email protected]>
  • Loading branch information
itowlson committed Oct 3, 2023
1 parent cd8d561 commit 03779c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/bin/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use spin_cli::commands::{
cloud::{DeployCommand, LoginCommand},
doctor::DoctorCommand,
external::execute_external_subcommand,
generate_completions::GenerateCompletionsCommand,
new::{AddCommand, NewCommand},
plugins::PluginCommands,
registry::RegistryCommands,
Expand Down Expand Up @@ -129,8 +130,8 @@ enum SpinApp {
Plugins(PluginCommands),
#[clap(subcommand, hide = true)]
Trigger(TriggerCommands),
#[clap(hide = true, name = "generate-bash-completions")]
GenerateBashCompletions,
#[clap(hide = true, name = "generate-completions")]
GenerateCompletions(GenerateCompletionsCommand),
#[clap(external_subcommand)]
External(Vec<String>),
Watch(WatchCommand),
Expand Down Expand Up @@ -164,11 +165,7 @@ impl SpinApp {
Self::External(cmd) => execute_external_subcommand(cmd, app).await,
Self::Watch(cmd) => cmd.run().await,
Self::Doctor(cmd) => cmd.run().await,
Self::GenerateBashCompletions => {
let mut cmd: clap::Command = SpinApp::into_app();
print_completions(clap_complete::Shell::Bash, &mut cmd);
Ok(())
}
Self::GenerateCompletions(cmd) => cmd.run(SpinApp::into_app()).await,
}
}
}
Expand Down Expand Up @@ -227,7 +224,3 @@ fn installed_plugin_help_entries() -> Vec<PluginHelpEntry> {
fn hide_plugin_in_help(plugin: &spin_plugins::manifest::PluginManifest) -> bool {
plugin.name().starts_with("trigger-")
}

fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout())
}
2 changes: 2 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod cloud;
pub mod doctor;
/// Commands for external subcommands (i.e. plugins)
pub mod external;
/// Command to generate shell completions
pub mod generate_completions;
/// Command for creating a new application.
pub mod new;
/// Command for adding a plugin to Spin
Expand Down
22 changes: 22 additions & 0 deletions src/commands/generate_completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use anyhow::Result;
use clap::Parser;

/// Generate shell completions.
#[derive(Parser, Debug)]
#[clap(about = "Generate completions")]
pub struct GenerateCompletionsCommand {
#[clap(value_parser = clap::value_parser!(clap_complete::Shell))]
pub shell: clap_complete::Shell,
}

impl GenerateCompletionsCommand {
pub async fn run(&self, mut cmd: clap::Command<'_>) -> Result<()> {
// let mut cmd: clap::Command = SpinApp::into_app();
print_completions(self.shell, &mut cmd);
Ok(())
}
}

fn print_completions<G: clap_complete::Generator>(gen: G, cmd: &mut clap::Command) {
clap_complete::generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout())
}

0 comments on commit 03779c7

Please sign in to comment.