fur_cli/schema/
migrate.rs1use std::fs;
2use std::path::Path;
3
4use serde_json::Value;
5use walkdir::WalkDir;
6
7use colored::*;
8use std::io::{stdin, stdout, Write};
9
10use crate::schema::upgrade_message_schema;
11
12use crate::schema::CURRENT_SCHEMA;
13
14pub fn detect_old_schema() -> bool {
15 let messages_dir = std::path::Path::new(".fur/messages");
16
17 if !messages_dir.exists() {
18 return false;
19 }
20
21 for entry in std::fs::read_dir(messages_dir).unwrap() {
22 let path = entry.unwrap().path();
23
24 if !path.is_file() {
25 continue;
26 }
27
28 if let Ok(content) = std::fs::read_to_string(&path) {
29 if let Ok(msg) = serde_json::from_str::<serde_json::Value>(&content) {
30 let schema = msg["schema_version"].as_str().unwrap_or("0.1");
31
32 if schema != CURRENT_SCHEMA {
33 return true;
34 }
35 }
36 }
37 }
38
39 false
40}
41
42pub fn ask_yes_no(question: &str) -> bool {
43 print!("{} [Y/n]: ", question.bold().bright_yellow());
44 stdout().flush().unwrap();
45
46 let mut input = String::new();
47 stdin().read_line(&mut input).unwrap();
48
49 let input = input.trim().to_lowercase();
50
51 input.is_empty() || input == "y" || input == "yes"
52}
53
54pub fn run_backfill_meta() {
55 let messages_dir = Path::new(".fur/messages");
56
57 println!("\nš§ Running metadata migration...\n");
58
59 let mut updated = 0;
60
61 for entry in WalkDir::new(messages_dir)
62 .into_iter()
63 .filter_map(|e| e.ok())
64 {
65 let path = entry.path();
66
67 if !path.is_file() {
68 continue;
69 }
70
71 let content = fs::read_to_string(path).unwrap();
72
73 let mut msg: Value = serde_json::from_str(&content).unwrap();
74
75 if upgrade_message_schema(&mut msg) {
76 fs::write(path, serde_json::to_string_pretty(&msg).unwrap()).unwrap();
77
78 updated += 1;
79 }
80 }
81
82 println!("ā Metadata backfilled for {}\n", updated);
83}