|
1 | | -use anyhow::{Context, Result}; |
2 | | -use serde::{Deserialize, Serialize}; |
3 | | - |
| 1 | +use anyhow::Result; |
4 | 2 | use std::collections::HashMap; |
5 | | -use std::path::PathBuf; |
6 | 3 | use std::future::Future; |
7 | 4 | use std::error::Error; |
| 5 | +use uuid::Uuid; |
8 | 6 |
|
9 | | -use crate::utils::{version, META_OS}; |
| 7 | +use crate::utils::META_OS; |
| 8 | +use crate::version::version; |
| 9 | +use crate::cli::config; |
10 | 10 |
|
11 | 11 | use super::{Args, TelemetryCommand}; |
12 | 12 |
|
13 | 13 | const API_KEY: &str = "L-AQtrFVtZGL_PjK2FbFLBR3oXNtfv8OrCD8ObyeBQo"; |
14 | 14 | const EVENT_NAME: &str = "synth-command"; |
15 | 15 |
|
16 | 16 | pub(crate) fn enable() -> Result<()> { |
17 | | - TelemetryConfig::enable_telemetry() |
| 17 | + // Initialise the `uuid` if it hasn't been initialised yet. |
| 18 | + let _ = get_or_initialise_uuid(); |
| 19 | + Ok(config::set_telemetry_enabled(true)) |
18 | 20 | } |
19 | 21 |
|
20 | 22 | pub(crate) fn disable() -> Result<()> { |
21 | | - TelemetryConfig::disable_telemetry() |
| 23 | + Ok(config::set_telemetry_enabled(false)) |
22 | 24 | } |
23 | 25 |
|
24 | 26 | pub(crate) fn is_enabled() -> bool { |
25 | | - TelemetryConfig::is_enabled() |
| 27 | + config::get_telemetry_enabled().unwrap_or(false) |
| 28 | +} |
| 29 | + |
| 30 | +fn get_or_initialise_uuid() -> String { |
| 31 | + if config::get_uuid().is_none() { |
| 32 | + config::set_uuid(Uuid::new_v4().to_hyphenated().to_string()); |
| 33 | + } |
| 34 | + config::get_uuid().expect("is ok here as was set earlier") |
26 | 35 | } |
27 | 36 |
|
28 | 37 | pub async fn with_telemetry<F, Fut, T, E>(args: Args, func: F) -> Result<T, E> |
|
39 | 48 | Args::Import { .. } => "import", |
40 | 49 | Args::Telemetry(TelemetryCommand::Enable) => "telemetry::enable", |
41 | 50 | Args::Telemetry(TelemetryCommand::Disable) => "telemetry::disable", |
42 | | - Args::Telemetry(TelemetryCommand::Status) => "telemetry::status" |
| 51 | + Args::Telemetry(TelemetryCommand::Status) => "telemetry::status", |
| 52 | + Args::Version => "version" |
43 | 53 | }; |
44 | 54 |
|
45 | 55 | func(args) |
|
48 | 58 | .or_else(|err| client.failed(command_name, err)) |
49 | 59 | } |
50 | 60 |
|
51 | | -#[derive(Serialize, Deserialize)] |
52 | | -struct TelemetryConfig { |
53 | | - uuid: String, |
54 | | -} |
55 | | - |
56 | | -impl TelemetryConfig { |
57 | | - pub fn initialise() -> Self { |
58 | | - Self::from_file().unwrap_or_else(|_| Self::new()) |
59 | | - } |
60 | | - |
61 | | - fn new() -> Self { |
62 | | - Self { |
63 | | - uuid: uuid::Uuid::new_v4().to_hyphenated().to_string(), |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - fn from_file() -> Result<Self> { |
68 | | - let file_contents = std::fs::read_to_string(Self::file_path()?)?; |
69 | | - let tc = serde_json::from_str(&file_contents)?; |
70 | | - Ok(tc) |
71 | | - } |
72 | | - |
73 | | - fn synth_config_dir() -> Result<PathBuf> { |
74 | | - let synth_config_dir = dirs::config_dir().ok_or_else(|| { |
75 | | - anyhow!( |
76 | | - "Could not find a configuration directory. Your operating system may not be supported." |
77 | | - ) |
78 | | - })?; |
79 | | - Ok(synth_config_dir.join("synth")) |
80 | | - } |
81 | | - |
82 | | - fn file_path() -> Result<PathBuf> { |
83 | | - Ok(Self::synth_config_dir()?.join("config.json")) |
84 | | - } |
85 | | - |
86 | | - fn enable_telemetry() -> Result<()> { |
87 | | - let config_dir = Self::synth_config_dir()?; |
88 | | - if !config_dir.exists() { |
89 | | - std::fs::create_dir_all(&config_dir) |
90 | | - .with_context(|| anyhow!("Could not create the directory: {}", config_dir.display()))?; |
91 | | - } |
92 | | - if !Self::is_enabled() { |
93 | | - let mut config_file_path = std::fs::OpenOptions::new() |
94 | | - .write(true) |
95 | | - .create(true) |
96 | | - .open(Self::file_path()?) |
97 | | - .map_err(|e| anyhow!("There was an issue enabling telemetry: {}", e))?; |
98 | | - serde_json::to_writer_pretty(&mut config_file_path, &TelemetryConfig::new())?; |
99 | | - } |
100 | | - Ok(()) |
101 | | - } |
102 | | - |
103 | | - fn disable_telemetry() -> Result<()> { |
104 | | - if Self::is_enabled() { |
105 | | - std::fs::remove_file(Self::file_path()?) |
106 | | - .map_err(|e| anyhow!("There was an issue disabling telemetry: {}", e))?; |
107 | | - } |
108 | | - Ok(()) |
109 | | - } |
110 | | - |
111 | | - fn is_enabled() -> bool { |
112 | | - Self::file_path().map(|path| path.exists()).unwrap_or(false) |
113 | | - } |
114 | | -} |
115 | | - |
116 | 61 | enum CommandResult { |
117 | 62 | Success, |
118 | 63 | Failed, |
@@ -142,10 +87,10 @@ impl TelemetryClient { |
142 | 87 |
|
143 | 88 | Self { |
144 | 89 | ph_client: posthog_rs::client(API_KEY), |
145 | | - uuid: TelemetryConfig::initialise().uuid, |
| 90 | + uuid: get_or_initialise_uuid(), |
146 | 91 | synth_version, |
147 | 92 | os, |
148 | | - enabled: TelemetryConfig::is_enabled(), |
| 93 | + enabled: is_enabled(), |
149 | 94 | } |
150 | 95 | } |
151 | 96 |
|
|
0 commit comments