summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/error.rs4
-rw-r--r--src/main.rs6
-rw-r--r--src/requests/start.rs19
3 files changed, 21 insertions, 8 deletions
diff --git a/src/error.rs b/src/error.rs
index 27fc7a6..f15c60a 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,9 +1,10 @@
1use std::fmt::Debug; 1use std::{fmt::Debug, num::ParseIntError};
2 2
3pub enum CliError { 3pub enum CliError {
4 Reqwest(reqwest::Error), 4 Reqwest(reqwest::Error),
5 Config(config::ConfigError), 5 Config(config::ConfigError),
6 Serde(serde_json::Error), 6 Serde(serde_json::Error),
7 Parse(ParseIntError),
7 WsResponse, 8 WsResponse,
8} 9}
9 10
@@ -13,6 +14,7 @@ impl Debug for CliError {
13 Self::Reqwest(err) => { err.fmt(f) }, 14 Self::Reqwest(err) => { err.fmt(f) },
14 Self::Config(err) => { err.fmt(f) }, 15 Self::Config(err) => { err.fmt(f) },
15 Self::Serde(err) => { err.fmt(f) }, 16 Self::Serde(err) => { err.fmt(f) },
17 Self::Parse(err) => { err.fmt(f) },
16 Self::WsResponse => { f.write_str("Error in Response") }, 18 Self::WsResponse => { f.write_str("Error in Response") },
17 } 19 }
18 } 20 }
diff --git a/src/main.rs b/src/main.rs
index d7c985f..204e671 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,9 +12,9 @@ mod config;
12mod error; 12mod error;
13mod requests; 13mod requests;
14 14
15static OVERVIEW_STYLE: &str = "{spinner:.green} {wide_msg}({elapsed})"; 15static OVERVIEW_STYLE: &str = "{spinner:.green} ({elapsed}{wide_msg}";
16static OVERVIEW_ERROR: &str = "✗ {wide_msg}({elapsed})"; 16static OVERVIEW_ERROR: &str = "✗ ({elapsed}) {wide_msg}";
17static OVERVIEW_DONE: &str = "✓ {wide_msg}({elapsed})"; 17static OVERVIEW_DONE: &str = "✓ ({elapsed}) {wide_msg}";
18static DEFAULT_STYLE: &str = " {spinner:.green} {wide_msg}"; 18static DEFAULT_STYLE: &str = " {spinner:.green} {wide_msg}";
19static DONE_STYLE: &str = " ✓ {wide_msg}"; 19static DONE_STYLE: &str = " ✓ {wide_msg}";
20static ERROR_STYLE: &str = " ✗ {wide_msg}"; 20static ERROR_STYLE: &str = " ✗ {wide_msg}";
diff --git a/src/requests/start.rs b/src/requests/start.rs
index 882b154..ca4ca44 100644
--- a/src/requests/start.rs
+++ b/src/requests/start.rs
@@ -1,5 +1,5 @@
1use futures_util::{StreamExt, SinkExt}; 1use futures_util::{StreamExt, SinkExt};
2use indicatif::MultiProgress; 2use indicatif::{MultiProgress, ProgressBar};
3use reqwest::StatusCode; 3use reqwest::StatusCode;
4use serde::Deserialize; 4use serde::Deserialize;
5use tokio_tungstenite::{connect_async, tungstenite::Message}; 5use tokio_tungstenite::{connect_async, tungstenite::Message};
@@ -9,7 +9,7 @@ use crate::{error::CliError, default_headers, ErrorResponse, format_url, Protoco
9pub async fn start(id: String, ping: bool) -> Result<(), CliError> { 9pub async fn start(id: String, ping: bool) -> Result<(), CliError> {
10 10
11 let send_start = MultiProgress::new(); 11 let send_start = MultiProgress::new();
12 let overview = add_pb(&send_start, OVERVIEW_STYLE, format!("start {}", id)); 12 let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {}", id));
13 13
14 // TODO: calculate average start-time on server 14 // TODO: calculate average start-time on server
15 let url = format_url("start", Protocols::Http)?; 15 let url = format_url("start", Protocols::Http)?;
@@ -38,7 +38,7 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> {
38 } 38 }
39 39
40 if ping { 40 if ping {
41 let status = status_socket(body.uuid, &send_start).await?; 41 let status = status_socket(body.uuid, &send_start, &overview, id).await?;
42 if status { 42 if status {
43 finish_pb(overview, format!("successfully started {}", body.id), OVERVIEW_DONE); 43 finish_pb(overview, format!("successfully started {}", body.id), OVERVIEW_DONE);
44 } else { 44 } else {
@@ -59,7 +59,7 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> {
59 Ok(()) 59 Ok(())
60} 60}
61 61
62async fn status_socket(uuid: String, pb: &MultiProgress) -> Result<bool, CliError> { 62async fn status_socket(uuid: String, pb: &MultiProgress, overview: &ProgressBar, id: String) -> Result<bool, CliError> {
63 // TODO: Remove unwraps 63 // TODO: Remove unwraps
64 let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); 64 let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string());
65 let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?) 65 let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?)
@@ -68,6 +68,11 @@ async fn status_socket(uuid: String, pb: &MultiProgress) -> Result<bool, CliErro
68 finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE); 68 finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE);
69 69
70 ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); 70 ws_stream.send(Message::Text(uuid.clone())).await.unwrap();
71
72 // Get ETA
73 let eta_msg = ws_stream.next().await.unwrap().unwrap();
74 let eta = get_eta(eta_msg.into_text().unwrap(), uuid.clone())? + overview.elapsed().as_secs();
75 overview.set_message(format!("/{}) start {}", eta, id));
71 76
72 let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); 77 let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string());
73 let msg = ws_stream.next().await.unwrap(); 78 let msg = ws_stream.next().await.unwrap();
@@ -101,6 +106,12 @@ async fn status_socket(uuid: String, pb: &MultiProgress) -> Result<bool, CliErro
101 } 106 }
102} 107}
103 108
109fn get_eta(msg: String, uuid: String) -> Result<u64, CliError> {
110 let spl: Vec<&str> = msg.split('_').collect();
111 if (spl[0] != "eta") || (spl[2] != uuid) { return Err(CliError::WsResponse); };
112 Ok(u64::from_str_radix(spl[1], 10).map_err(CliError::Parse)?)
113}
114
104fn verify_response(res: String, org_uuid: String) -> Result<Verified, CliError> { 115fn verify_response(res: String, org_uuid: String) -> Result<Verified, CliError> {
105 let spl: Vec<&str> = res.split('_').collect(); 116 let spl: Vec<&str> = res.split('_').collect();
106 let res_type = spl[0]; 117 let res_type = spl[0];