From cc10270ede85e3a55a15dae145c768b78a5010bc Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Fri, 24 Nov 2023 10:52:01 +0100 Subject: [PATCH] Allow `config generate` to generate to a file directly instead of stdout --- crates/cli/src/commands/config.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/cli/src/commands/config.rs b/crates/cli/src/commands/config.rs index 5030cf2c..114116a4 100644 --- a/crates/cli/src/commands/config.rs +++ b/crates/cli/src/commands/config.rs @@ -14,6 +14,7 @@ use std::collections::HashSet; +use camino::Utf8PathBuf; use clap::Parser; use mas_config::{ConfigurationSection, RootConfig, SyncConfig}; use mas_storage::{ @@ -23,6 +24,7 @@ use mas_storage::{ use mas_storage_pg::PgRepository; use rand::SeedableRng; use sqlx::{postgres::PgAdvisoryLock, Acquire}; +use tokio::io::AsyncWriteExt; use tracing::{error, info, info_span, warn}; use crate::util::database_connection_from_config; @@ -94,7 +96,13 @@ enum Subcommand { Check, /// Generate a new config file - Generate, + Generate { + /// The path to the config file to generate + /// + /// If not specified, the config will be written to stdout + #[clap(short, long)] + output: Option, + }, /// Sync the clients and providers from the config file to the database Sync { @@ -128,14 +136,22 @@ impl Options { info!(path = ?root.config, "Configuration file looks good"); } - SC::Generate => { + SC::Generate { output } => { let _span = info_span!("cli.config.generate").entered(); // XXX: we should disallow SeedableRng::from_entropy let rng = rand_chacha::ChaChaRng::from_entropy(); let config = RootConfig::load_and_generate(rng).await?; + let config = serde_yaml::to_string(&config)?; - serde_yaml::to_writer(std::io::stdout(), &config)?; + if let Some(output) = output { + info!("Writing configuration to {output:?}"); + let mut file = tokio::fs::File::create(output).await?; + file.write_all(config.as_bytes()).await?; + } else { + info!("Writing configuration to standard output"); + tokio::io::stdout().write_all(config.as_bytes()).await?; + } } SC::Sync { prune, dry_run } => {