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

support shell completion #5

Merged
merged 2 commits into from
Jan 24, 2025
Merged
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
13 changes: 10 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pkg-fmt = "bin"
actix-web = { version = "4", features = ["openssl"] }
anyhow = "1"
biscuit = "0.7"
chrono = "0.4"
clap = { version = "4", features = ["derive", "env"] }
clap_complete = "4"
colored_json = "5"
comfy-table = "7"
directories = "5"
Expand Down
37 changes: 37 additions & 0 deletions src/cmd/completion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::Cli;
use clap::CommandFactory;
use clap_complete::generate;
use clap_complete::Shell::{Bash, Fish, Zsh};
use std::path::Path;
use std::{env, io};

/// Generate shell completion
#[derive(Debug, clap::Parser)]
#[command(rename_all_env = "SNAKE_CASE")]
pub struct GetCompletion {
/// The shell to generate completions for. Supported values are bash, zsh or fish
pub shell: String,
}

impl GetCompletion {
pub async fn run(self) -> anyhow::Result<()> {
let shell = self.shell;
let mut cmd = Cli::command();
let bin_name = env::args()
.next()
.and_then(|path| {
Path::new(&path)
.file_stem()
.map(|name| name.to_string_lossy().into_owned())
})
.unwrap();

match shell.as_str() {
"bash" => generate(Bash, &mut cmd, &bin_name, &mut io::stdout()),
"zsh" => generate(Zsh, &mut cmd, &bin_name, &mut io::stdout()),
"fish" => generate(Fish, &mut cmd, &bin_name, &mut io::stdout()),
_ => eprintln!("Unsupported shell: {}", shell),
}
Ok(())
}
}
3 changes: 3 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod completion;
mod create;
mod delete;
mod inspect;
Expand All @@ -13,6 +14,7 @@ pub enum Command {
Token(token::GetToken),
List(list::List),
Inspect(inspect::Inspect),
Completion(completion::GetCompletion),
}

impl Command {
Expand All @@ -23,6 +25,7 @@ impl Command {
Self::Token(cmd) => cmd.run().await,
Self::List(cmd) => cmd.run().await,
Self::Inspect(cmd) => cmd.run().await,
Self::Completion(cmd) => cmd.run().await,
}
.map(|()| ExitCode::SUCCESS)
}
Expand Down
Loading