| 186 | |
| 187 | impl SettingsArgs { |
| 188 | pub async fn execute(&self, os: &mut Os) -> Result<ExitCode> { |
| 189 | match self.cmd { |
| 190 | Some(SettingsSubcommands::Open) => { |
| 191 | let file = GlobalPaths::settings_path().context("Could not get settings path")?; |
| 192 | let editor = |
| 193 | crate::util::env_var::try_get_editor().context("The EDITOR environment variable is not set")?; |
| 194 | tokio::process::Command::new(editor).arg(file).spawn()?.wait().await?; |
| 195 | Ok(ExitCode::SUCCESS) |
| 196 | }, |
| 197 | Some(SettingsSubcommands::List { all, format, state }) => { |
| 198 | if state { |
| 199 | print_state_dump(os, format)?; |
| 200 | } else if all { |
| 201 | print_all_settings(os, format)?; |
| 202 | } else { |
| 203 | print_configured_settings(os, format)?; |
| 204 | } |
| 205 | Ok(ExitCode::SUCCESS) |
| 206 | }, |
| 207 | Some(SettingsSubcommands::All { format, state }) => { |
| 208 | // Deprecated: redirect to List behavior for backward compatibility |
| 209 | if state { |
| 210 | print_state_dump(os, format)?; |
| 211 | } else { |
| 212 | print_configured_settings(os, format)?; |
| 213 | } |
| 214 | Ok(ExitCode::SUCCESS) |
| 215 | }, |
| 216 | None => { |
| 217 | let Some(key) = &self.key else { |
| 218 | if self.delete { |
| 219 | return Err(eyre::eyre!( |
| 220 | "the argument {} requires a {}\n Usage: q settings {} {}", |
| 221 | "'--delete'".yellow(), |
| 222 | "<KEY>".green(), |
| 223 | "--delete".yellow(), |
| 224 | "<KEY>".green() |
| 225 | )); |
| 226 | } |
| 227 | return Ok(ExitCode::SUCCESS); |
| 228 | }; |
| 229 | |
| 230 | let key = Setting::try_from(key.as_str())?; |
| 231 | match (&self.value, self.delete) { |
| 232 | (Some(_), true) => Err(eyre::eyre!( |
| 233 | "the argument {} cannot be used with {}\n Usage: q settings {} {key}", |
| 234 | "'--delete'".yellow(), |
| 235 | "'[VALUE]'".yellow(), |
| 236 | "--delete".yellow() |
| 237 | )), |
| 238 | (None, false) => match os.database.settings.get(key) { |
| 239 | Some(value) => { |
| 240 | match self.format { |
| 241 | OutputFormat::Plain => match value.as_str() { |
| 242 | Some(value) => println!("{value}"), |
| 243 | None => println!("{value:#}"), |
| 244 | }, |
| 245 | OutputFormat::Json => println!("{value}"), |