icydb-cli 0.162.6

Developer CLI tools for IcyDB
//! Module: observability command integration.
//! Responsibility: dispatch metrics, schema, schema-check, and snapshot canister calls.
//! Does not own: endpoint configuration, ICP process construction, or CLI argument parsing.
//! Boundary: decodes raw canister responses and delegates report rendering to submodules.

mod metrics;
mod render;
mod schema;
mod schema_check;
mod snapshot;

use crate::cli::{CanisterTarget, MetricsArgs};
use crate::icp::{call_query_hex, call_update_hex};

pub(crate) fn run_metrics_command(args: MetricsArgs) -> Result<(), String> {
    metrics::run_metrics_command(args)
}

pub(crate) fn run_schema_show_command(target: CanisterTarget) -> Result<(), String> {
    schema::run_schema_show_command(target)
}

pub(crate) fn run_schema_check_command(target: CanisterTarget) -> Result<(), String> {
    schema_check::run_schema_check_command(target)
}

pub(crate) fn run_snapshot_command(target: CanisterTarget) -> Result<(), String> {
    snapshot::run_snapshot_command(target)
}

fn call_query(
    environment: &str,
    canister: &str,
    method: &str,
    candid_arg: &str,
) -> Result<Vec<u8>, String> {
    call_query_hex(environment, canister, method, candid_arg, |stderr| {
        format!(
            "IcyDB query method '{method}' failed on canister '{canister}' in environment '{environment}': {stderr}",
        )
    })
}

fn call_update(
    environment: &str,
    canister: &str,
    method: &str,
    candid_arg: &str,
) -> Result<Vec<u8>, String> {
    call_update_hex(environment, canister, method, candid_arg, |stderr| {
        format!(
            "IcyDB update method '{method}' failed on canister '{canister}' in environment '{environment}': {stderr}",
        )
    })
}

#[cfg(test)]
pub(crate) mod test_support {
    pub(crate) fn metrics_candid_arg(window_start_ms: Option<u64>) -> String {
        super::metrics::metrics_candid_arg(window_start_ms)
    }

    pub(crate) fn render_metrics_report(report: &icydb::metrics::EventReport) -> String {
        super::metrics::render_metrics_report(report)
    }

    pub(crate) fn render_schema_report(report: &[icydb::db::EntitySchemaDescription]) -> String {
        super::schema::render_schema_report(report)
    }

    pub(crate) fn render_schema_check_report(
        report: &[icydb::db::EntitySchemaCheckDescription],
    ) -> String {
        super::schema_check::render_schema_check_report(report)
    }

    pub(crate) fn render_snapshot_report(report: &icydb::db::StorageReport) -> String {
        super::snapshot::render_snapshot_report(report)
    }
}