Skip to content

Commit

Permalink
feat: include timestamp on log print
Browse files Browse the repository at this point in the history
  • Loading branch information
bittermandel committed Jun 15, 2024
1 parent ffdf33e commit f4fbff5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 54 deletions.
93 changes: 50 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.75"
camino = "1.1.6"
chrono = { version = "0.4.30", features = ["serde"] }
chrono = { version = "0.4.30", features = ["serde", "rustc-serialize"] }
clap = { version = "4.4.2", features = ["derive", "env"] }
config = "0.13.3"
dialoguer = { version = "0.10.4", features = ["fuzzy-select"] }
Expand Down
20 changes: 14 additions & 6 deletions src/api/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::{Display, Formatter, Result};
use chrono::{serde::ts_seconds_option, DateTime, Utc};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter, Result};
use tabled::Tabled;

#[derive(Serialize, Deserialize, Debug, Tabled)]
Expand All @@ -17,12 +18,12 @@ pub struct ListOrganizationResponse {

#[derive(Serialize, Deserialize, Debug, Tabled)]
pub struct CreateEnvironmentResponse {
pub name: String
pub name: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ListServicesResponse {
pub services: Vec<Service>
pub services: Vec<Service>,
}

#[derive(Serialize, Deserialize, Debug, Tabled, Clone, PartialEq)]
Expand All @@ -33,19 +34,26 @@ pub struct Service {
#[serde(default, skip_serializing_if = "is_default")]
pub env: DisplayOption<DisplayHashMap>,
#[serde(default, skip_serializing_if = "is_default")]
pub secrets: DisplayOption<DisplayHashMap>
pub secrets: DisplayOption<DisplayHashMap>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ListSecretsResponse {
pub secrets: Vec<Secret>
pub secrets: Vec<Secret>,
}

#[derive(Serialize, Deserialize, Debug, Tabled)]
pub struct Secret {
pub name: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct LogLine {
pub data: String,
#[serde(with = "ts_seconds_option")]
pub time: Option<DateTime<Utc>>,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct DisplayHashMap(pub IndexMap<String, String>);

Expand All @@ -59,7 +67,7 @@ fn is_default<T: Default + PartialEq>(t: &T) -> bool {
impl Display for DisplayOption<DisplayHashMap> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if self.0.is_none() {
return Ok(())
return Ok(());
}

let hashmap = self.0.as_ref().unwrap();
Expand Down
21 changes: 17 additions & 4 deletions src/commands/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use tungstenite::connect;
use tungstenite::http::Uri;
use tungstenite::ClientRequestBuilder;

use crate::api::types::LogLine;
use crate::api::types::{DisplayHashMap, DisplayOption, Service};
use crate::api::APIClient;

Expand Down Expand Up @@ -327,15 +328,21 @@ fn get_image_name(

return Ok(format!(
"register.molnett.org/{}/{}:{}",
org_id, image_name, image_tag.trim()
org_id,
image_name,
image_tag.trim()
));
}

#[derive(Parser, Debug)]
pub struct ImageName {
#[arg(short, long, help = "Image tag to use")]
tag: Option<String>,
#[arg(short, long, help = "Path to a molnett manifest. The manifest's image field will be updated to the returned image name")]
#[arg(
short,
long,
help = "Path to a molnett manifest. The manifest's image field will be updated to the returned image name"
)]
update_manifest: Option<String>,
}

Expand Down Expand Up @@ -455,8 +462,14 @@ impl Logs {
let (mut socket, _) = connect(builder).expect("Could not connect");

loop {
let msg = socket.read().expect("Error reading message");
println!("{}", msg.to_string().trim_end());
let rawmsg = socket.read().expect("Error reading message").to_string();
let msg: LogLine = serde_json::from_str(rawmsg.as_str())?;

println!(
"time=\"{}\" message=\"{}\"",
msg.time.unwrap().format("%d/%m/%Y %H:%M:%S"),
msg.data
);
}
}
}
Expand Down

0 comments on commit f4fbff5

Please sign in to comment.