devalang_wasm/tools/cli/config/
telemetry.rs

1#![cfg(feature = "cli")]
2
3use anyhow::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() -> PathBuf {
20    let home = dirs::home_dir().expect("Failed to find home directory");
21    home.join(".devalang").join("telemetry.json")
22}
23
24pub fn is_telemetry_enabled() -> bool {
25    let config_path = get_telemetry_config_path();
26
27    if !config_path.exists() {
28        return false;
29    }
30
31    match fs::read_to_string(&config_path) {
32        Ok(content) => match serde_json::from_str::<TelemetryConfig>(&content) {
33            Ok(config) => config.enabled,
34            Err(_) => false,
35        },
36        Err(_) => false,
37    }
38}
39
40pub fn enable_telemetry() -> Result<()> {
41    let config_path = get_telemetry_config_path();
42
43    // Ensure directory exists
44    if let Some(parent) = config_path.parent() {
45        fs::create_dir_all(parent)?;
46    }
47
48    let config = TelemetryConfig { enabled: true };
49    let json = serde_json::to_string_pretty(&config)?;
50    fs::write(&config_path, json)?;
51
52    Ok(())
53}
54
55pub fn disable_telemetry() -> Result<()> {
56    let config_path = get_telemetry_config_path();
57
58    // Ensure directory exists
59    if let Some(parent) = config_path.parent() {
60        fs::create_dir_all(parent)?;
61    }
62
63    let config = TelemetryConfig { enabled: false };
64    let json = serde_json::to_string_pretty(&config)?;
65    fs::write(&config_path, json)?;
66
67    Ok(())
68}
69
70pub fn get_telemetry_status() -> String {
71    if is_telemetry_enabled() {
72        "enabled".to_string()
73    } else {
74        "disabled".to_string()
75    }
76}