diff --git a/src/globals.rs b/src/globals.rs index 2325c9c..a290ed3 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -18,7 +18,8 @@ lazy_static! { #[cfg(not(feature = "dev"))] lazy_static! { - pub static ref CONFIG: crate::setup::Config = crate::setup::init(); + pub static ref CONFIG: crate::setup::Config = + crate::setup::init(std::env::args().any(|a| &a == "init")); pub static ref PASSWORD_HASH: Vec = { let password_path = crate::setup::get_password_path(); std::fs::read(&password_path).unwrap_or_else(|e| { diff --git a/src/setup.rs b/src/setup.rs index 2a601a2..01c3b44 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -252,14 +252,14 @@ pub fn logger_middleware() -> Logger { /// Performs the initial setup #[cfg(not(feature = "dev"))] -pub fn init() -> Config { +pub fn init(init: bool) -> Config { fs::create_dir_all(get_config_dir()).unwrap_or_else(|e| { eprintln!("Can't create config directory: {}.", e); process::exit(1); }); let password_path = get_password_path(); - if !password_path.exists() { + if init { let mut password; loop { password = PasswordInput::new() @@ -294,19 +294,27 @@ pub fn init() -> Config { eprintln!("Can't write password: {}", e); process::exit(1); }); + } else if !get_password_path().exists() { + eprintln!("No password file found. Try running `filite init`."); + process::exit(1); } let config_path = get_config_path(); - if !config_path.exists() { + if init { println!("Generating config file at {}", config_path.display()); let config = Config::default(); config.write_file().unwrap_or_else(|e| { eprintln!("Can't write config file: {}", e); process::exit(1); }); - return config; + } else if !config_path.exists() { + eprintln!("No config file found. Try running `filite init`."); + process::exit(1); } + if init { + process::exit(0); + } Config::read_file().unwrap_or_else(|e| { eprintln!("{}", e); process::exit(1);