Skip to content

Commit 24fbef5

Browse files
committed
Add a completions subcommand group for bash, fish and zsh
1 parent 500ce1d commit 24fbef5

3 files changed

Lines changed: 33 additions & 17 deletions

File tree

diesel_cli/src/cli.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,18 @@ pub fn build_cli() -> App<'static, 'static> {
9090
)
9191
.setting(AppSettings::SubcommandRequiredElseHelp);
9292

93-
let generate_bash_completion_subcommand = SubCommand::with_name("bash-completion")
94-
.about("Generate bash completion script for the diesel command.");
95-
96-
let generate_zsh_completion_subcommand = SubCommand::with_name("zsh-completion")
97-
.about("Generate zsh completion script for the diesel command.");
93+
let generate_completions_subcommand = SubCommand::with_name("completions")
94+
.about("Generate shell completion scripts for the diesel command.")
95+
.subcommand(SubCommand::with_name("bash").about(
96+
"Generate bash completions."
97+
))
98+
.subcommand(SubCommand::with_name("fish").about(
99+
"Generate fish completions."
100+
))
101+
.subcommand(SubCommand::with_name("zsh").about(
102+
"Generate zsh completions."
103+
))
104+
.setting(AppSettings::SubcommandRequiredElseHelp);
98105

99106
let infer_schema_subcommand = SubCommand::with_name("print-schema")
100107
.setting(AppSettings::VersionlessSubcommands)
@@ -187,8 +194,7 @@ pub fn build_cli() -> App<'static, 'static> {
187194
.subcommand(migration_subcommand)
188195
.subcommand(setup_subcommand)
189196
.subcommand(database_subcommand)
190-
.subcommand(generate_bash_completion_subcommand)
191-
.subcommand(generate_zsh_completion_subcommand)
197+
.subcommand(generate_completions_subcommand)
192198
.subcommand(infer_schema_subcommand)
193199
.setting(AppSettings::SubcommandRequiredElseHelp)
194200
}

diesel_cli/src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ fn main() {
6161
("migration", Some(matches)) => run_migration_command(matches).unwrap_or_else(handle_error),
6262
("setup", Some(matches)) => run_setup_command(matches),
6363
("database", Some(matches)) => run_database_command(matches).unwrap_or_else(handle_error),
64-
("bash-completion", Some(matches)) => generate_bash_completion_command(matches),
65-
("zsh-completion", Some(matches)) => generate_zsh_completion_command(matches),
64+
("completions", Some(matches)) => generate_completions_command(matches),
6665
("print-schema", Some(matches)) => run_infer_schema(matches).unwrap_or_else(handle_error),
6766
_ => unreachable!("The cli parser should prevent reaching here"),
6867
}
@@ -245,12 +244,14 @@ fn run_database_command(matches: &ArgMatches) -> Result<(), Box<Error>> {
245244
Ok(())
246245
}
247246

248-
fn generate_bash_completion_command(_: &ArgMatches) {
249-
cli::build_cli().gen_completions_to("diesel", Shell::Bash, &mut stdout());
250-
}
251-
252-
fn generate_zsh_completion_command(_: &ArgMatches) {
253-
cli::build_cli().gen_completions_to("diesel", Shell::Zsh, &mut stdout());
247+
fn generate_completions_command(matches: &ArgMatches) {
248+
let shell = match matches.subcommand() {
249+
("bash", _) => Shell::Bash,
250+
("fish", _) => Shell::Fish,
251+
("zsh", _) => Shell::Zsh,
252+
_ => unreachable!("The cli parser should prevent reaching here"),
253+
};
254+
cli::build_cli().gen_completions_to("diesel", shell, &mut stdout());
254255
}
255256

256257
/// Looks for a migrations directory in the current path and all parent paths,

diesel_cli/tests/completion_generation.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ use support::project;
44
fn can_generate_bash_completion() {
55
let p = project("migration_redo").build();
66

7-
let result = p.command("bash-completion").run();
7+
let result = p.command("completions").arg("bash").run();
8+
9+
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
10+
}
11+
12+
#[test]
13+
fn can_generate_fish_completion() {
14+
let p = project("migration_redo").build();
15+
16+
let result = p.command("completions").arg("fish").run();
817

918
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
1019
}
@@ -13,7 +22,7 @@ fn can_generate_bash_completion() {
1322
fn can_generate_zsh_completion() {
1423
let p = project("migration_redo").build();
1524

16-
let result = p.command("zsh-completion").run();
25+
let result = p.command("completions").arg("zsh").run();
1726

1827
assert!(result.is_success(), "Result was unsuccessful {:?}", result);
1928
}

0 commit comments

Comments
 (0)