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

Bash completion #372

Closed
wants to merge 1 commit into from
Closed
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
56 changes: 40 additions & 16 deletions diesel_cli/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 diesel_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ name = "diesel"

[dependencies]
chrono = "0.2.17"
clap = "1.5.5"
clap = "2.11"
diesel = { version = "0.7.0", default-features = false }
dotenv = "0.8.0"

Expand Down
20 changes: 20 additions & 0 deletions diesel_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,23 @@ Runs the `down.sql` and then the `up.sql` for the most recent migration.

[pending-migrations]: http://docs.diesel.rs/diesel/migrations/fn.run_pending_migrations.html
[rust-dotenv]: https://github.com/slapresta/rust-dotenv#examples


Bash completion
---------------

Diesel can generate a bash completion script for itself:

#### linux

```shell
$ diesel bash-completion > /etc/bash_completion.d/diesel
```


#### os x (homebrew)

```shell
$ brew install bash-completion # you may already have this installed
$ diesel bash-completion > $(brew --prefix)/etc/bash_completion.d/diesel
```
86 changes: 86 additions & 0 deletions diesel_cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use clap::{App, AppSettings, Arg, SubCommand};


pub fn build_cli() -> App<'static, 'static> {
let database_arg = Arg::with_name("DATABASE_URL")
.long("database-url")
.help("Specifies the database URL to connect to. Falls back to \
the DATABASE_URL environment variable if unspecified.")
.global(true)
.takes_value(true);

let migration_subcommand = SubCommand::with_name("migration")
.about("A group of commands for generating, running, and reverting \
migrations.")
.setting(AppSettings::VersionlessSubcommands)
.arg(Arg::with_name("MIGRATION_DIRECTORY")
.long("migration-dir")
.help("The location of your migration directory. By default this \
will look for a directory called `migrations` in the \
current directory and its parents.")
.takes_value(true)
.global(true)
).subcommand(
SubCommand::with_name("run")
.about("Runs all pending migrations")
).subcommand(
SubCommand::with_name("revert")
.about("Reverts the latest run migration")
).subcommand(
SubCommand::with_name("redo")
.about("Reverts and re-runs the latest migration. Useful \
for testing that a migration can in fact be reverted.")
).subcommand(
SubCommand::with_name("generate")
.about("Generate a new migration with the given name, and \
the current timestamp as the version")
.arg(Arg::with_name("MIGRATION_NAME")
.help("The name of the migration to create")
.required(true)
)
.arg(Arg::with_name("MIGRATION_VERSION")
.long("version")
.help("The version number to use when generating the migration. \
Defaults to the current timestamp, which should suffice \
for most use cases.")
.takes_value(true)
)
).setting(AppSettings::SubcommandRequiredElseHelp);

let setup_subcommand = SubCommand::with_name("setup")
.about("Creates the migrations directory, creates the database \
specified in your DATABASE_URL, and runs existing migrations.");

let database_subcommand = SubCommand::with_name("database")
.about("A group of commands for setting up and resetting your database.")
.setting(AppSettings::VersionlessSubcommands)
.subcommand(
SubCommand::with_name("setup")
.about("Creates the database specified in your DATABASE_URL, \
and then runs any existing migrations.")
).subcommand(
SubCommand::with_name("reset")
.about("Resets your database by dropping the database specified \
in your DATABASE_URL and then running `diesel database setup`.")
).subcommand(
SubCommand::with_name("drop")
.about("Drops the database specified in your DATABASE_URL.")
.setting(AppSettings::Hidden)
).setting(AppSettings::SubcommandRequiredElseHelp);

let generate_bash_completion_subcommand =
SubCommand::with_name("bash-completion")
.about("Generate bash completion script for the diesel command.")
;

App::new("diesel")
.version(env!("CARGO_PKG_VERSION"))
.setting(AppSettings::VersionlessSubcommands)
.after_help("You can also run `diesel SUBCOMMAND -h` to get more information about that subcommand.")
.arg(database_arg)
.subcommand(migration_subcommand)
.subcommand(setup_subcommand)
.subcommand(database_subcommand)
.subcommand(generate_bash_completion_subcommand)
.setting(AppSettings::SubcommandRequiredElseHelp)
}
85 changes: 8 additions & 77 deletions diesel_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ extern crate dotenv;
mod database_error;
#[macro_use]
mod database;
mod cli;

use chrono::*;
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
use clap::{ArgMatches,Shell};
#[cfg(feature = "postgres")]
use diesel::pg::PgConnection;
#[cfg(feature = "sqlite")]
Expand All @@ -27,87 +28,13 @@ fn main() {
use self::dotenv::dotenv;
dotenv().ok();

let database_arg = Arg::with_name("DATABASE_URL")
.long("database-url")
.help("Specifies the database URL to connect to. Falls back to \
the DATABASE_URL environment variable if unspecified.")
.global(true)
.takes_value(true);

let migration_subcommand = SubCommand::with_name("migration")
.about("A group of commands for generating, running, and reverting \
migrations.")
.setting(AppSettings::VersionlessSubcommands)
.arg(Arg::with_name("MIGRATION_DIRECTORY")
.long("migration-dir")
.help("The location of your migration directory. By default this \
will look for a directory called `migrations` in the \
current directory and its parents.")
.takes_value(true)
.global(true)
).subcommand(
SubCommand::with_name("run")
.about("Runs all pending migrations")
).subcommand(
SubCommand::with_name("revert")
.about("Reverts the latest run migration")
).subcommand(
SubCommand::with_name("redo")
.about("Reverts and re-runs the latest migration. Useful \
for testing that a migration can in fact be reverted.")
).subcommand(
SubCommand::with_name("generate")
.about("Generate a new migration with the given name, and \
the current timestamp as the version")
.arg(Arg::with_name("MIGRATION_NAME")
.help("The name of the migration to create")
.required(true)
)
.arg(Arg::with_name("MIGRATION_VERSION")
.long("version")
.help("The version number to use when generating the migration. \
Defaults to the current timestamp, which should suffice \
for most use cases.")
.takes_value(true)
)
).setting(AppSettings::SubcommandRequiredElseHelp);

let setup_subcommand = SubCommand::with_name("setup")
.about("Creates the migrations directory, creates the database \
specified in your DATABASE_URL, and runs existing migrations.");

let database_subcommand = SubCommand::with_name("database")
.about("A group of commands for setting up and resetting your database.")
.setting(AppSettings::VersionlessSubcommands)
.subcommand(
SubCommand::with_name("setup")
.about("Creates the database specified in your DATABASE_URL, \
and then runs any existing migrations.")
).subcommand(
SubCommand::with_name("reset")
.about("Resets your database by dropping the database specified \
in your DATABASE_URL and then running `diesel database setup`.")
).subcommand(
SubCommand::with_name("drop")
.about("Drops the database specified in your DATABASE_URL.")
.setting(AppSettings::Hidden)
).setting(AppSettings::SubcommandRequiredElseHelp);

let matches = App::new("diesel")
.version(env!("CARGO_PKG_VERSION"))
.setting(AppSettings::VersionlessSubcommands)
.after_help("You can also run `diesel SUBCOMMAND -h` to get more information about that subcommand.")
.arg(database_arg)
.subcommand(migration_subcommand)
.subcommand(setup_subcommand)
.subcommand(database_subcommand)
.setting(AppSettings::SubcommandRequiredElseHelp)
.get_matches();
let matches = cli::build_cli().get_matches();

match matches.subcommand() {
("migration", Some(matches)) => run_migration_command(matches),
("setup", Some(matches)) => run_setup_command(matches),
("database", Some(matches)) => run_database_command(matches),
("bash-completion", Some(matches)) => generate_bash_completion_command(matches),
_ => unreachable!("The cli parser should prevent reaching here"),
}
}
Expand Down Expand Up @@ -187,6 +114,10 @@ fn run_database_command(matches: &ArgMatches) {
};
}

fn generate_bash_completion_command(_: &ArgMatches) {
cli::build_cli().gen_completions_to("diesel", Shell::Bash, &mut stdout());
}

/// Looks for a migrations directory in the current path and all parent paths,
/// and creates one in the same directory as the Cargo.toml if it can't find
/// one. It also sticks a .gitkeep in the directory so git will pick it up.
Expand Down
15 changes: 15 additions & 0 deletions diesel_cli/tests/completion_generation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use support::project;

#[test]
fn can_generate_bash_completion() {
let p = project("migration_redo")
.build();

let result = p.command("bash-completion").run();

let expected_last_line = "complete -F _diesel diesel";

assert!(result.is_success(), "Result was unsuccessful {:?}", result);
assert!(result.stdout().contains(expected_last_line),
"Unexpected stdout {}", result.stdout());
}
1 change: 1 addition & 0 deletions diesel_cli/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ mod database_drop;
mod database_setup;
mod database_reset;
mod exit_codes;
mod completion_generation;