/ src / bin / cmd / config.rs
config.rs
 1  use std::path::PathBuf;
 2  
 3  use ambient_ci::runlog::RunLog;
 4  use clap::Parser;
 5  
 6  use super::{AmbientError, Config, Leaf};
 7  
 8  /// Show configuration to user.
 9  #[derive(Debug, Parser)]
10  pub struct ConfigCmd {
11      #[clap(long)]
12      oneline: bool,
13  
14      #[clap(long)]
15      output: Option<PathBuf>,
16  }
17  
18  impl Leaf for ConfigCmd {
19      fn run(&self, config: &Config, _runlog: &mut RunLog) -> Result<(), AmbientError> {
20          let json = if self.oneline {
21              serde_json::to_string(config).map_err(ConfigError::ToJson)?
22          } else {
23              serde_json::to_string_pretty(config).map_err(ConfigError::ToJson)?
24          };
25  
26          if let Some(filename) = &self.output {
27              std::fs::write(filename, json.as_bytes())
28                  .map_err(|err| ConfigError::Write(filename.into(), err))?
29          } else {
30              println!("{json}");
31          }
32  
33          Ok(())
34      }
35  }
36  
37  #[derive(Debug, thiserror::Error)]
38  pub enum ConfigError {
39      #[error("failed to serialize configuration to JSON")]
40      ToJson(#[source] serde_json::Error),
41  
42      #[error("failed to write output to {0}")]
43      Write(PathBuf, #[source] std::io::Error),
44  }