ic-query-cli 0.103.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: ic::replica_version
//!
//! Responsibility: parse and dispatch official Dashboard replica-version commands.
//! Does not own: Dashboard transport, source validation, report construction, or rendering.
//! Boundary: exposes one bounded page and one exact release lookup to the IC CLI facade.

use super::IcCommandError;
use crate::cli::common::OutputFormat;
use crate::cli::common::{
    COLLECTION_MODE_LIVE, collection_help, current_unix_secs, write_text_or_json,
};
use crate::presentation::ic::ic_replica_version_info_report_text;
use crate::presentation::ic::ic_replica_version_list_report_text;
use clap::Subcommand;
use ic_query::ic::DEFAULT_IC_DASHBOARD_SOURCE_ENDPOINT;
use ic_query::ic::IcReplicaVersionInfoRequest;
use ic_query::ic::IcReplicaVersionListQuery;
use ic_query::ic::IcReplicaVersionListRequest;
use ic_query::ic::LiveIcSource;
use ic_query::ic::MAX_IC_REPLICA_VERSION_PAGE_LIMIT;
use ic_query::ic::build_ic_replica_version_info_report_with_source;
use ic_query::ic::build_ic_replica_version_list_report_with_source;

const DEFAULT_LIMIT_ARG: &str = "50";

const INFO_HELP_AFTER: &str = "\
Examples:
  icq ic replica-version info e3d101b22ae3fa02aca737f9fb96cc6c4ca83ac3
  icq ic replica-version info e3d101b22ae3fa02aca737f9fb96cc6c4ca83ac3 --json

This command makes exactly one official Dashboard detail request. It reports
release-election and Dashboard-recorded Subnet rollout evidence; it does not
prove which replica version a Subnet is currently running. No cache is used.";

const LIST_HELP_AFTER: &str = "\
Examples:
  icq ic replica-version list --limit 25
  icq ic replica-version list --offset 25 --max-proposal-index 438 --json

This command makes exactly one official Dashboard list request for at most 100
release rows. It never follows an offset automatically. Reuse the returned
resolved_max_proposal_index as --max-proposal-index when requesting a later
page so newly indexed proposals do not move the selected ceiling. The response
is off-chain, non-certified, not runtime-version evidence, and is not cached.";

pub(super) async fn run(command: ReplicaVersionCommand) -> Result<(), IcCommandError> {
    match command {
        ReplicaVersionCommand::Info(options) => run_info(options).await,
        ReplicaVersionCommand::List(options) => run_list(options).await,
    }
}

async fn run_info(options: ReplicaInfoOptions) -> Result<(), IcCommandError> {
    let (format, request) = info_request(options, current_unix_secs()?);
    let report =
        build_ic_replica_version_info_report_with_source(&request, &LiveIcSource::default())
            .await?;
    write_text_or_json(format, &report, ic_replica_version_info_report_text)
}

async fn run_list(options: ReplicaListOptions) -> Result<(), IcCommandError> {
    let (format, request) = list_request(options, current_unix_secs()?);
    let report =
        build_ic_replica_version_list_report_with_source(&request, &LiveIcSource::default())
            .await?;
    write_text_or_json(format, &report, ic_replica_version_list_report_text)
}

///
/// ReplicaVersionCommand
///
/// Typed Dashboard release operation and its leaf arguments.
///

#[derive(Subcommand)]
#[command(about = "Inspect official Dashboard replica release records", long_about = None, mut_subcommands(|command| command.mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_IC_DASHBOARD_SOURCE_ENDPOINT).help("Official IC Dashboard API v3 base endpoint"))))]
pub(super) enum ReplicaVersionCommand {
    #[command(about = "Show one exact Dashboard replica release record", long_about = None, after_help = collection_help(COLLECTION_MODE_LIVE, INFO_HELP_AFTER))]
    Info(ReplicaInfoOptions),
    #[command(about = "Show one bounded Dashboard replica release page", long_about = None, after_help = collection_help(COLLECTION_MODE_LIVE, LIST_HELP_AFTER))]
    List(ReplicaListOptions),
}

///
/// ReplicaInfoOptions
///
/// Release identity, source and output for one exact Dashboard lookup.
///

#[derive(clap::Args)]
#[group(skip)]
pub(super) struct ReplicaInfoOptions {
    #[arg(
        id = "replica-version-id",
        value_name = "replica-version-id",
        help = "Exact 40-character lowercase hexadecimal version id"
    )]
    pub(super) replica_version_id: String,
    #[command(flatten)]
    pub(super) source: crate::cli::common::SourceOutputOptions,
}

///
/// ReplicaListOptions
///
/// Page bounds, source and output for one Dashboard release page.
///

#[derive(clap::Args)]
#[group(skip)]
pub(super) struct ReplicaListOptions {
    #[arg(long, value_name = "rows", default_value = DEFAULT_LIMIT_ARG, value_parser = clap::builder::RangedU64ValueParser::<u16>::new().range(1..=u64::from(MAX_IC_REPLICA_VERSION_PAGE_LIMIT)), help = "Maximum release rows; one through 100")]
    pub(super) limit: u16,
    #[arg(
        long,
        value_name = "rows",
        default_value = "0",
        help = "Zero-based release-row offset"
    )]
    pub(super) offset: u64,
    #[arg(
        id = "max-proposal-index",
        long,
        value_name = "index",
        help = "Optional proposal-index ceiling returned by an earlier page"
    )]
    pub(super) max_proposal_index: Option<u64>,
    #[command(flatten)]
    pub(super) source: crate::cli::common::SourceOutputOptions,
}

fn info_request(
    options: ReplicaInfoOptions,
    now_unix_secs: u64,
) -> (OutputFormat, IcReplicaVersionInfoRequest) {
    let (format, dashboard) = options.source.into_dashboard_request(now_unix_secs);
    let request = IcReplicaVersionInfoRequest::new(dashboard, options.replica_version_id);
    (format, request)
}

fn list_request(
    options: ReplicaListOptions,
    now_unix_secs: u64,
) -> (OutputFormat, IcReplicaVersionListRequest) {
    let (format, dashboard) = options.source.into_dashboard_request(now_unix_secs);
    let request = IcReplicaVersionListRequest::new(
        dashboard,
        IcReplicaVersionListQuery::new(options.limit, options.offset, options.max_proposal_index),
    );
    (format, request)
}

#[cfg(test)]
mod tests;