ic-query-cli 0.104.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
use super::*;
use super::{
    data_center::{DATA_CENTER_SPEC, command as data_center_command},
    governance::governance_command,
    leaf::{
        NnsCommonOptions, NnsLeafInfoOptions, NnsLeafListOptions, NnsLeafRefreshOptions,
        NnsPagedRefreshOptions, info_command as leaf_info_command,
        list_command as leaf_list_command, refresh_command as leaf_refresh_command,
    },
    neuron::{NeuronInfoOptions, NeuronListOptions, neuron_command},
    node::{NODE_SPEC, node_command, node_list_command, node_list_request},
    node_operator::{NODE_OPERATOR_SPEC, command as node_operator_command},
    node_provider::{NODE_PROVIDER_SPEC, command as node_provider_command},
    proposals::{
        DEFAULT_NNS_PROPOSAL_SOURCE_ENDPOINT, NnsProposalListSort, NnsProposalRewardStatusFilter,
        NnsProposalStatusFilter, NnsProposalTopicFilter, ProposalActivityOptions,
        ProposalInfoOptions, ProposalListOptions, nns_proposal_command,
    },
    registry::registry_command,
    subnet::{catalog_info_request, catalog_list_request, subnet_command},
    topology::{topology_command, topology_refresh_request},
};
use clap::{ArgMatches, Command as ClapCommand};
use ic_query::nns::data_center::DEFAULT_NNS_DATA_CENTER_SOURCE_ENDPOINT;
use ic_query::nns::node::DEFAULT_NNS_NODE_SOURCE_ENDPOINT;
use ic_query::nns::node_operator::DEFAULT_NNS_NODE_OPERATOR_SOURCE_ENDPOINT;
use ic_query::nns::node_provider::DEFAULT_NNS_NODE_PROVIDER_SOURCE_ENDPOINT;
use ic_query::nns::registry::DEFAULT_NNS_REGISTRY_SOURCE_ENDPOINT;
use ic_query::subnet_catalog::DEFAULT_SUBNET_CATALOG_SOURCE_ENDPOINT;
use ic_query::subnet_catalog::GeographicScope;
use ic_query::subnet_catalog::MAINNET_NETWORK;
use ic_query::subnet_catalog::SubnetKind;
use ic_query::subnet_catalog::SubnetSpecialization;
use std::path::PathBuf;

fn parse_test_options<Options>(
    command: ClapCommand,
    args: &[&str],
    from_matches: fn(&ArgMatches, &str) -> Options,
) -> Result<Options, NnsCommandError> {
    let matches = parse_test_matches(command, args)?;
    Ok(from_matches(&matches, MAINNET_NETWORK))
}

fn parsed_arguments<Options: clap::FromArgMatches>(matches: &ArgMatches, _: &str) -> Options {
    crate::cli::clap::parsed_options(matches)
}

fn common_options(matches: &ArgMatches, network: &str) -> NnsCommonOptions {
    NnsCommonOptions::from_source(parsed_arguments(matches, network), network)
}

fn parse_test_matches(command: ClapCommand, args: &[&str]) -> Result<ArgMatches, NnsCommandError> {
    crate::cli::parse_test_args(command, args)
        .map_err(|error| NnsCommandError::Usage(error.to_string()))
}

mod data_center;
mod governance;
mod neuron;
mod node;
mod node_operator;
mod node_provider;
mod proposals;
mod registry;
mod subnet;
mod topology_help;
mod topology_options;

const TEST_NOW_UNIX_SECS: u64 = 1_780_531_200;

fn test_node_list_request(
    matches: &ArgMatches,
    network: &str,
) -> (OutputFormat, bool, ic_query::nns::node::NnsNodeListRequest) {
    node_list_request(
        matches,
        ic_query::nns::NnsInventoryCacheRequest::new("/cache", network),
        TEST_NOW_UNIX_SECS,
    )
}

fn test_catalog_list_request(
    matches: &ArgMatches,
    network: &str,
) -> (
    OutputFormat,
    bool,
    ic_query::subnet_catalog::SubnetCatalogListRequest,
) {
    catalog_list_request(
        crate::cli::clap::parsed_options(matches),
        ic_query::subnet_catalog::SubnetCatalogCacheRequest::new("/cache", network),
        TEST_NOW_UNIX_SECS,
    )
}

fn test_catalog_info_request(
    matches: &ArgMatches,
    network: &str,
) -> (
    OutputFormat,
    ic_query::subnet_catalog::SubnetCatalogInfoRequest,
) {
    catalog_info_request(
        crate::cli::clap::parsed_options(matches),
        ic_query::subnet_catalog::SubnetCatalogCacheRequest::new("/cache", network),
        TEST_NOW_UNIX_SECS,
    )
}

fn replica_endpoint(request: &ic_query::nns::governance::NnsGovernanceRequest) -> &str {
    let ic_query::nns::governance::NnsGovernanceSourceSelection::ReplicaQuery { endpoint, .. } =
        &request.source
    else {
        panic!("CLI parser must select a replica query")
    };
    endpoint
}

fn catalog_endpoint(policy: &ic_query::subnet_catalog::CatalogReadPolicy) -> &str {
    let ic_query::subnet_catalog::CatalogReadPolicy::RefreshMissingOrInvalid {
        source: ic_query::subnet_catalog::CatalogSourceSelection::UncertifiedQuery { endpoint },
    } = policy
    else {
        panic!("CLI parser must select missing-or-invalid recovery from one endpoint")
    };
    endpoint
}

fn operation(command: ClapCommand, path: &[&str]) -> ClapCommand {
    path.iter().fold(command, |command, name| {
        command
            .find_subcommand(name)
            .expect("declared operation")
            .clone()
    })
}

fn neuron_list_command() -> ClapCommand {
    operation(neuron_command(), &["list"])
}

fn neuron_info_command() -> ClapCommand {
    operation(neuron_command(), &["info"])
}

fn neuron_refresh_command() -> ClapCommand {
    operation(neuron_command(), &["refresh"])
}

fn neuron_distribution_command() -> ClapCommand {
    operation(neuron_command(), &["distribution"])
}

fn neuron_cache_command() -> ClapCommand {
    operation(neuron_command(), &["cache"])
}

fn neuron_cache_status_command() -> ClapCommand {
    operation(neuron_command(), &["cache", "status"])
}

fn nns_proposal_list_command() -> ClapCommand {
    operation(nns_proposal_command(), &["list"])
}

fn nns_proposal_info_command() -> ClapCommand {
    operation(nns_proposal_command(), &["info"])
}

fn nns_proposal_refresh_command() -> ClapCommand {
    operation(nns_proposal_command(), &["refresh"])
}

fn nns_proposal_activity_command() -> ClapCommand {
    operation(nns_proposal_command(), &["activity"])
}

fn nns_proposal_cache_command() -> ClapCommand {
    operation(nns_proposal_command(), &["cache"])
}

fn nns_proposal_cache_status_command() -> ClapCommand {
    operation(nns_proposal_command(), &["cache", "status"])
}

fn topology_leaf(name: &str) -> clap::Command {
    topology_command().find_subcommand(name).unwrap().clone()
}