Skip to content

Commit 3dd3ec3

Browse files
committed
add basic completion
1 parent 4dba63d commit 3dd3ec3

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Cargo.lock

+10-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ pkg-fmt = "bin"
2525
actix-web = { version = "4", features = ["openssl"] }
2626
anyhow = "1"
2727
biscuit = "0.7"
28-
chrono = "0.4"
2928
clap = { version = "4", features = ["derive", "env"] }
29+
clap_complete = "4"
3030
colored_json = "5"
3131
comfy-table = "7"
3232
directories = "5"

src/cmd/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ mod inspect;
44
mod list;
55
mod token;
66

7+
use crate::Cli;
8+
use clap::CommandFactory;
9+
use clap_complete::{
10+
generate,
11+
shells::{Bash, Fish, Zsh},
12+
};
13+
use std::path::Path;
714
use std::process::ExitCode;
15+
use std::{env, io};
816

917
#[derive(Debug, clap::Subcommand)]
1018
pub enum Command {
@@ -13,6 +21,7 @@ pub enum Command {
1321
Token(token::GetToken),
1422
List(list::List),
1523
Inspect(inspect::Inspect),
24+
Completion { shell: String },
1625
}
1726

1827
impl Command {
@@ -23,6 +32,25 @@ impl Command {
2332
Self::Token(cmd) => cmd.run().await,
2433
Self::List(cmd) => cmd.run().await,
2534
Self::Inspect(cmd) => cmd.run().await,
35+
Self::Completion { shell } => {
36+
let mut cmd = Cli::command();
37+
let bin_name = env::args()
38+
.next()
39+
.and_then(|path| {
40+
Path::new(&path)
41+
.file_stem()
42+
.map(|name| name.to_string_lossy().into_owned())
43+
})
44+
.unwrap();
45+
46+
match shell.as_str() {
47+
"bash" => generate(Bash, &mut cmd, &bin_name, &mut io::stdout()),
48+
"zsh" => generate(Zsh, &mut cmd, &bin_name, &mut io::stdout()),
49+
"fish" => generate(Fish, &mut cmd, &bin_name, &mut io::stdout()),
50+
_ => eprintln!("Unsupported shell: {}", shell),
51+
}
52+
Ok(())
53+
}
2654
}
2755
.map(|()| ExitCode::SUCCESS)
2856
}

0 commit comments

Comments
 (0)