summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 4d7b9098d1a45e96848a79e72a4cae3ca5c2b0e0 (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
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
extern crate lazy_static;

use clap::{App, AppSettings, Arg};
use env_logger;
use log::error;

mod backends;
mod conf;
mod serde_duration;
mod wait;

fn main() {
    let matches = App::new("evr")
        .version(env!("CARGO_PKG_VERSION"))
        .author("syn")
        .setting(AppSettings::ColoredHelp)
        .setting(AppSettings::UnifiedHelpMessage)
        .arg(
            Arg::with_name("src")
                .required(true)
                .index(1)
                .help("source file"),
        )
        .arg(
            Arg::with_name("time")
                .short("t")
                .long("time")
                .help("show wall time"),
        )
        .arg(
            Arg::with_name("mem")
                .short("m")
                .long("mem")
                .help("show mem usage (rss)"),
        )
        .arg(
            Arg::with_name("where")
                .short("w")
                .long("where")
                .help("only print executable location. error if does not exist"),
        )
        .get_matches();

    env_logger::builder().format_timestamp(None).init();

    let src_path: std::path::PathBuf = matches.value_of("src").expect("src is required").into();

    let config = match conf::get_conf() {
        Ok(c) => c,
        Err(err) => {
            error!("could not load config: {}", err);
            std::process::exit(64); // EX_USAGE (BSD sysexits(3))
        }
    };

    if matches.is_present("where") {
        match backends::get_binary_by_filename(src_path.as_ref()) {
            Ok(path) => if path.exists() {
                println!("{}", path.display())
            } else {
                error!("File should be located at '{}', when you build it", path.display())
            }
            Err(e) => error!("No binary: {}", e),
        }
        return;
    }

    if src_path.exists() {
        if let Some(backend) = config.get_backend(&src_path) {
            match backend.run(&src_path) {
                Ok(status) => {
                    // print to stderr to allow `>/dev/null` for user programs
                    if matches.is_present("time") {
                        eprintln!("wall time: {:?}", status.wall_time);
                    }
                    if matches.is_present("mem") {
                        eprintln!("rss: {}K", status.usage.get_rss_bytes() / 1000);
                    }
                }
                Err(err) => error!("{}", err),
            };
        } else {
            error!("could not match backend");
        }
    } else {
        error!("File {} does not exist", src_path.display());
    };
}