| 381 | } |
| 382 | |
| 383 | fn update_storage_file( |
| 384 | storage_file_path: &Path, |
| 385 | machine_id: &str, |
| 386 | mac_machine_id: &str, |
| 387 | dev_device_id: &str, |
| 388 | sqm_id: &str, |
| 389 | ) -> bool { |
| 390 | if !storage_file_path.exists() { |
| 391 | println!("{} Configuration file not found: {:?}", "[ERROR]".color(RED), storage_file_path); |
| 392 | println!("{} Please install and run Cursor once before using this script", "[TIP]".color(YELLOW)); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | let original_content = match fs::read_to_string(storage_file_path) { |
| 397 | Ok(content) => content, |
| 398 | Err(e) => { |
| 399 | println!("{} Failed to read configuration file {:?}: {}", "[ERROR]".color(RED), storage_file_path, e); |
| 400 | return false; |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | let mut config: Value = match serde_json::from_str(&original_content) { |
| 405 | Ok(json_value) => json_value, |
| 406 | Err(e) => { |
| 407 | println!("{} Failed to parse configuration file JSON: {}", "[ERROR]".color(RED), e); |
| 408 | // Attempt to restore original content is not applicable here as we haven't written yet |
| 409 | return false; |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | // Ensure the path to telemetry values exists or create it |
| 414 | // serde_json::Value uses `pointer_mut` for this kind of access. |
| 415 | // Example: /telemetry/machineId |
| 416 | // We need to ensure `config["telemetry"]` is an object. |
| 417 | if !config.get("telemetry").map_or(false, |v| v.is_object()) { |
| 418 | if config.as_object_mut().is_some() { // Check if config itself is an object |
| 419 | config["telemetry"] = serde_json::json!({}); |
| 420 | } else { |
| 421 | println!("{} Configuration root is not a JSON object. Cannot set telemetry.", "[ERROR]".color(RED)); |
| 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Update specific values |
| 427 | // Using .get_mut("telemetry") and then working with the resulting Option<&mut Value> |
| 428 | if let Some(telemetry) = config.get_mut("telemetry") { |
| 429 | if let Some(telemetry_obj) = telemetry.as_object_mut() { |
| 430 | telemetry_obj.insert("machineId".to_string(), Value::String(machine_id.to_string())); |
| 431 | telemetry_obj.insert("macMachineId".to_string(), Value::String(mac_machine_id.to_string())); |
| 432 | telemetry_obj.insert("devDeviceId".to_string(), Value::String(dev_device_id.to_string())); |
| 433 | telemetry_obj.insert("sqmId".to_string(), Value::String(sqm_id.to_string())); |
| 434 | } else { |
| 435 | println!("{} 'telemetry' field is not an object.", "[ERROR]".color(RED)); |
| 436 | return false; // Or attempt to restore original_content |
| 437 | } |
| 438 | } else { |
| 439 | // This case should ideally be covered by the creation logic above. |
| 440 | println!("{} Failed to access or create 'telemetry' object.", "[ERROR]".color(RED)); |