From 84a9a0ccee713e26a28ff5e54ea3776085d93b5f Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Mon, 22 Jun 2020 01:48:59 +0200 Subject: Updating just like... a bunch of shit --- src/cli.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/cli.rs (limited to 'src/cli.rs') diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..500a901 --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,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(), + } +} -- cgit v1.2.3