devalang_wasm/tools/cli/config/
telemetry.rs1#![cfg(feature = "cli")]
2
3use anyhow::{Context, Result};
4use serde::{Deserialize, Serialize};
5use std::fs;
6use std::path::PathBuf;
7
8#[derive(Debug, Deserialize, Serialize, Clone)]
9pub struct TelemetryConfig {
10 pub enabled: bool,
11}
12
13impl Default for TelemetryConfig {
14 fn default() -> Self {
15 Self { enabled: false }
16 }
17}
18
19fn get_telemetry_config_path() -> Result<PathBuf> {
20 let home = dirs::home_dir().context("Failed to find home directory")?;
21 Ok(home.join(".devalang").join("telemetry.json"))
22}
23
24pub fn is_telemetry_enabled() -> bool {
25 let Ok(config_path) = get_telemetry_config_path() else {
26 return false;
27 };
28
29 if !config_path.exists() {
30 return false;
31 }
32
33 match fs::read_to_string(&config_path) {
34 Ok(content) => match serde_json::from_str::<TelemetryConfig>(&content) {
35 Ok(config) => config.enabled,
36 Err(_) => false,
37 },
38 Err(_) => false,
39 }
40}
41
42pub fn enable_telemetry() -> Result<()> {
43 let config_path = get_telemetry_config_path()?;
44
45 if let Some(parent) = config_path.parent() {
47 fs::create_dir_all(parent)?;
48 }
49
50 let config = TelemetryConfig { enabled: true };
51 let json = serde_json::to_string_pretty(&config)?;
52 fs::write(&config_path, json)?;
53
54 Ok(())
55}
56
57pub fn disable_telemetry() -> Result<()> {
58 let config_path = get_telemetry_config_path()?;
59
60 if let Some(parent) = config_path.parent() {
62 fs::create_dir_all(parent)?;
63 }
64
65 let config = TelemetryConfig { enabled: false };
66 let json = serde_json::to_string_pretty(&config)?;
67 fs::write(&config_path, json)?;
68
69 Ok(())
70}
71
72pub fn get_telemetry_status() -> String {
73 if is_telemetry_enabled() {
74 "enabled".to_string()
75 } else {
76 "disabled".to_string()
77 }
78}