dev_scripts/
main.rs

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::fs::remove_dir_all;

use anyhow::{Context, Result};
use clap::Parser;
use cli::Cli;
use log::LevelFilter;
use simplelog::{Config, SimpleLogger};
use sync::{mirror::MirrorDownloader, pkgsrc::PkgSrcDownloader};
use testing::TestRunner;

mod cli;
mod cmd;
pub mod sync;
pub mod testing;
mod ui;

fn main() -> Result<()> {
    // Parse commandline options.
    let args = Cli::parse();

    // Init and set the verbosity level of the logger.
    let level = match args.verbose {
        0 => LevelFilter::Info,
        1 => LevelFilter::Debug,
        _ => LevelFilter::Trace,
    };
    SimpleLogger::init(level, Config::default()).context("Failed to initialize simple logger")?;

    match args.cmd {
        cli::Command::TestFiles { cmd } => match cmd {
            cli::TestFilesCmd::Test {
                test_data_dir,
                file_type,
            } => {
                // Set a default download destination.
                let test_data_dir = match test_data_dir {
                    Some(test_data_dir) => test_data_dir,
                    None => dirs::cache_dir()
                        .context("Failed to determine home user cache directory.")?
                        .join("alpm/testing"),
                };

                let runner = TestRunner {
                    test_data_dir,
                    file_type,
                };
                runner.run_tests()?;
            }
            cli::TestFilesCmd::Download {
                destination,
                source,
            } => {
                // Set a default download destination.
                let dest = match destination {
                    Some(dest) => dest,
                    None => dirs::cache_dir()
                        .context("Failed to determine home user cache directory.")?
                        .join("alpm/testing"),
                };

                match source {
                    cli::DownloadCmd::PkgSrcRepositories {} => {
                        let downloader = PkgSrcDownloader { dest };
                        downloader.download_package_source_repositories()?;
                    }
                    cli::DownloadCmd::Databases { mirror } => {
                        let downloader = MirrorDownloader { dest, mirror };
                        downloader.sync_remote_databases()?;
                    }
                    cli::DownloadCmd::Packages { mirror } => {
                        let downloader = MirrorDownloader { dest, mirror };
                        downloader.sync_remote_packages()?;
                    }
                };
            }
            cli::TestFilesCmd::Clean {
                destination,
                source,
            } => {
                // Set a default download destination.
                let dest = match destination {
                    Some(dest) => dest,
                    None => dirs::cache_dir()
                        .context("Failed to determine home user cache directory.")?
                        .join("alpm/testing"),
                };

                match source {
                    cli::CleanCmd::PkgSrcRepositories => {
                        remove_dir_all(dest.join("download").join("pkgsrc"))?;
                        remove_dir_all(dest.join("pkgsrc"))?;
                    }
                    cli::CleanCmd::Databases => {
                        remove_dir_all(dest.join("download").join("databases"))?;
                        remove_dir_all(dest.join("databases"))?;
                    }
                    cli::CleanCmd::Packages => {
                        remove_dir_all(dest.join("download").join("packages"))?;
                        remove_dir_all(dest.join("packages"))?;
                    }
                };
            }
        },
    }

    Ok(())
}