Skip to content

Commit

Permalink
Allow init without connecting (#195)
Browse files Browse the repository at this point in the history
Add `--no-connect` option to butane cli
  • Loading branch information
jayvdb authored Feb 29, 2024
1 parent 820df4a commit 1e35bee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 4 additions & 2 deletions butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
13 changes: 11 additions & 2 deletions butane_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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::<bool>("connect").unwrap();
butane_cli::init(base_dir, name, connstr, connect)
}

fn make_migration(base_dir: &PathBuf, args: Option<&ArgMatches>) -> Result<()> {
Expand Down

0 comments on commit 1e35bee

Please sign in to comment.