Skip to content

Commit

Permalink
Fixing passing --database-url as global cli arg
Browse files Browse the repository at this point in the history
--database-url is passed as global cli argument, this means it may be
not part of the current ArgMatches. To solve this we need to look recursivly
in all current subcommands for the database-url
  • Loading branch information
weiznich committed Sep 25, 2017
1 parent b9f5aa0 commit 9d68c28
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions diesel_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub fn build_cli() -> App<'static, 'static> {
.subcommand(generate_bash_completion_subcommand)
.subcommand(infer_schema_subcommand)
.setting(AppSettings::SubcommandRequiredElseHelp)
.setting(AppSettings::PropagateGlobalValuesDown)
}

fn migration_dir_arg<'a, 'b>() -> Arg<'a, 'b> {
Expand Down
9 changes: 7 additions & 2 deletions diesel_cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,15 @@ pub fn schema_table_exists(database_url: &str) -> DatabaseResult<bool> {
}.map_err(Into::into)
}

pub fn database_url(matches: &ArgMatches) -> String {
fn database_url_from_cli(matches: &ArgMatches) -> Option<String> {
matches
.value_of("DATABASE_URL")
.map(|s| s.into())
.map(Into::into)
.or_else(|| matches.subcommand().1.and_then(|s| database_url_from_cli(s)))
}

pub fn database_url(matches: &ArgMatches) -> String {
database_url_from_cli(matches)
.or_else(|| env::var("DATABASE_URL").ok())
.unwrap_or_else(|| handle_error(DatabaseError::DatabaseUrlMissing))
}
Expand Down

0 comments on commit 9d68c28

Please sign in to comment.