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