diff --git a/src/main.rs b/src/main.rs index cd39f23..fa8ba3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ extern crate clap; extern crate git2; extern crate yaml_rust; +use std::io::{self, Write}; use backend::Backend; use cli::cli; use conf::{Conf,get_configuration,create_default_config}; @@ -24,45 +25,57 @@ mod constants; mod models; -fn main() { +fn run_app() -> Result<(), String> { let app = cli(); let matches = app.get_matches(); let debug_enabled = matches.is_present("debug"); if debug_enabled { println!("Debug messages are enabled."); } - let repo = match Repository::discover(".") { - Ok(repo) => repo, - // not a git repository, ignore - Err(e) => { - if debug_enabled { println!("This is not a git repository: {:?}", e); } - return (); - } - }; - let conf_path: Option = if matches.is_present("config") { Some(String::from(matches.value_of("config").unwrap())) } else { None }; + // create default config command if matches.is_present(CLI_DEFAULT_CONFIG_SUBC_NAME) { let p = get_default_config_path(); match create_default_config(&p) { Ok(path) => { println!("Configuration file created at \"{}\"", path); - return (); + return Ok(()); + } + Err(e) => { + return Err(format!("Failed to create configuration file \"{}\": {}", p.to_str().unwrap(), e)); } + }; + } else { + // no command, run primary functionality + let repo = match Repository::discover(".") { + Ok(repo) => repo, + // not a git repository, ignore Err(e) => { - println!("Failed to create configuration file \"{}\": {}", p.to_str().unwrap(), e); - return (); + if debug_enabled { println!("This is not a git repository: {:?}", e); } + return Ok(()); } }; + + let backend = Backend::new(repo, debug_enabled); + let dm: DisplayMaster = DisplayMaster::new(backend, debug_enabled); + let mut conf: Conf = get_configuration(conf_path, dm); + let out: String = conf.populate_values(); + println!("{}", out); } + Ok(()) +} - let backend = Backend::new(repo, debug_enabled); - let dm: DisplayMaster = DisplayMaster::new(backend, debug_enabled); - let mut conf: Conf = get_configuration(conf_path, dm); - let out: String = conf.populate_values(); - println!("{}", out); +fn main() { + ::std::process::exit(match run_app() { + Ok(_) => 0, + Err(err) => { + writeln!(io::stderr(), "{}", err).unwrap(); + 2 + } + }); }