Skip to content

Commit 7934788

Browse files
committed
chore: move completion to dedicated module
1 parent 08d2fd3 commit 7934788

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

src/cmd/completion.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use crate::Cli;
2+
use clap::CommandFactory;
3+
use clap_complete::generate;
4+
use clap_complete::Shell::{Bash, Fish, Zsh};
5+
use std::path::Path;
6+
use std::{env, io};
7+
8+
/// Generate shell completion
9+
#[derive(Debug, clap::Parser)]
10+
#[command(rename_all_env = "SNAKE_CASE")]
11+
pub struct GetCompletion {
12+
/// The shell to generate completions for. Supported values are bash, zsh or fish
13+
pub shell: String,
14+
}
15+
16+
impl GetCompletion {
17+
pub async fn run(self) -> anyhow::Result<()> {
18+
let shell = self.shell;
19+
let mut cmd = Cli::command();
20+
let bin_name = env::args()
21+
.next()
22+
.and_then(|path| {
23+
Path::new(&path)
24+
.file_stem()
25+
.map(|name| name.to_string_lossy().into_owned())
26+
})
27+
.unwrap();
28+
29+
match shell.as_str() {
30+
"bash" => generate(Bash, &mut cmd, &bin_name, &mut io::stdout()),
31+
"zsh" => generate(Zsh, &mut cmd, &bin_name, &mut io::stdout()),
32+
"fish" => generate(Fish, &mut cmd, &bin_name, &mut io::stdout()),
33+
_ => eprintln!("Unsupported shell: {}", shell),
34+
}
35+
Ok(())
36+
}
37+
}

src/cmd/mod.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1+
mod completion;
12
mod create;
23
mod delete;
34
mod inspect;
45
mod list;
56
mod token;
67

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;
148
use std::process::ExitCode;
15-
use std::{env, io};
169

1710
#[derive(Debug, clap::Subcommand)]
1811
pub enum Command {
@@ -21,7 +14,7 @@ pub enum Command {
2114
Token(token::GetToken),
2215
List(list::List),
2316
Inspect(inspect::Inspect),
24-
Completion { shell: String },
17+
Completion(completion::GetCompletion),
2518
}
2619

2720
impl Command {
@@ -32,25 +25,7 @@ impl Command {
3225
Self::Token(cmd) => cmd.run().await,
3326
Self::List(cmd) => cmd.run().await,
3427
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-
}
28+
Self::Completion(cmd) => cmd.run().await,
5429
}
5530
.map(|()| ExitCode::SUCCESS)
5631
}

0 commit comments

Comments
 (0)