Skip to content

Commit

Permalink
refactor: fix file read operation (#3079)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <[email protected]>
  • Loading branch information
laststylebender14 and tusharmath authored Oct 29, 2024
1 parent ce97477 commit 0e13d7d
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions tailcall-tracker/src/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashSet;
use std::process::Output;
use tokio::sync::Mutex;

use chrono::{DateTime, Utc};
use machineid_rs::{Encryption, HWIDComponent, IdBuilder};
Expand Down Expand Up @@ -33,6 +34,7 @@ pub struct Tracker {
collectors: Vec<Box<dyn Collect>>,
can_track: bool,
start_time: DateTime<Utc>,
email: Mutex<Option<Vec<String>>>,
}

impl Default for Tracker {
Expand All @@ -48,6 +50,7 @@ impl Default for Tracker {
collectors: vec![ga_tracker, posthog_tracker],
can_track,
start_time,
email: Mutex::new(None),
}
}
}
Expand Down Expand Up @@ -78,7 +81,7 @@ impl Tracker {
cwd: cwd(),
user: user(),
version: version(),
email: email().await.into_iter().collect(),
email: self.email().await.clone(),
};

// Dispatch the event to all collectors
Expand All @@ -91,6 +94,14 @@ impl Tracker {

Ok(())
}

async fn email(&'static self) -> Vec<String> {
let mut guard = self.email.lock().await;
if guard.is_none() {
*guard = Some(email().await.into_iter().collect());
}
guard.clone().unwrap_or_default()
}
}

// Get the email address
Expand All @@ -103,13 +114,13 @@ async fn email() -> HashSet<String> {
}
}

return None;
None
}

// From Git
async fn git() -> Option<String> {
let output = Command::new("git")
.args(&["config", "--global", "user.email"])
.args(["config", "--global", "user.email"])
.output()
.await
.ok()?;
Expand All @@ -118,39 +129,39 @@ async fn email() -> HashSet<String> {
}

// From SSH Keys
async fn ssh() -> Option<String> {
let output = Command::new("cat")
.args(&["~/.ssh/id_rsa.pub"])
async fn ssh() -> Option<HashSet<String>> {
// Single command to find all unique email addresses from .pub files
let output = Command::new("sh")
.args([
"-c",
"cat ~/.ssh/*.pub | grep -o '[^ ]\\+@[^ ]\\+\\.[^ ]\\+'",
])
.output()
.await
.ok()?;

let pub_key = parse(output)?;
Some(parse(output)?.lines().map(|o| o.to_owned()).collect())
}

let parts: Vec<&str> = pub_key.trim().split_whitespace().collect();
let git_email = git().await;
let ssh_emails = ssh().await;
let mut email_ids = HashSet::new();

// SSH public keys typically have at least three parts
if parts.len() < 3 {
return None;
if let Some(email) = git_email {
if !email.trim().is_empty() {
email_ids.insert(email.trim().to_string());
}
}

// The comment part is usually the third element
let comment = parts.get(2)?;

// Optional: Validate the email format using a simple check
if comment.contains('@') && comment.contains('.') {
Some(comment.to_string())
} else {
None
if let Some(emails) = ssh_emails {
for email in emails {
if !email.trim().is_empty() {
email_ids.insert(email.trim().to_string());
}
}
}

[git().await, ssh().await]
.into_iter()
.flatten()
.map(|a| a.trim().to_string())
.filter(|a| !a.is_empty())
.collect::<HashSet<_>>()
email_ids
}

// Generates a random client ID
Expand Down Expand Up @@ -206,8 +217,8 @@ fn os_name() -> String {

#[cfg(test)]
mod tests {

use lazy_static::lazy_static;
use std::process::Command;

use super::*;

Expand Down

0 comments on commit 0e13d7d

Please sign in to comment.