use core::time::Duration;
use clap::Args;
use miette::{Context as _, IntoDiagnostic};
use tracing::info;
use ockam::Context;
use ockam_api::address::extract_address_value;
use ockam_api::nodes::service::messages::Messages;
use ockam_api::nodes::BackgroundNodeClient;
use ockam_api::nodes::InMemoryNode;
use ockam_multiaddr::MultiAddr;
use crate::project::util::{
clean_projects_multiaddr, get_projects_secure_channels_from_config_lookup,
};
use crate::util::api::{CloudOpts, TrustOpts};
use crate::util::duration::duration_parser;
use crate::util::{async_cmd, clean_nodes_multiaddr};
use crate::{docs, CommandGlobalOpts};
const LONG_ABOUT: &str = include_str!("./static/send/long_about.txt");
const AFTER_LONG_HELP: &str = include_str!("./static/send/after_long_help.txt");
#[derive(Clone, Debug, Args)]
#[command(
arg_required_else_help = true,
long_about = docs::about(LONG_ABOUT),
after_long_help = docs::after_help(AFTER_LONG_HELP)
)]
pub struct SendCommand {
#[arg(short, long, value_name = "NODE", value_parser = extract_address_value)]
from: Option<String>,
#[arg(short, long, value_name = "ROUTE")]
pub to: MultiAddr,
#[arg(long)]
pub hex: bool,
#[arg(long, value_name = "TIMEOUT", default_value = "10s", value_parser = duration_parser)]
pub timeout: Duration,
pub message: String,
#[command(flatten)]
cloud_opts: CloudOpts,
#[command(flatten)]
pub trust_opts: TrustOpts,
}
impl SendCommand {
pub fn run(self, opts: CommandGlobalOpts) -> miette::Result<()> {
async_cmd(&self.name(), opts.clone(), |ctx| async move {
self.async_run(&ctx, opts).await
})
}
pub fn name(&self) -> String {
"send message".into()
}
async fn async_run(&self, ctx: &Context, opts: CommandGlobalOpts) -> miette::Result<()> {
let (to, meta) = clean_nodes_multiaddr(&self.to, &opts.state)
.await
.context("Argument '--to' is invalid")?;
let msg_bytes = if self.hex {
hex::decode(self.message.clone())
.into_diagnostic()
.context("The message is not a valid hex string")?
} else {
self.message.as_bytes().to_vec()
};
let response: Vec<u8> = if let Some(node) = &self.from {
BackgroundNodeClient::create_to_node(ctx, &opts.state, node.as_str())
.await?
.send_message(ctx, &to, msg_bytes, Some(self.timeout))
.await?
} else {
let identity_name = opts
.state
.get_identity_name_or_default(&self.cloud_opts.identity)
.await?;
info!("starting an in memory node to send a message");
let node_manager = InMemoryNode::start_node(
ctx,
&opts.state,
&identity_name,
self.trust_opts.project_name.clone(),
self.trust_opts.authority_identity.clone(),
self.trust_opts.authority_route.clone(),
)
.await?;
info!("started an in memory node to send a message");
let projects_sc = get_projects_secure_channels_from_config_lookup(
&opts,
ctx,
&node_manager,
&meta,
Some(identity_name),
Some(self.timeout),
)
.await?;
let to = clean_projects_multiaddr(to, projects_sc)?;
info!("sending to {to}");
node_manager
.send_message(ctx, &to, msg_bytes, Some(self.timeout))
.await?
};
let result = if self.hex {
hex::encode(response)
} else {
String::from_utf8(response)
.into_diagnostic()
.context("Received content is not a valid utf8 string")?
};
opts.terminal.stdout().plain(result).write_line()?;
Ok(())
}
}