use crate::StandardOptions;
use asimov_protocol::{EndpointTicket, Node, Ticket, Topic};
use color_print::ceprintln;
use core::error::Error;
pub async fn publish(
topic: &Topic,
message: &String,
ticket: &Option<String>,
_flags: &StandardOptions,
) -> Result<(), Box<dyn Error>> {
let mut node = Node::default().bind().await?.start().await?;
node.online().await;
if let Some(ticket) = ticket {
let peer_ticket = EndpointTicket::decode_string(ticket.as_ref()).unwrap();
node.add_peer(peer_ticket.endpoint_addr().id);
}
let mut topic_subscription = node.subscribe_and_join(topic).await?;
ceprintln!("<s,g>✓</> Topic=<s>{:?}</>", topic_subscription);
topic_subscription.publish(message.clone()).await?;
tokio::signal::ctrl_c().await?;
node.terminate().await?;
Ok(())
}