]> git.r.bdr.sh - rbdr/blog/blame_incremental - src/main.rs
Make smaller
[rbdr/blog] / src / main.rs
... / ...
CommitLineData
1// mod argument_parser;
2mod configuration;
3mod command;
4mod constants;
5mod gemini_parser;
6mod generator;
7mod archiver;
8mod metadata;
9mod post;
10mod template;
11mod utils;
12mod remote;
13
14use std::iter::once;
15use std::env::args;
16use std::io::Result;
17use command::{available_commands, Command, help::Help};
18use configuration::Configuration;
19
20fn main() -> Result<()> {
21 let result = run();
22
23 if cfg!(debug_assertions) {
24 result
25 } else {
26 match result {
27 Ok(_) => Ok(()),
28 Err(e) => {
29 eprintln!("Error: {}", e);
30 std::process::exit(1);
31 }
32 }
33 }
34}
35
36fn run() -> Result<()> {
37 let configuration = Configuration::new();
38 let commands = available_commands();
39 let arguments: Vec<String> = args().collect();
40
41 if let Some(command_name) = arguments.get(1) {
42 if let Some(main_command) = commands.into_iter().find(|c| c.command() == command_name) {
43 let before_commands = main_command.before_dependencies();
44 let after_commands = main_command.after_dependencies();
45
46 let command_chain: Vec<Box<dyn Command>> = before_commands
47 .into_iter()
48 .chain(once(main_command))
49 .chain(after_commands.into_iter())
50 .collect();
51
52 for command in command_chain {
53 let result = command.execute(arguments.get(2), &configuration, command_name);
54 if let Err(_) = result {
55 return result;
56 }
57 }
58
59 return Ok(())
60 }
61 }
62
63 Help::new().execute(None, &configuration, &"help".to_string())
64}