๐Ÿ“ฆ jieyouxu / rustc-less-ignore-debug

๐Ÿ“„ main.rs ยท 80 lines
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#![feature(let_chains)]

mod cli;
mod config;
mod logging;
mod run;

use std::path::PathBuf;

use clap::Parser as _;
use confique::toml::FormatOptions;
use confique::Config as _;
use miette::{bail, IntoDiagnostic};
use tracing::*;

use crate::cli::{Cli, Cmd};
use crate::config::Config;

#[allow(dead_code)]
const TARGET_TRIPLE: &str = env!("TARGET");

fn main() -> miette::Result<()> {
    logging::setup_logging();

    let cli = Cli::parse();
    debug!(?cli);

    let exe_path = std::env::current_exe().into_diagnostic()?;
    let config_path = exe_path.parent().unwrap().join("config.toml");
    debug!(?config_path);
    debug!("config exists: {}", config_path.exists());
    let config = if cli.command != Cmd::GenerateConfig {
        info!("trying to read config from `{}`", config_path.display());
        if !config_path.exists() {
            info!("no existing config detected");
            info!("you can generate a default config via `generate-config` command");
            info!("the tool will now exit");
            return Ok(());
        }

        let config = Config::from_file(&config_path)
            .inspect_err(|e| {
                warn!("failed to load config from `{}`", config_path.display());
                warn!("default config values will be used");
                warn!(?e);
            })
            .unwrap_or_default();
        debug!(?config);
        config
    } else {
        Config::default()
    };

    match &cli.command {
        Cmd::GenerateConfig => {
            if !config_path.exists() {
                info!("generating config at `{}`", config_path.display());
                let template = confique::toml::template::<Config>(FormatOptions::default());
                std::fs::write(&config_path, template).into_diagnostic()?;
            } else {
                error!("config.toml already exists");
                bail!("config.toml already exists!");
            }
        }
        Cmd::Run {
            rustc_repo_path,
            report_path,
        } => {
            run::run(
                &config,
                &exe_path,
                rustc_repo_path.as_path(),
                report_path.as_ref().map(PathBuf::as_path),
            )?;
        }
    }

    Ok(())
}