1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::cli::parser::Commands;
use crate::config::{Config, migrate};
use crate::errors::{AppError, AppResult};
use crate::ui::messages::{error, info, success, warning};
use std::process::Command;
/// Handle the `config` subcommand
pub fn handle(cmd: &Commands, cfg: &Config) -> AppResult<()> {
if let Commands::Config {
print_config,
check,
migrate,
edit_config,
editor,
} = cmd
{
let path = Config::config_file();
// ------------------------------------------------------------
// PRINT CONFIG
// ------------------------------------------------------------
if *print_config {
info("Current configuration:");
println!("{}", serde_yaml::to_string(&cfg).unwrap());
}
// ------------------------------------------------------------
// CHECK CONFIG
// ------------------------------------------------------------
if *check {
info("🔧 Checking configuration…");
let cfg = Config::load();
info(format!("Config file: {:?}", Config::config_file()));
info(format!("Database : {:?}", cfg.database));
let db_exists = std::path::Path::new(&cfg.database).exists();
if !db_exists {
warning("âš Database file is missing.");
} else {
success("✔ Database file exists.");
}
// qui puoi aggiungere altre verifiche, tipo valori malformati ecc.
return Ok(());
}
// ------------------------------------------------------------
// MIGRATE CONFIG
// ------------------------------------------------------------
if *migrate {
info("🔧 Running configuration migration…");
match migrate::run_fs_migration() {
Ok(_) => success("✔ Filesystem migration completed."),
Err(e) => error(format!("Migration error: {}", e)),
}
return Ok(());
}
// ------------------------------------------------------------
// EDIT CONFIG
// ------------------------------------------------------------
if *edit_config {
// Requested editor via --editor
let requested_editor = editor.clone();
// Determine default editor
let default_editor = std::env::var("EDITOR")
.or_else(|_| std::env::var("VISUAL"))
.unwrap_or_else(|_| {
if cfg!(target_os = "windows") {
"notepad".to_string()
} else {
"nano".to_string()
}
});
// If --editor supplied → use it, otherwise fallback to default
let editor_to_use = requested_editor.unwrap_or_else(|| default_editor.clone());
info(format!(
"Opening configuration file with editor '{}'",
editor_to_use
));
// Try primary editor
let status = Command::new(&editor_to_use).arg(&path).status();
match status {
Ok(s) if s.success() => {
success(format!(
"Configuration file edited successfully using '{}'.",
editor_to_use
));
}
// Editor not usable → fallback
Ok(_) | Err(_) => {
warning(format!(
"Editor '{}' not available or failed to start. Falling back to '{}'.",
editor_to_use, default_editor
));
let fallback_status = Command::new(&default_editor).arg(&path).status();
match fallback_status {
Ok(s) if s.success() => {
success(format!(
"Configuration file edited successfully using fallback editor '{}'.",
default_editor
));
}
Ok(_) | Err(_) => {
return Err(AppError::InvalidOperation(format!(
"Unable to edit configuration file.\nAttempted editors:\n • Primary: '{}'\n • Fallback: '{}'\nBoth failed to start or exited with an error.",
editor_to_use, default_editor
)));
}
}
}
}
}
}
Ok(())
}