Skip to content

Commit f4fbff5

Browse files
committed
feat: include timestamp on log print
1 parent ffdf33e commit f4fbff5

4 files changed

Lines changed: 82 additions & 54 deletions

File tree

Cargo.lock

Lines changed: 50 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
[dependencies]
99
anyhow = "1.0.75"
1010
camino = "1.1.6"
11-
chrono = { version = "0.4.30", features = ["serde"] }
11+
chrono = { version = "0.4.30", features = ["serde", "rustc-serialize"] }
1212
clap = { version = "4.4.2", features = ["derive", "env"] }
1313
config = "0.13.3"
1414
dialoguer = { version = "0.10.4", features = ["fuzzy-select"] }

src/api/types.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use std::fmt::{Display, Formatter, Result};
1+
use chrono::{serde::ts_seconds_option, DateTime, Utc};
22
use indexmap::IndexMap;
33
use serde::{Deserialize, Serialize};
4+
use std::fmt::{Display, Formatter, Result};
45
use tabled::Tabled;
56

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

1819
#[derive(Serialize, Deserialize, Debug, Tabled)]
1920
pub struct CreateEnvironmentResponse {
20-
pub name: String
21+
pub name: String,
2122
}
2223

2324
#[derive(Serialize, Deserialize, Debug)]
2425
pub struct ListServicesResponse {
25-
pub services: Vec<Service>
26+
pub services: Vec<Service>,
2627
}
2728

2829
#[derive(Serialize, Deserialize, Debug, Tabled, Clone, PartialEq)]
@@ -33,19 +34,26 @@ pub struct Service {
3334
#[serde(default, skip_serializing_if = "is_default")]
3435
pub env: DisplayOption<DisplayHashMap>,
3536
#[serde(default, skip_serializing_if = "is_default")]
36-
pub secrets: DisplayOption<DisplayHashMap>
37+
pub secrets: DisplayOption<DisplayHashMap>,
3738
}
3839

3940
#[derive(Serialize, Deserialize, Debug)]
4041
pub struct ListSecretsResponse {
41-
pub secrets: Vec<Secret>
42+
pub secrets: Vec<Secret>,
4243
}
4344

4445
#[derive(Serialize, Deserialize, Debug, Tabled)]
4546
pub struct Secret {
4647
pub name: String,
4748
}
4849

50+
#[derive(Serialize, Deserialize, Debug)]
51+
pub struct LogLine {
52+
pub data: String,
53+
#[serde(with = "ts_seconds_option")]
54+
pub time: Option<DateTime<Utc>>,
55+
}
56+
4957
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
5058
pub struct DisplayHashMap(pub IndexMap<String, String>);
5159

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

6573
let hashmap = self.0.as_ref().unwrap();

src/commands/services.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tungstenite::connect;
1616
use tungstenite::http::Uri;
1717
use tungstenite::ClientRequestBuilder;
1818

19+
use crate::api::types::LogLine;
1920
use crate::api::types::{DisplayHashMap, DisplayOption, Service};
2021
use crate::api::APIClient;
2122

@@ -327,15 +328,21 @@ fn get_image_name(
327328

328329
return Ok(format!(
329330
"register.molnett.org/{}/{}:{}",
330-
org_id, image_name, image_tag.trim()
331+
org_id,
332+
image_name,
333+
image_tag.trim()
331334
));
332335
}
333336

334337
#[derive(Parser, Debug)]
335338
pub struct ImageName {
336339
#[arg(short, long, help = "Image tag to use")]
337340
tag: Option<String>,
338-
#[arg(short, long, help = "Path to a molnett manifest. The manifest's image field will be updated to the returned image name")]
341+
#[arg(
342+
short,
343+
long,
344+
help = "Path to a molnett manifest. The manifest's image field will be updated to the returned image name"
345+
)]
339346
update_manifest: Option<String>,
340347
}
341348

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

457464
loop {
458-
let msg = socket.read().expect("Error reading message");
459-
println!("{}", msg.to_string().trim_end());
465+
let rawmsg = socket.read().expect("Error reading message").to_string();
466+
let msg: LogLine = serde_json::from_str(rawmsg.as_str())?;
467+
468+
println!(
469+
"time=\"{}\" message=\"{}\"",
470+
msg.time.unwrap().format("%d/%m/%Y %H:%M:%S"),
471+
msg.data
472+
);
460473
}
461474
}
462475
}

0 commit comments

Comments
 (0)