1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-08-06 06:02:40 +03:00

Allow config generate to generate to a file directly instead of stdout

This commit is contained in:
Quentin Gliech
2023-11-24 10:52:01 +01:00
parent 1010945905
commit cc10270ede

View File

@@ -14,6 +14,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use camino::Utf8PathBuf;
use clap::Parser; use clap::Parser;
use mas_config::{ConfigurationSection, RootConfig, SyncConfig}; use mas_config::{ConfigurationSection, RootConfig, SyncConfig};
use mas_storage::{ use mas_storage::{
@@ -23,6 +24,7 @@ use mas_storage::{
use mas_storage_pg::PgRepository; use mas_storage_pg::PgRepository;
use rand::SeedableRng; use rand::SeedableRng;
use sqlx::{postgres::PgAdvisoryLock, Acquire}; use sqlx::{postgres::PgAdvisoryLock, Acquire};
use tokio::io::AsyncWriteExt;
use tracing::{error, info, info_span, warn}; use tracing::{error, info, info_span, warn};
use crate::util::database_connection_from_config; use crate::util::database_connection_from_config;
@@ -94,7 +96,13 @@ enum Subcommand {
Check, Check,
/// Generate a new config file /// 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<Utf8PathBuf>,
},
/// Sync the clients and providers from the config file to the database /// Sync the clients and providers from the config file to the database
Sync { Sync {
@@ -128,14 +136,22 @@ impl Options {
info!(path = ?root.config, "Configuration file looks good"); info!(path = ?root.config, "Configuration file looks good");
} }
SC::Generate => { SC::Generate { output } => {
let _span = info_span!("cli.config.generate").entered(); let _span = info_span!("cli.config.generate").entered();
// XXX: we should disallow SeedableRng::from_entropy // XXX: we should disallow SeedableRng::from_entropy
let rng = rand_chacha::ChaChaRng::from_entropy(); let rng = rand_chacha::ChaChaRng::from_entropy();
let config = RootConfig::load_and_generate(rng).await?; 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 } => { SC::Sync { prune, dry_run } => {