ic-query-cli 0.86.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
use super::options::{NnsLeafInfoOptions, NnsLeafListOptions, NnsLeafRefreshOptions};
use crate::storage::cache_root;
use crate::{
    cli::common::{current_unix_secs, write_text_or_json_verbose},
    nns::{NnsCommandError, write_text_or_json},
    progress::StderrQueryProgress,
};
use clap::ArgMatches;
use ic_query::QueryProgress;
use ic_query::nns::NnsInventoryCacheRequest;
use ic_query::nns::NnsInventoryInfoRequest;
use ic_query::nns::NnsInventoryListRequest;
use ic_query::nns::NnsInventoryRefreshReport;
use ic_query::nns::NnsInventoryRefreshRequest;
use serde::Serialize;

pub(in crate::nns) async fn run_cached_leaf_list<Report, Error>(
    matches: &ArgMatches,
    network: &str,
    build_report: impl AsyncFn(
        &NnsInventoryListRequest,
        &mut dyn QueryProgress,
    ) -> Result<Report, Error>,
    render: &(dyn Fn(&Report) -> String + Sync),
    render_verbose: &(dyn Fn(&Report) -> String + Sync),
) -> Result<(), NnsCommandError>
where
    Report: Serialize,
    Error: Into<NnsCommandError>,
{
    let options = NnsLeafListOptions::from_matches(matches, network);
    let cache = NnsInventoryCacheRequest::new(cache_root()?, options.common.network);
    let request =
        NnsInventoryListRequest::new(cache, options.common.source_endpoint, current_unix_secs()?);
    let report = build_report(&request, &mut StderrQueryProgress::new())
        .await
        .map_err(Into::into)?;
    write_text_or_json_verbose(
        options.common.format,
        &report,
        options.verbose,
        render,
        render_verbose,
    )
}

pub(in crate::nns) async fn run_cached_leaf_info<Report, Error>(
    matches: &ArgMatches,
    network: &str,
    build_report: impl AsyncFn(
        &NnsInventoryInfoRequest,
        &mut dyn QueryProgress,
    ) -> Result<Report, Error>,
    render: &(dyn Fn(&Report) -> String + Sync),
) -> Result<(), NnsCommandError>
where
    Report: Serialize,
    Error: Into<NnsCommandError>,
{
    let options = NnsLeafInfoOptions::from_matches(matches, network);
    let cache = NnsInventoryCacheRequest::new(cache_root()?, options.common.network);
    let request = NnsInventoryInfoRequest::new(
        cache,
        options.common.source_endpoint,
        options.input,
        current_unix_secs()?,
    );
    let report = build_report(&request, &mut StderrQueryProgress::new())
        .await
        .map_err(Into::into)?;
    write_text_or_json(options.common.format, &report, render)
}

pub(in crate::nns) async fn run_cached_leaf_refresh<Error>(
    matches: &ArgMatches,
    network: &str,
    refresh_report: impl AsyncFn(
        &NnsInventoryRefreshRequest,
    ) -> Result<NnsInventoryRefreshReport, Error>,
) -> Result<(), NnsCommandError>
where
    Error: Into<NnsCommandError>,
{
    let options = NnsLeafRefreshOptions::from_matches(matches, network);
    let format = options.common.format;
    let cache = NnsInventoryCacheRequest::new(cache_root()?, options.common.network);
    let mut request = NnsInventoryRefreshRequest::new(
        cache,
        options.common.source_endpoint,
        current_unix_secs()?,
    )
    .with_dry_run(options.dry_run);
    if let Some(output_path) = options.output_path {
        request = request.with_output_path(output_path);
    }
    let report = refresh_report(&request).await.map_err(Into::into)?;
    write_text_or_json(
        format,
        &report,
        ic_query_text::nns::nns_inventory_refresh_report_text,
    )
}