mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-10-09 03:20:56 +00:00
DRY up telemetry ID loading and Telemetry construction
Extract read_existing_id() helper to deduplicate file-read-trim-check pattern, and extract private Telemetry::new() to consolidate for_server/for_cli. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f4b530c691
commit
6cee3e1926
2 changed files with 21 additions and 31 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use anyhow::{Context, Result};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use uuid::Uuid;
|
||||
|
||||
fn dot_id_path() -> Result<PathBuf> {
|
||||
|
|
@ -8,15 +8,18 @@ fn dot_id_path() -> Result<PathBuf> {
|
|||
Ok(home.join(".arc").join(".id"))
|
||||
}
|
||||
|
||||
fn read_existing_id(path: &Path) -> Option<String> {
|
||||
let contents = fs::read_to_string(path).ok()?;
|
||||
let id = contents.trim().to_string();
|
||||
(!id.is_empty()).then_some(id)
|
||||
}
|
||||
|
||||
/// Server: UUID persisted at ~/.arc/.id
|
||||
pub fn load_or_create_server_id() -> Result<String> {
|
||||
let path = dot_id_path()?;
|
||||
|
||||
if let Ok(contents) = fs::read_to_string(&path) {
|
||||
let id = contents.trim().to_string();
|
||||
if !id.is_empty() {
|
||||
return Ok(id);
|
||||
}
|
||||
if let Some(id) = read_existing_id(&path) {
|
||||
return Ok(id);
|
||||
}
|
||||
|
||||
let id = Uuid::new_v4().to_string();
|
||||
|
|
@ -34,13 +37,8 @@ pub fn load_or_create_server_id() -> Result<String> {
|
|||
|
||||
/// CLI: prefer existing ~/.arc/.id, else MD5 of MAC address
|
||||
pub fn compute_cli_id() -> Result<String> {
|
||||
let path = dot_id_path()?;
|
||||
|
||||
if let Ok(contents) = fs::read_to_string(&path) {
|
||||
let id = contents.trim().to_string();
|
||||
if !id.is_empty() {
|
||||
return Ok(id);
|
||||
}
|
||||
if let Some(id) = read_existing_id(&dot_id_path()?) {
|
||||
return Ok(id);
|
||||
}
|
||||
|
||||
let mac = mac_address::get_mac_address()
|
||||
|
|
|
|||
|
|
@ -24,28 +24,20 @@ pub struct Telemetry {
|
|||
}
|
||||
|
||||
impl Telemetry {
|
||||
pub fn for_server() -> Result<Self> {
|
||||
let level = telemetry_level();
|
||||
let anonymous_id = anonymous_id::load_or_create_server_id()?;
|
||||
let context = context::build_context();
|
||||
|
||||
Ok(Self {
|
||||
level,
|
||||
fn new(anonymous_id: String) -> Self {
|
||||
Self {
|
||||
level: telemetry_level(),
|
||||
anonymous_id,
|
||||
context,
|
||||
})
|
||||
context: context::build_context(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_server() -> Result<Self> {
|
||||
Ok(Self::new(anonymous_id::load_or_create_server_id()?))
|
||||
}
|
||||
|
||||
pub fn for_cli() -> Result<Self> {
|
||||
let level = telemetry_level();
|
||||
let anonymous_id = anonymous_id::compute_cli_id()?;
|
||||
let context = context::build_context();
|
||||
|
||||
Ok(Self {
|
||||
level,
|
||||
anonymous_id,
|
||||
context,
|
||||
})
|
||||
Ok(Self::new(anonymous_id::compute_cli_id()?))
|
||||
}
|
||||
|
||||
pub fn level(&self) -> &TelemetryLevel {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue