aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
blob: 500a901954edc9bd68ded41bddeea1995387ce61 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use clap::{App, Arg};
use colored::Colorize;
use std::{
    env,
    fs::File,
    path::{Path, PathBuf},
};

pub struct Paths {
    pub config: File,
    pub data: PathBuf,
}

/// Initialise the application by getting valid path options
pub fn init() -> Paths {
    let app = App::new("webgit")
        .about("The friendly and simple git web frontend")
        .version("0.0.0")
        .arg(
            Arg::with_name("CONFIG")
                .short("c")
                .long("config")
                .takes_value(true)
                .help(
                    "Provide the path to the system configuration. Alternatively \
                     set WEBGIT_CONFIG_PATH in your env",
                ),
        )
        .arg(
            Arg::with_name("DATA_DIR")
                .short("d")
                .long("data-dir")
                .takes_value(true)
                .help(
                    "Specify where webgit should save git repositories. Alternatively \
                     set WEBGIT_DATA_DIR in your env",
                ),
        );

    let matches = app.get_matches();

    Paths {
        config: File::open(
            env::var_os("WEBGIT_CONFIG_PATH")
                .map(|os| match os.into_string() {
                    Ok(p) => p.to_owned(),
                    Err(_) => {
                        eprintln!("{}: Failed to parse provided config path!", "Error:".red());
                        std::process::exit(2);
                    }
                })
                .unwrap_or_else(|| match matches.value_of("CONFIG") {
                    Some(p) => p.to_owned(),
                    None => {
                        eprintln!("{}: No config provided!", "Error:".red());
                        std::process::exit(2);
                    }
                }),
        )
        .expect(&format!("{}: Config file not found!", "Error:".red())),
        data: Path::new(
            &env::var_os("WEBGIT_DATA_DIR")
                .map(|os| {
                    os.into_string().expect(&format!(
                        "{}: Failed to parse provided data-dir path!",
                        "Error".red()
                    ))
                })
                .unwrap_or_else(|| match matches.value_of("CONFIG") {
                    Some(p) => p.to_owned(),
                    None => {
                        eprintln!("{}: No data dir provided!", "Error:".red());
                        std::process::exit(2);
                    }
                }),
        )
        .into(),
    }
}