diff --git a/butane_cli/src/lib.rs b/butane_cli/src/lib.rs index 9b96027f..f1a56de9 100644 --- a/butane_cli/src/lib.rs +++ b/butane_cli/src/lib.rs @@ -49,14 +49,16 @@ pub fn default_name() -> String { Utc::now().format("%Y%m%d_%H%M%S%3f").to_string() } -pub fn init(base_dir: &PathBuf, name: &str, connstr: &str) -> Result<()> { +pub fn init(base_dir: &PathBuf, name: &str, connstr: &str, connect: bool) -> Result<()> { if db::get_backend(name).is_none() { eprintln!("Unknown backend {name}"); std::process::exit(1); }; let spec = db::ConnectionSpec::new(name, connstr); - db::connect(&spec)?; // ensure we can + if connect { + db::connect(&spec)?; + } std::fs::create_dir_all(base_dir)?; spec.save(base_dir)?; diff --git a/butane_cli/src/main.rs b/butane_cli/src/main.rs index f0d9402c..5db1f29c 100644 --- a/butane_cli/src/main.rs +++ b/butane_cli/src/main.rs @@ -4,7 +4,7 @@ use butane_cli::{ base_dir, clean, clear_data, collapse_migrations, delete_table, detach_latest_migration, embed, get_migrations, handle_error, list_migrations, migrate, Result, }; -use clap::{value_parser, Arg, ArgMatches}; +use clap::{value_parser, Arg, ArgMatches, ArgAction}; fn main() { let app = clap::Command::new("butane") @@ -33,6 +33,14 @@ fn main() { .required(true) .index(2) .help("Database connection string. Format depends on backend"), + ) + .arg( + Arg::new("connect") + .long("no-connect") + .action(ArgAction::SetFalse) + .required(false) + .num_args(0) + .help("Do not connect to the database"), ), ) .subcommand( @@ -158,7 +166,8 @@ fn init(base_dir: &PathBuf, args: Option<&ArgMatches>) -> Result<()> { let args = args.unwrap(); let name: &String = args.get_one("BACKEND").unwrap(); let connstr: &String = args.get_one("CONNECTION").unwrap(); - butane_cli::init(base_dir, name, connstr) + let connect: bool = *args.get_one::("connect").unwrap(); + butane_cli::init(base_dir, name, connstr, connect) } fn make_migration(base_dir: &PathBuf, args: Option<&ArgMatches>) -> Result<()> {