14
14
15
15
use std:: collections:: HashSet ;
16
16
17
+ use camino:: Utf8PathBuf ;
17
18
use clap:: Parser ;
18
19
use mas_config:: { ConfigurationSection , RootConfig , SyncConfig } ;
19
20
use mas_storage:: {
@@ -23,6 +24,7 @@ use mas_storage::{
23
24
use mas_storage_pg:: PgRepository ;
24
25
use rand:: SeedableRng ;
25
26
use sqlx:: { postgres:: PgAdvisoryLock , Acquire } ;
27
+ use tokio:: io:: AsyncWriteExt ;
26
28
use tracing:: { error, info, info_span, warn} ;
27
29
28
30
use crate :: util:: database_connection_from_config;
@@ -94,7 +96,13 @@ enum Subcommand {
94
96
Check ,
95
97
96
98
/// Generate a new config file
97
- Generate ,
99
+ Generate {
100
+ /// The path to the config file to generate
101
+ ///
102
+ /// If not specified, the config will be written to stdout
103
+ #[ clap( short, long) ]
104
+ output : Option < Utf8PathBuf > ,
105
+ } ,
98
106
99
107
/// Sync the clients and providers from the config file to the database
100
108
Sync {
@@ -128,14 +136,22 @@ impl Options {
128
136
info ! ( path = ?root. config, "Configuration file looks good" ) ;
129
137
}
130
138
131
- SC :: Generate => {
139
+ SC :: Generate { output } => {
132
140
let _span = info_span ! ( "cli.config.generate" ) . entered ( ) ;
133
141
134
142
// XXX: we should disallow SeedableRng::from_entropy
135
143
let rng = rand_chacha:: ChaChaRng :: from_entropy ( ) ;
136
144
let config = RootConfig :: load_and_generate ( rng) . await ?;
137
-
138
- serde_yaml:: to_writer ( std:: io:: stdout ( ) , & config) ?;
145
+ let config = serde_yaml:: to_string ( & config) ?;
146
+
147
+ if let Some ( output) = output {
148
+ info ! ( "Writing configuration to {output:?}" ) ;
149
+ let mut file = tokio:: fs:: File :: create ( output) . await ?;
150
+ file. write_all ( config. as_bytes ( ) ) . await ?;
151
+ } else {
152
+ info ! ( "Writing configuration to standard output" ) ;
153
+ tokio:: io:: stdout ( ) . write_all ( config. as_bytes ( ) ) . await ?;
154
+ }
139
155
}
140
156
141
157
SC :: Sync { prune, dry_run } => {
0 commit comments