-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix global cli argument handling #1304
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,32 +54,32 @@ fn main() { | |
|
||
fn run_migration_command(matches: &ArgMatches) { | ||
match matches.subcommand() { | ||
("run", Some(args)) => { | ||
("run", Some(_)) => { | ||
let database_url = database::database_url(matches); | ||
let dir = migrations_dir(args); | ||
let dir = migrations_dir(matches); | ||
call_with_conn!( | ||
database_url, | ||
migrations::run_pending_migrations_in_directory(&dir, &mut stdout()) | ||
).unwrap_or_else(handle_error); | ||
} | ||
("revert", Some(args)) => { | ||
("revert", Some(_)) => { | ||
let database_url = database::database_url(matches); | ||
let dir = migrations_dir(args); | ||
let dir = migrations_dir(matches); | ||
call_with_conn!( | ||
database_url, | ||
migrations::revert_latest_migration_in_directory(&dir) | ||
).unwrap_or_else(handle_error); | ||
} | ||
("redo", Some(args)) => { | ||
("redo", Some(_)) => { | ||
let database_url = database::database_url(matches); | ||
let dir = migrations_dir(args); | ||
let dir = migrations_dir(matches); | ||
call_with_conn!(database_url, redo_latest_migration(&dir)); | ||
} | ||
("list", Some(args)) => { | ||
("list", Some(_)) => { | ||
use std::ffi::OsStr; | ||
|
||
let database_url = database::database_url(matches); | ||
let dir = migrations_dir(args); | ||
let dir = migrations_dir(matches); | ||
let migrations = | ||
call_with_conn!(database_url, migrations::mark_migrations_in_directory(&dir)) | ||
.unwrap_or_else(handle_error); | ||
|
@@ -118,7 +118,7 @@ fn run_migration_command(matches: &ArgMatches) { | |
let migration_name = args.value_of("MIGRATION_NAME").unwrap(); | ||
let version = migration_version(args); | ||
let versioned_name = format!("{}_{}", version, migration_name); | ||
let migration_dir = migrations_dir(args).join(versioned_name); | ||
let migration_dir = migrations_dir(matches).join(versioned_name); | ||
fs::create_dir(&migration_dir).unwrap(); | ||
|
||
let migration_dir_relative = | ||
|
@@ -153,10 +153,20 @@ fn migration_version<'a>(matches: &'a ArgMatches) -> Box<Display + 'a> { | |
.unwrap_or_else(|| Box::new(Utc::now().format(TIMESTAMP_FORMAT))) | ||
} | ||
|
||
fn migrations_dir(matches: &ArgMatches) -> PathBuf { | ||
fn migrations_dir_from_cli(matches: &ArgMatches) -> Option<PathBuf> { | ||
matches | ||
.value_of("MIGRATION_DIRECTORY") | ||
.map(PathBuf::from) | ||
.or_else(|| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @weiznich - I'm still becoming familiar with rust idioms and clap! 😅. From what I understand, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As far as I understood the stdlib docs/code this conversion is guaranteed to succeed. If the user passes a nonsense path a
The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome!!! 😄 Thanks so much! I'll finish review ASAP this week (unless someone else beats me to it). |
||
matches | ||
.subcommand() | ||
.1 | ||
.and_then(|s| migrations_dir_from_cli(s)) | ||
}) | ||
} | ||
|
||
fn migrations_dir(matches: &ArgMatches) -> PathBuf { | ||
migrations_dir_from_cli(matches) | ||
.or_else(|| env::var("MIGRATION_DIRECTORY").map(PathBuf::from).ok()) | ||
.unwrap_or_else(|| { | ||
migrations::find_migrations_directory().unwrap_or_else(handle_error) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diesel CLI 0.99 is going to break when a new version of clap is released...