ic-query-cli 0.100.2

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Shared CLI orchestration for NNS Governance collection refresh and status.

use super::{NnsCommandError, leaf::NnsPagedRefreshOptions};
use crate::presentation::nns::governance::nns_governance_cache_status_report_text;
use crate::presentation::nns::governance::nns_governance_refresh_report_text;
use crate::{
    cli::common::{OutputFormat, current_unix_secs, write_text_or_json},
    progress::StderrQueryProgress,
    storage::cache_root,
};

use ic_query::QueryProgress;
use ic_query::nns::NnsGovernanceCacheRequest;
use ic_query::nns::NnsGovernanceRefreshRequest;
use ic_query::nns::governance::NnsGovernanceCacheStatusReport;
use ic_query::nns::governance::NnsGovernanceRefreshReport;

pub(super) async fn run_refresh<Error>(
    options: NnsPagedRefreshOptions,
    network: &str,
    refresh: impl AsyncFn(
        &NnsGovernanceRefreshRequest,
        &mut dyn QueryProgress,
    ) -> Result<NnsGovernanceRefreshReport, Error>,
) -> Result<(), NnsCommandError>
where
    Error: Into<NnsCommandError>,
{
    let format = options.common.format();
    let request = options.into_request(cache_root()?, network, current_unix_secs()?);
    let report = refresh(&request, &mut StderrQueryProgress::new())
        .await
        .map_err(Into::into)?;
    write_text_or_json(format, &report, nns_governance_refresh_report_text)
}

pub(super) fn run_cache_status<Error>(
    options: JsonOptions,
    network: &str,
    build: fn(&NnsGovernanceCacheRequest) -> Result<NnsGovernanceCacheStatusReport, Error>,
) -> Result<(), NnsCommandError>
where
    Error: Into<NnsCommandError>,
{
    let request = NnsGovernanceCacheRequest::new(cache_root()?, network);
    let report = build(&request).map_err(Into::into)?;
    write_text_or_json(
        OutputFormat::from_json(options.json),
        &report,
        nns_governance_cache_status_report_text,
    )
}

///
/// JsonOptions
///
/// Output selection for local NNS collection inspection.
///

#[derive(Clone, Copy, clap::Args)]
#[group(skip)]
pub(super) struct JsonOptions {
    #[arg(long, help = "Print raw JSON instead of human-readable text")]
    pub(super) json: bool,
}