Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shell completions #1032

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ bindle = { workspace = true }
bytes = "1.1"
chrono = "0.4"
clap = { version = "3.2.24", features = ["derive", "env"] }
clap_complete = "3.1.4"
clearscreen = "2.0.1"
command-group = "2.1"
comfy-table = "5.0"
Expand Down
4 changes: 4 additions & 0 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,6 +130,8 @@ enum SpinApp {
Plugins(PluginCommands),
#[clap(subcommand, hide = true)]
Trigger(TriggerCommands),
#[clap(hide = true, name = "generate-completions")]
GenerateCompletions(GenerateCompletionsCommand),
#[clap(external_subcommand)]
External(Vec<String>),
Watch(WatchCommand),
Expand Down Expand Up @@ -162,6 +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::GenerateCompletions(cmd) => cmd.run(SpinApp::into_app()).await,
}
}
}
Expand Down
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())
}