Skip to content

Commit 84c2fa1

Browse files
committed
slightly rework approach with namespace parameter in CLI and introduce --db-name optional argument
1 parent b89e28c commit 84c2fa1

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

bottomless-cli/src/main.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct Cli {
2727
namespace: Option<String>,
2828
#[clap(long)]
2929
encryption_key: Option<Bytes>,
30+
#[clap(long)]
31+
db_name: Option<String>,
3032
}
3133

3234
#[derive(Debug, Subcommand)]
@@ -231,9 +233,35 @@ async fn run() -> Result<()> {
231233
std::str::from_utf8(encryption_key)?,
232234
);
233235
}
234-
let namespace = options.namespace.as_deref().unwrap_or("ns-default");
235-
std::env::set_var("LIBSQL_BOTTOMLESS_DATABASE_ID", namespace);
236-
236+
let namespace_init = std::env::var("LIBSQL_BOTTOMLESS_DATABASE_ID").unwrap_or(String::new());
237+
if options.db_name.is_some() && options.namespace.is_some() {
238+
return Err(anyhow!(
239+
"only one of the arguments --db-name or --namespace is expected to be set"
240+
));
241+
}
242+
if let Some(ref db_name) = options.db_name {
243+
if namespace_init != "" {
244+
std::env::set_var(
245+
"LIBSQL_BOTTOMLESS_DATABASE_ID",
246+
format!("ns-{}:{}", &namespace_init, db_name),
247+
);
248+
} else {
249+
return Err(anyhow!(
250+
"db_name can be set only if LIBSQL_BOTTOMLESS_DATABASE_ID env var has namespace ID"
251+
));
252+
}
253+
} else {
254+
let namespace = options.namespace.as_deref().unwrap_or("ns-default");
255+
std::env::set_var("LIBSQL_BOTTOMLESS_DATABASE_ID", namespace);
256+
}
257+
let namespace = std::env::var("LIBSQL_BOTTOMLESS_DATABASE_ID").unwrap();
258+
if namespace_init != namespace {
259+
tracing::info!(
260+
"LIBSQL_BOTTOMLESS_DATABASE_ID env var were updated: '{}' -> '{}'",
261+
namespace_init,
262+
namespace
263+
);
264+
}
237265
match options.command {
238266
Commands::Create { ref source_db_path } => {
239267
let mut client =

0 commit comments

Comments
 (0)