aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: b84939c335af30b492980e0d0beedb1d205b14e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mod archiver;
mod command;
mod configuration;
mod constants;
mod generator;
mod metadata;
mod post;
mod remote;
mod template;
mod utils;

use command::{Command, available_commands, help::Help};
use configuration::Configuration;
use std::env::args;
use std::io::{Error, Result};
use std::iter::once;

fn main() -> Result<()> {
    let result = run();

    if cfg!(debug_assertions) {
        result
    } else {
        match result {
            Ok(()) => Ok(()),
            Err(e) => {
                eprintln!("{e}");
                std::process::exit(1);
            }
        }
    }
}

fn run() -> Result<()> {
    let configuration = Configuration::new()?;
    let commands = available_commands();
    let arguments: Vec<String> = args().collect();
    let message;

    if let Some(command_name) = arguments.get(1) {
        if let Some(main_command) = commands.into_iter().find(|c| c.command() == command_name) {
            let before_commands = main_command.before_dependencies();
            let after_commands = main_command.after_dependencies();

            let command_chain: Vec<Box<dyn Command>> = before_commands
                .into_iter()
                .chain(once(main_command))
                .chain(after_commands)
                .collect();

            for command in command_chain {
                command.execute(arguments.get(2), &configuration, command_name)?;
            }

            return Ok(());
        }
        message = format!("Command {command_name} does not exist.");
    } else {
        message = "You must provide at least one command.".to_string();
    }

    Help::new().execute(None, &configuration, "help")?;
    Err(Error::other(message))
}