Rust · 5940 bytes Raw Blame History
1 //! wanda config - Manage configuration
2
3 use clap::Subcommand;
4 use console::style;
5 use std::path::PathBuf;
6 use wanda_core::{config::WandaConfig, Result};
7
8 #[derive(Subcommand)]
9 pub enum ConfigCommands {
10 /// Display current configuration
11 Show,
12
13 /// Open config file in editor
14 Edit,
15
16 /// Set a configuration value
17 Set {
18 /// Configuration key (e.g., "proton.preferred_version")
19 key: String,
20 /// Value to set
21 value: String,
22 },
23
24 /// Reset configuration to defaults
25 Reset,
26
27 /// Show config file path
28 Path,
29 }
30
31 pub async fn run(cmd: ConfigCommands, config_path: Option<PathBuf>) -> Result<()> {
32 match cmd {
33 ConfigCommands::Show => show(config_path),
34 ConfigCommands::Edit => edit(config_path).await,
35 ConfigCommands::Set { key, value } => set(key, value, config_path),
36 ConfigCommands::Reset => reset(config_path),
37 ConfigCommands::Path => path(config_path),
38 }
39 }
40
41 fn show(config_path: Option<PathBuf>) -> Result<()> {
42 let config = match &config_path {
43 Some(path) => WandaConfig::load_from(path)?,
44 None => WandaConfig::load()?,
45 };
46
47 println!("WANDA Configuration:\n");
48
49 // Steam settings
50 println!("[steam]");
51 if let Some(ref path) = config.steam.install_path {
52 println!(" install_path = \"{}\"", path.display());
53 }
54 if !config.steam.additional_libraries.is_empty() {
55 println!(" additional_libraries = [");
56 for lib in &config.steam.additional_libraries {
57 println!(" \"{}\",", lib.display());
58 }
59 println!(" ]");
60 }
61 println!(" scan_flatpak = {}", config.steam.scan_flatpak);
62
63 // Proton settings
64 println!("\n[proton]");
65 if let Some(ref version) = config.proton.preferred_version {
66 println!(" preferred_version = \"{}\"", version);
67 }
68 if !config.proton.search_paths.is_empty() {
69 println!(" search_paths = [");
70 for path in &config.proton.search_paths {
71 println!(" \"{}\",", path.display());
72 }
73 println!(" ]");
74 }
75
76 // WeMod settings
77 println!("\n[wemod]");
78 println!(" auto_update = {}", config.wemod.auto_update);
79 if let Some(ref version) = config.wemod.version {
80 println!(" version = \"{}\"", version);
81 }
82
83 // Prefix settings
84 println!("\n[prefix]");
85 if let Some(ref path) = config.prefix.base_path {
86 println!(" base_path = \"{}\"", path.display());
87 }
88 println!(" auto_repair = {}", config.prefix.auto_repair);
89
90 Ok(())
91 }
92
93 async fn edit(config_path: Option<PathBuf>) -> Result<()> {
94 let path = config_path.unwrap_or_else(WandaConfig::default_path);
95
96 // Ensure config file exists
97 if !path.exists() {
98 println!("Config file doesn't exist, creating default...");
99 let config = WandaConfig::default();
100 config.save_to(&path)?;
101 }
102
103 // Try common editors
104 let editors = ["$EDITOR", "code", "nano", "vim", "vi"];
105
106 for editor in &editors {
107 let editor_cmd = if *editor == "$EDITOR" {
108 std::env::var("EDITOR").ok()
109 } else {
110 Some(editor.to_string())
111 };
112
113 if let Some(cmd) = editor_cmd {
114 let status = tokio::process::Command::new(&cmd)
115 .arg(&path)
116 .status()
117 .await;
118
119 if status.is_ok() {
120 return Ok(());
121 }
122 }
123 }
124
125 println!("Could not open editor. Config file is at:");
126 println!(" {}", path.display());
127
128 Ok(())
129 }
130
131 fn set(key: String, value: String, config_path: Option<PathBuf>) -> Result<()> {
132 let path = config_path.unwrap_or_else(WandaConfig::default_path);
133 let mut config = if path.exists() {
134 WandaConfig::load_from(&path)?
135 } else {
136 WandaConfig::default()
137 };
138
139 match key.as_str() {
140 "steam.install_path" => {
141 config.steam.install_path = Some(PathBuf::from(&value));
142 }
143 "steam.scan_flatpak" => {
144 config.steam.scan_flatpak = value.parse().unwrap_or(false);
145 }
146 "proton.preferred_version" => {
147 config.proton.preferred_version = Some(value.clone());
148 }
149 "wemod.auto_update" => {
150 config.wemod.auto_update = value.parse().unwrap_or(true);
151 }
152 "wemod.version" => {
153 config.wemod.version = Some(value.clone());
154 }
155 "prefix.base_path" => {
156 config.prefix.base_path = Some(PathBuf::from(&value));
157 }
158 "prefix.auto_repair" => {
159 config.prefix.auto_repair = value.parse().unwrap_or(true);
160 }
161 _ => {
162 println!("Unknown configuration key: {}", key);
163 println!("\nAvailable keys:");
164 println!(" steam.install_path");
165 println!(" steam.scan_flatpak");
166 println!(" proton.preferred_version");
167 println!(" wemod.auto_update");
168 println!(" wemod.version");
169 println!(" prefix.base_path");
170 println!(" prefix.auto_repair");
171 return Ok(());
172 }
173 }
174
175 config.save_to(&path)?;
176 println!("{} {} = {}", style("Set").green(), key, value);
177
178 Ok(())
179 }
180
181 fn reset(config_path: Option<PathBuf>) -> Result<()> {
182 let path = config_path.unwrap_or_else(WandaConfig::default_path);
183
184 println!(
185 "This will reset all configuration to defaults. Are you sure?"
186 );
187 println!("Config file: {}", path.display());
188 println!("\nRun with --force to confirm (not implemented yet).");
189
190 // For now, just create a default config
191 let config = WandaConfig::default();
192 config.save_to(&path)?;
193 println!("\nConfiguration reset to defaults.");
194
195 Ok(())
196 }
197
198 fn path(config_path: Option<PathBuf>) -> Result<()> {
199 let path = config_path.unwrap_or_else(WandaConfig::default_path);
200 println!("{}", path.display());
201 Ok(())
202 }
203