pub mod api;
pub mod event;
pub mod filter;
pub mod websocket;
use crate::settings::global_user::GlobalUser;
use crate::terminal::styles;
use api::Tail;
use websocket::{TailOptions, WebSocketTail};
use anyhow::Result;
use indicatif::{ProgressBar, ProgressStyle};
use url::Url;
pub async fn run(
user: GlobalUser,
account_id: String,
script_name: String,
url: Option<Url>,
options: TailOptions,
) -> Result<()> {
let progress = &mut ProgressBar::new_spinner()
.with_style(ProgressStyle::default_spinner().template("{spinner} {msg}"));
progress.enable_steady_tick(20);
progress.set_message("Creating tail...");
let tail = &mut Tail::new(user, account_id, script_name, url);
tail.create().await?;
if tail.is_web_socket() {
progress.set_message("Connecting to tail...");
match &mut WebSocketTail::connect(tail.clone(), options).await {
Ok(websocket) => {
progress.abandon_with_message(&format!(
"Connected! Streaming logs from {}... (ctrl-c to quit)",
styles::bold(&tail.script_name)
));
if let Err(err) = websocket.update().await {
log::warn!("{}", err);
};
if let Err(err) = websocket.read().await {
log::warn!("{}", err);
}
}
Err(err) => progress.abandon_with_message(&format!("{}", err)),
}
} else {
progress.set_message(&format!(
"Forwarding logs from {} to {} (ctrl-c to quit)",
styles::bold(&tail.script_name),
styles::url(
tail.url
.clone()
.map(String::from)
.unwrap_or_else(|| "an endpoint".to_owned())
)
));
loop {
tokio::select! {
_ = tokio::signal::ctrl_c() => break
}
}
}
tail.delete().await?;
Ok(())
}