Skip to content

Commit

Permalink
feat: add basic completion
Browse files Browse the repository at this point in the history
  • Loading branch information
emouty authored and ctron committed Jan 24, 2025
1 parent 4dba63d commit f870b08
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
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
28 changes: 28 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ mod inspect;
mod list;
mod token;

use crate::Cli;
use clap::CommandFactory;
use clap_complete::{
generate,
shells::{Bash, Fish, Zsh},
};
use std::path::Path;
use std::process::ExitCode;
use std::{env, io};

#[derive(Debug, clap::Subcommand)]
pub enum Command {
Expand All @@ -13,6 +21,7 @@ pub enum Command {
Token(token::GetToken),
List(list::List),
Inspect(inspect::Inspect),
Completion { shell: String },
}

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

0 comments on commit f870b08

Please sign in to comment.