22use crate :: input:: { load_commodity_graphs, load_model} ;
33use crate :: log;
44use crate :: output:: graph:: save_commodity_graphs_for_model;
5- use crate :: output:: { create_output_directory, get_output_dir} ;
5+ use crate :: output:: { create_output_directory, get_graphs_dir , get_output_dir} ;
66use crate :: settings:: Settings ;
77use :: log:: { info, warn} ;
88use anyhow:: { Context , Result } ;
@@ -67,8 +67,9 @@ enum Commands {
6767 BuildCommodityGraphs {
6868 /// The path to the model directory.
6969 model_dir : PathBuf ,
70- /// Output path
71- output_dir : PathBuf ,
70+ /// Other options (uses the same settings as the `run` command)
71+ #[ command( flatten) ]
72+ opts : RunOpts ,
7273 } ,
7374 /// Manage settings file.
7475 Settings {
@@ -85,10 +86,9 @@ impl Commands {
8586 Self :: Run { model_dir, opts } => handle_run_command ( & model_dir, & opts, None ) ,
8687 Self :: Example { subcommand } => subcommand. execute ( ) ,
8788 Self :: Validate { model_dir } => handle_validate_command ( & model_dir, None ) ,
88- Self :: BuildCommodityGraphs {
89- model_dir,
90- output_dir,
91- } => handle_build_commodity_graphs_command ( & model_dir, & output_dir, None ) ,
89+ Self :: BuildCommodityGraphs { model_dir, opts } => {
90+ handle_build_commodity_graphs_command ( & model_dir, & opts, None )
91+ }
9292 Self :: Settings { subcommand } => subcommand. execute ( ) ,
9393 }
9494 }
@@ -194,21 +194,52 @@ pub fn handle_validate_command(model_path: &Path, settings: Option<Settings>) ->
194194/// Handle the `build-commodity-graphs` command.
195195pub fn handle_build_commodity_graphs_command (
196196 model_path : & Path ,
197- output_path : & Path ,
197+ opts : & RunOpts ,
198198 settings : Option < Settings > ,
199199) -> Result < ( ) > {
200200 // Load program settings, if not provided
201- let settings = if let Some ( settings) = settings {
201+ let mut settings = if let Some ( settings) = settings {
202202 settings
203203 } else {
204204 Settings :: load ( ) . context ( "Failed to load settings." ) ?
205205 } ;
206206
207- // Initialise program logger (we won't save log files when running the validate command)
207+ // These settings can be overridden by command-line arguments
208+ if opts. debug_model {
209+ settings. debug_model = true ;
210+ }
211+ if opts. overwrite {
212+ settings. overwrite = true ;
213+ }
214+
215+ // Get path to output folder
216+ let pathbuf: PathBuf ;
217+ let output_path = if let Some ( p) = opts. output_dir . as_deref ( ) {
218+ p
219+ } else {
220+ pathbuf = get_graphs_dir ( model_path) ?;
221+ & pathbuf
222+ } ;
223+
224+ let overwrite =
225+ create_output_directory ( output_path, settings. overwrite ) . with_context ( || {
226+ format ! (
227+ "Failed to create commodity flow graphs directory: {}" ,
228+ output_path. display( )
229+ )
230+ } ) ?;
231+
232+ // Initialise program logger (we won't save log files when running this command
208233 log:: init ( & settings. log_level , None ) . context ( "Failed to initialise logging." ) ?;
209234
235+ // NB: We have to wait until the logger is initialised to display this warning
236+ if overwrite {
237+ warn ! ( "Commodity flow graphs directory will be overwritten" ) ;
238+ }
239+
210240 // Load commodity flow graphs and save to file
211- let commodity_graphs = load_commodity_graphs ( model_path) ?;
241+ let commodity_graphs =
242+ load_commodity_graphs ( model_path) . context ( "Failed to build commodity flow graphs." ) ?;
212243 save_commodity_graphs_for_model ( & commodity_graphs, output_path) ?;
213244 info ! ( "Commodity flow graphs saved to file" ) ;
214245
0 commit comments