summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 487d0955f4561d5c7bbc3f659ad462c1366f0dbc (plain) (blame)
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
use backup::Backup;
use clap::Parser;
use cli::Subcommands;
use config::Config;
use error::Error;
use tracing::{debug, level_filters::LevelFilter};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

mod backup;
mod cli;
mod config;
mod error;
mod packages;
mod pathinfo;

fn main() -> color_eyre::Result<()> {
    color_eyre::install()?;

    let file_appender = tracing_appender::rolling::never("./", "arbs.log");
    let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);

    tracing_subscriber::registry()
        .with(
            fmt::layer()
                .with_writer(non_blocking)
                .with_file(false)
                .with_ansi(false)
                .without_time(),
        )
        .with(fmt::layer().with_file(false).without_time())
        .with(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::INFO.into())
                .from_env_lossy(),
        )
        .init();
    debug!("logging initialized");

    let cli = cli::Cli::parse();

    let config = Config::load(cli.config)?;

    match cli.subcommand {
        Subcommands::GenerateConfig => Config::generate()?,
        Subcommands::Save { package_manager } => {
            let backup = Backup::create(&config, package_manager)?;
            backup.save(&config)?;
        }
        Subcommands::Restore { package_install } => {
            let Some(last_backup) = Backup::get_last(&config)? else {
                return Err(Error::BackupNotFound)?;
            };

            if package_install {
                last_backup.packages.install()?;
            }

            last_backup.restore(&config)?;
        }
    };
    Ok(())
}