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 Apr 5, 2024
1 parent bd15a98 commit 40a7743
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 52 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(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
11 changes: 9 additions & 2 deletions src/commands/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tungstenite::connect;
use tungstenite::http::Uri;
use tungstenite::ClientRequestBuilder;

use crate::api::types::LogLine;
use crate::{
api::types::Service,
config::{
Expand Down Expand Up @@ -422,8 +423,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 40a7743

Please sign in to comment.