use crate::cli::command::CommandRequest;
pub use super::list::{ResponseItem, Target};
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.instances.get.Request")]
pub struct Request {
pub path_type: Path,
pub targets: Vec<Target>,
pub jq: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
#[schemars(rename = "cli.command.agents.instances.get.Path")]
pub enum Path {
#[serde(rename = "agents/instances/get")]
AgentsInstancesGet,
}
impl CommandRequest for Request {
fn into_command(&self) -> Vec<String> {
let mut argv = vec![
"agents".to_string(),
"instances".to_string(),
"get".to_string(),
];
for target in &self.targets {
argv.push("--target".to_string());
argv.push(target.into_arg_string());
}
if let Some(jq) = &self.jq {
argv.push("--jq".to_string());
argv.push(jq.clone());
}
argv
}
}
#[derive(clap::Args)]
pub struct Args {
#[arg(long = "target", required = true)]
pub targets: Vec<String>,
#[arg(long)]
pub jq: Option<String>,
}
#[derive(clap::Args)]
#[command(args_conflicts_with_subcommands = true)]
pub struct Command {
#[command(flatten)]
pub args: Args,
#[command(subcommand)]
pub schema: Option<Schema>,
}
#[derive(clap::Subcommand)]
pub enum Schema {
RequestSchema(request_schema::Args),
ResponseSchema(response_schema::Args),
}
impl TryFrom<Args> for Request {
type Error = crate::cli::command::FromArgsError;
fn try_from(args: Args) -> Result<Self, Self::Error> {
let targets = args
.targets
.iter()
.map(|s| {
s.parse::<Target>().map_err(|msg| {
crate::cli::command::FromArgsError::path_parse("target", msg)
})
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
path_type: Path::AgentsInstancesGet,
targets,
jq: args.jq,
})
}
}
#[cfg(feature = "cli-executor")]
pub async fn execute<E: crate::cli::command::CommandExecutor>(
executor: &E,
mut request: Request,
agent_arguments: Option<&crate::cli::command::AgentArguments>,
) -> Result<E::Stream<ResponseItem>, E::Error> {
request.jq = None;
executor.execute(request, agent_arguments).await
}
#[cfg(feature = "cli-executor")]
pub async fn execute_jq<E: crate::cli::command::CommandExecutor>(
executor: &E,
mut request: Request,
jq: String,
agent_arguments: Option<&crate::cli::command::AgentArguments>,
) -> Result<E::Stream<serde_json::Value>, E::Error> {
request.jq = Some(jq);
executor.execute(request, agent_arguments).await
}
pub mod request_schema;
pub mod response_schema;