use anyhow::Error;
use crate::common::app_context::AppContext;
use crate::common::clap_ext::ArgMatchesExt;
use crate::http_client::{MantaClient, OpenApiResultExt};
use crate::output;
use manta_shared::types::api::node::GetNodesParams;
fn parse_nodes_params(
cli_args: &clap::ArgMatches,
) -> Result<GetNodesParams, Error> {
let xname = cli_args.req_str("VALUE")?.to_string();
Ok(GetNodesParams {
host_expression: xname,
include_siblings: cli_args.get_flag("include-siblings"),
status_filter: cli_args.opt_string("status"),
})
}
pub async fn exec(
ctx: &AppContext<'_>,
token: &str,
cli_args: &clap::ArgMatches,
) -> Result<(), Error> {
let params = parse_nodes_params(cli_args)?;
let nids_only = cli_args.get_flag("nids-only-one-line");
let output_opt = cli_args.opt_str("output");
let status_summary = cli_args.get_flag("summary-status");
let client = MantaClient::from_app_ctx(ctx, Some(token))?;
let node_details_list = client
.openapi
.get_nodes(
Some(params.include_siblings),
params.status_filter.as_deref(),
¶ms.host_expression,
client.site_name(),
)
.await
.into_anyhow()
.await?;
output::node::render_node_details(
node_details_list,
nids_only,
false,
status_summary,
output_opt,
)
}
#[cfg(test)]
mod tests {
use super::*;
fn nodes_cmd() -> clap::Command {
crate::build::get::subcommand_get_node_details()
}
#[test]
fn parse_xname_only() {
let matches = nodes_cmd().get_matches_from(["nodes", "x1000c0s0b0n0"]);
let params = parse_nodes_params(&matches).unwrap();
assert_eq!(params.host_expression, "x1000c0s0b0n0");
assert!(!params.include_siblings);
assert!(params.status_filter.is_none());
}
#[test]
fn parse_with_siblings() {
let matches = nodes_cmd().get_matches_from([
"nodes",
"x1000c0s0b0n0",
"--include-siblings",
]);
let params = parse_nodes_params(&matches).unwrap();
assert!(params.include_siblings);
}
#[test]
fn parse_with_status() {
let matches = nodes_cmd().get_matches_from([
"nodes",
"x1000c0s0b0n0",
"--status",
"ON",
]);
let params = parse_nodes_params(&matches).unwrap();
assert_eq!(params.status_filter.as_deref(), Some("ON"));
}
}