Skip to content

Commit 088a416

Browse files
authored
Merge branch 'main' into chore/toolchain-update
2 parents 58d549b + d63b288 commit 088a416

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

tailcall-tracker/src/dispatch.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
use std::collections::HashSet;
2+
use std::process::Output;
3+
14
use chrono::{DateTime, Utc};
25
use machineid_rs::{Encryption, HWIDComponent, IdBuilder};
36
use sysinfo::System;
7+
use tokio::process::Command;
8+
use tokio::sync::Mutex;
49
use tokio::time::Duration;
510

611
use super::Result;
@@ -29,6 +34,7 @@ pub struct Tracker {
2934
collectors: Vec<Box<dyn Collect>>,
3035
can_track: bool,
3136
start_time: DateTime<Utc>,
37+
email: Mutex<Option<Vec<String>>>,
3238
}
3339

3440
impl Default for Tracker {
@@ -44,6 +50,7 @@ impl Default for Tracker {
4450
collectors: vec![ga_tracker, posthog_tracker],
4551
can_track,
4652
start_time,
53+
email: Mutex::new(None),
4754
}
4855
}
4956
}
@@ -74,6 +81,7 @@ impl Tracker {
7481
cwd: cwd(),
7582
user: user(),
7683
version: version(),
84+
email: self.email().await.clone(),
7785
};
7886

7987
// Dispatch the event to all collectors
@@ -86,6 +94,74 @@ impl Tracker {
8694

8795
Ok(())
8896
}
97+
98+
async fn email(&'static self) -> Vec<String> {
99+
let mut guard = self.email.lock().await;
100+
if guard.is_none() {
101+
*guard = Some(email().await.into_iter().collect());
102+
}
103+
guard.clone().unwrap_or_default()
104+
}
105+
}
106+
107+
// Get the email address
108+
async fn email() -> HashSet<String> {
109+
fn parse(output: Output) -> Option<String> {
110+
if output.status.success() {
111+
let text = String::from_utf8_lossy(&output.stdout).trim().to_string();
112+
if !text.is_empty() {
113+
return Some(text);
114+
}
115+
}
116+
117+
None
118+
}
119+
120+
// From Git
121+
async fn git() -> Option<String> {
122+
let output = Command::new("git")
123+
.args(["config", "--global", "user.email"])
124+
.output()
125+
.await
126+
.ok()?;
127+
128+
parse(output)
129+
}
130+
131+
// From SSH Keys
132+
async fn ssh() -> Option<HashSet<String>> {
133+
// Single command to find all unique email addresses from .pub files
134+
let output = Command::new("sh")
135+
.args([
136+
"-c",
137+
"cat ~/.ssh/*.pub | grep -o '[^ ]\\+@[^ ]\\+\\.[^ ]\\+'",
138+
])
139+
.output()
140+
.await
141+
.ok()?;
142+
143+
Some(parse(output)?.lines().map(|o| o.to_owned()).collect())
144+
}
145+
146+
let git_email = git().await;
147+
let ssh_emails = ssh().await;
148+
let mut email_ids = HashSet::new();
149+
150+
if let Some(email) = git_email {
151+
if !email.trim().is_empty() {
152+
email_ids.insert(email.trim().to_string());
153+
}
154+
}
155+
156+
if let Some(emails) = ssh_emails {
157+
for email in emails {
158+
if !email.trim().is_empty() {
159+
email_ids.insert(email.trim().to_string());
160+
}
161+
}
162+
}
163+
164+
email_ids
89165
}
90166

91167
// Generates a random client ID
@@ -141,6 +217,7 @@ fn os_name() -> String {
141217

142218
#[cfg(test)]
143219
mod tests {
220+
144221
use lazy_static::lazy_static;
145222

146223
use super::*;

tailcall-tracker/src/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Event {
1717
pub user: String,
1818
pub args: Vec<String>,
1919
pub version: String,
20+
pub email: Vec<String>,
2021
}
2122

2223
#[derive(Clone, Debug, Serialize, Deserialize)]

0 commit comments

Comments
 (0)