use super::options::{NnsLeafInfoOptions, NnsLeafListOptions, NnsLeafRefreshOptions};
use crate::{cli::clap::parsed_options, 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::NnsInventoryError;
use ic_query::nns::NnsInventoryInfoRequest;
use ic_query::nns::NnsInventoryListRequest;
use ic_query::nns::NnsInventoryRefreshRequest;
use serde::Serialize;
pub(in crate::nns) async fn run_cached_leaf<List: Serialize, Info: Serialize>(
matches: &ArgMatches,
network: &str,
list: impl AsyncFn(
&NnsInventoryListRequest,
&mut dyn QueryProgress,
) -> Result<List, NnsInventoryError>,
info: impl AsyncFn(
&NnsInventoryInfoRequest,
&mut dyn QueryProgress,
) -> Result<Info, NnsInventoryError>,
render_list: fn(&List, crate::presentation::TextOptions) -> String,
render_info: fn(&Info) -> String,
) -> Result<(), NnsCommandError> {
match matches.subcommand() {
Some(("list", matches)) => run_cached_leaf_list(matches, network, list, render_list).await,
Some(("info", matches)) => run_cached_leaf_info(matches, network, info, render_info).await,
Some(("refresh", matches)) => {
run_cached_leaf_refresh(
matches,
network,
async |request| {
ic_query::nns::refresh_nns_registry_inventory_with_source(
request,
&ic_query::nns::LiveNnsSource::default(),
)
.await
},
crate::presentation::nns::nns_inventory_refresh_report_text,
)
.await
}
_ => unreachable!("clap requires a known NNS leaf subcommand"),
}
}
pub(in crate::nns) async fn run_cached_leaf_list<Report: Serialize>(
matches: &ArgMatches,
network: &str,
build_report: impl AsyncFn(
&NnsInventoryListRequest,
&mut dyn QueryProgress,
) -> Result<Report, NnsInventoryError>,
render: fn(&Report, crate::presentation::TextOptions) -> String,
) -> Result<(), NnsCommandError> {
let options: NnsLeafListOptions = parsed_options(matches);
let format = options.common.format();
let cache = NnsInventoryCacheRequest::new(cache_root()?, network);
let request =
NnsInventoryListRequest::new(cache, options.common.source_endpoint, current_unix_secs()?);
let report = build_report(&request, &mut StderrQueryProgress::new()).await?;
write_text_or_json_verbose(format, &report, options.verbose, render)
}
pub(in crate::nns) async fn run_cached_leaf_info<Report: Serialize>(
matches: &ArgMatches,
network: &str,
build_report: impl AsyncFn(
&NnsInventoryInfoRequest,
&mut dyn QueryProgress,
) -> Result<Report, NnsInventoryError>,
render: fn(&Report) -> String,
) -> Result<(), NnsCommandError> {
let options: NnsLeafInfoOptions = parsed_options(matches);
let format = options.common.format();
let cache = NnsInventoryCacheRequest::new(cache_root()?, network);
let request = NnsInventoryInfoRequest::new(
cache,
options.common.source_endpoint,
options.input,
current_unix_secs()?,
);
let report = build_report(&request, &mut StderrQueryProgress::new()).await?;
write_text_or_json(format, &report, render)
}
pub(in crate::nns) async fn run_cached_leaf_refresh<Report: Serialize>(
matches: &ArgMatches,
network: &str,
refresh_report: impl AsyncFn(&NnsInventoryRefreshRequest) -> Result<Report, NnsInventoryError>,
render: fn(&Report) -> String,
) -> Result<(), NnsCommandError> {
let options: NnsLeafRefreshOptions = parsed_options(matches);
let format = options.common.format();
let cache = NnsInventoryCacheRequest::new(cache_root()?, network);
let mut request = NnsInventoryRefreshRequest::new(
cache,
options.common.source_endpoint,
current_unix_secs()?,
);
request.dry_run = options.dry_run;
request.output_path = options.output_path;
let report = refresh_report(&request).await?;
write_text_or_json(format, &report, render)
}