| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use std::path::Path; |
| 8 | use std::process::ExitCode; |
| 9 | |
| 10 | use clap::Subcommand; |
| 11 | |
| 12 | |
| 13 | #[derive(Debug, Subcommand)] |
| 14 | pub(crate) enum ConfigCommand { |
| 15 | |
| 16 | Check, |
| 17 | |
| 18 | Show, |
| 19 | } |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | pub(crate) fn run( |
| 28 | command: &ConfigCommand, |
| 29 | config_path: Option<&Path>, |
| 30 | json: bool, |
| 31 | ) -> anyhow::Result<ExitCode> { |
| 32 | match command { |
| 33 | ConfigCommand::Check => match config::load(config_path) { |
| 34 | Ok(loaded) => { |
| 35 | warn(&loaded.warnings); |
| 36 | println!("configuration ok"); |
| 37 | Ok(ExitCode::SUCCESS) |
| 38 | } |
| 39 | Err(err) => { |
| 40 | eprintln!("error: {err}"); |
| 41 | Ok(ExitCode::FAILURE) |
| 42 | } |
| 43 | }, |
| 44 | ConfigCommand::Show => { |
| 45 | let loaded = config::load(config_path)?; |
| 46 | warn(&loaded.warnings); |
| 47 | if json { |
| 48 | println!("{}", serde_json::to_string_pretty(&loaded.config)?); |
| 49 | } else { |
| 50 | print!("{}", loaded.config.to_toml_string()?); |
| 51 | } |
| 52 | Ok(ExitCode::SUCCESS) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | fn warn(warnings: &[String]) { |
| 59 | for warning in warnings { |
| 60 | eprintln!("warning: {warning}"); |
| 61 | } |
| 62 | } |