This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Add tool in case
sqlite3
is unavailable
Adds the `rad node db exec <query>` command. Easy way to run sqlite queries if the sqlite3 CLI is not available or too old.
- Loading branch information
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::ffi::OsString; | ||
|
||
use anyhow::anyhow; | ||
use radicle::Profile; | ||
use radicle_term as term; | ||
|
||
#[derive(PartialEq, Eq)] | ||
pub enum Operation { | ||
Exec { query: String }, | ||
} | ||
|
||
pub fn db(profile: &Profile, args: Vec<OsString>) -> anyhow::Result<()> { | ||
use lexopt::prelude::*; | ||
|
||
let mut parser = lexopt::Parser::from_args(args); | ||
let mut op: Option<Operation> = None; | ||
|
||
while let Some(arg) = parser.next()? { | ||
match arg { | ||
Value(cmd) if op.is_none() => match cmd.to_string_lossy().as_ref() { | ||
"exec" => { | ||
let val = parser | ||
.value() | ||
.map_err(|_| anyhow!("a query to execute must be provided for `exec`"))?; | ||
op = Some(Operation::Exec { | ||
query: val.to_string_lossy().to_string(), | ||
}); | ||
} | ||
unknown => anyhow::bail!("unknown operation '{unknown}'"), | ||
}, | ||
_ => return Err(anyhow!(arg.unexpected())), | ||
} | ||
} | ||
|
||
match op.ok_or_else(|| anyhow!("a command must be provided, eg. `rad node db exec`"))? { | ||
Operation::Exec { query } => { | ||
let db = profile.database_mut()?; | ||
db.execute(query)?; | ||
|
||
let changed = db.change_count(); | ||
if changed > 0 { | ||
term::success!("{changed} row(s) affected."); | ||
} else { | ||
term::print(term::format::italic("No rows affected.")); | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters