]> git.r.bdr.sh - rbdr/blog/blob - src/main.rs
Add mac
[rbdr/blog] / src / main.rs
1 mod configuration;
2 mod command;
3 mod constants;
4 mod gemini_parser;
5 mod generator;
6 mod archiver;
7 mod metadata;
8 mod post;
9 mod template;
10 mod utils;
11 mod remote;
12
13 use std::iter::once;
14 use std::env::args;
15 use std::io::Result;
16 use command::{available_commands, Command, help::Help};
17 use configuration::Configuration;
18
19 fn main() -> Result<()> {
20 let result = run();
21
22 if cfg!(debug_assertions) {
23 result
24 } else {
25 match result {
26 Ok(_) => Ok(()),
27 Err(e) => {
28 eprintln!("{}", e);
29 std::process::exit(1);
30 }
31 }
32 }
33 }
34
35 fn run() -> Result<()> {
36 let configuration = Configuration::new();
37 let commands = available_commands();
38 let arguments: Vec<String> = args().collect();
39
40 if let Some(command_name) = arguments.get(1) {
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 {
52 let result = command.execute(arguments.get(2), &configuration, command_name);
53 if let Err(_) = result {
54 return result;
55 }
56 }
57
58 return Ok(())
59 }
60 }
61
62 Help::new().execute(None, &configuration, &"help".to_string())
63 }