aboutsummaryrefslogtreecommitdiff
path: root/src/cli.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli.rs')
-rw-r--r--src/cli.rs79
1 files changed, 79 insertions, 0 deletions
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(),
+ }
+}