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(), } }