Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include timestamp on log print #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions Cargo.lock

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

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
25 changes: 20 additions & 5 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 @@ -433,7 +440,9 @@ impl Logs {
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;

let manifest = read_manifest(&self.manifest)?;
let manifest = read_manifest(&self.manifest)
.expect(format!("Could not read manifest file {}", self.manifest).as_str());

let logurl: Uri = url::Url::parse(
format!(
"{}/orgs/{}/envs/{}/svcs/{}/logs",
Expand All @@ -455,8 +464,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
Loading