Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: `db exec` subcommand
✗ CI failure Fintan Halpenny committed 6 months ago
commit 4400af31a2afc9cd9cde9ac3ac766d01cabbd9fe
parent 2ee7f0492e38f9be2b9221416337182df57b07b0
3 failed (3 total) View logs
3 files changed +27 -42
modified crates/radicle-cli/src/commands/node.rs
@@ -56,8 +56,8 @@ pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
                control::config(&node)?;
            }
        }
-
        Command::Db { args } => {
-
            commands::db(&profile, args)?;
+
        Command::Db(op) => {
+
            commands::db(&profile, op)?;
        }
        Command::Debug => {
            control::debug(&mut node)?;
modified crates/radicle-cli/src/commands/node/args.rs
@@ -116,18 +116,15 @@ pub(super) enum Command {
        addresses: bool,
    },

-
    /// Interact with the database
-
    Db {
-
        /// The external arguments to pass, e.g. `rad node db exec`
-
        #[arg(long)]
-
        args: Vec<OsString>,
-
    },
+
    /// Interact with the node database
+
    #[command(subcommand, hide = true)]
+
    Db(DbOperation),

    /// Watch and print events.
-
    /// 
+
    ///
    /// This command will connect to the node and print events to
    /// standard output as they occur.
-
    /// 
+
    ///
    /// If no timeout or count is specified, it will run indefinitely.
    Events {
        /// How long to wait to receive an event before giving up
@@ -216,3 +213,19 @@ impl Default for Command {
        Command::Status { only: None }
    }
}
+

+
/// Operations related to the [`Command::Db`]
+
#[derive(Debug, Subcommand)]
+
pub(super) enum DbOperation {
+
    /// Execute an SQL operation on the local node database.
+
    ///
+
    /// The command only returns the number of rows that are affected by the
+
    /// query. This means that `SELECT` queries will not return their output.
+
    ///
+
    /// The command should only be used for executing queries given you know
+
    /// what you are doing.
+
    Exec {
+
        #[arg(value_name = "SQL")]
+
        query: String,
+
    },
+
}
modified crates/radicle-cli/src/commands/node/commands.rs
@@ -1,39 +1,11 @@
-
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())),
-
        }
-
    }
+
use super::args::DbOperation;

-
    match op.ok_or_else(|| anyhow!("a command must be provided, eg. `rad node db exec`"))? {
-
        Operation::Exec { query } => {
+
pub fn db(profile: &Profile, op: DbOperation) -> anyhow::Result<()> {
+
    match op {
+
        DbOperation::Exec { query } => {
            let db = profile.database_mut()?;
            db.execute(query)?;