From 1f1c92004cf207d0019ce6cc93bf83825a825957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20P=C3=B6hlmann?= Date: Thu, 3 Sep 2020 15:03:06 +0200 Subject: [PATCH] fix(*): usability --- src/action/default/init.rs | 11 +++--- src/action/repository/add.rs | 19 +++++----- src/action/repository/remove.rs | 13 +++++-- src/action/template/create.rs | 63 +++++++++++++++++---------------- src/action/template/list.rs | 12 +++++-- src/main.rs | 5 ++- src/out/error.rs | 7 +--- src/repository/mod.rs | 16 +++++---- src/update/mod.rs | 4 ++- 9 files changed, 83 insertions(+), 67 deletions(-) diff --git a/src/action/default/init.rs b/src/action/default/init.rs index 25c0a58..17ddb8c 100644 --- a/src/action/default/init.rs +++ b/src/action/default/init.rs @@ -55,17 +55,16 @@ pub fn init(config: &Config, args: &ArgMatches) { // Load repository let mut repository = match Repository::new(config, &repository_name) { Ok(repository) => repository, - Err(error) => { - log::error!("{}", error); - match error { + Err(error) => return match error { RepositoryError::NotFound => out::error::repository_not_found(&repository_name), _ => out::error::unknown(), - } - return; }, }; - repository.init(); + match repository.init() { + Ok(_) => (), + Err(_) => (), + }; // check if templates exist if repository.get_templates().len() <= 0 { diff --git a/src/action/repository/add.rs b/src/action/repository/add.rs index 86e86f2..84cf904 100644 --- a/src/action/repository/add.rs +++ b/src/action/repository/add.rs @@ -73,19 +73,16 @@ pub fn add(config: &mut Config, args: &ArgMatches) { git_options: git_options, }); - // test repository - let mut repository = match Repository::new(config, &repository_name) { - Ok(repository) => repository, - Err(error) => { - log::error!("{}", error); - match error { - RepositoryError::InitializationError => out::error::init_repository(), - _ => out::error::unknown(), - } - return; - }, + // Load repository + let repository = match Repository::new(config, &repository_name) { + Ok(repository) => repository, + Err(error) => return match error { + RepositoryError::NotFound => out::error::repository_not_found(&repository_name), + _ => out::error::unknown(), + }, }; + // Test repository match repository.test() { Ok(()) => (), Err(_) => { diff --git a/src/action/repository/remove.rs b/src/action/repository/remove.rs index 5bb0339..2d34042 100644 --- a/src/action/repository/remove.rs +++ b/src/action/repository/remove.rs @@ -3,7 +3,7 @@ use std::io::ErrorKind; use crate::cli::input::select; use crate::config::{Config}; use crate::out; -use crate::repository::Repository; +use crate::repository::{Repository, RepositoryError}; use crate::utils; use clap::ArgMatches; @@ -28,8 +28,17 @@ pub fn remove(config: &mut Config, args: &ArgMatches) { utils::lowercase(repository_name.unwrap()) }; + // Load repository + let repository = match Repository::new(config, &repository_name) { + Ok(repository) => repository, + Err(error) => return match error { + RepositoryError::NotFound => out::error::repository_not_found(&repository_name), + _ => out::error::unknown(), + }, + }; + // remove template folder - match Repository::delete_repository(config, &repository_name) { + match repository.delete_repository() { Ok(()) => (), Err(error) => match error.kind() { ErrorKind::NotFound => (), diff --git a/src/action/template/create.rs b/src/action/template/create.rs index a66af4e..d4da611 100644 --- a/src/action/template/create.rs +++ b/src/action/template/create.rs @@ -32,38 +32,41 @@ pub fn create(config: &mut Config, args: &ArgMatches) { utils::lowercase(repository_name.unwrap()) }; - // Load repository - let mut repository = match Repository::new(config, &repository_name) { - Ok(repository) => repository, - Err(error) => match error { - RepositoryError::NotFound => return out::error::repository_not_found(&repository_name), - _ => return, - }, - }; + // Load repository + let mut repository = match Repository::new(config, &repository_name) { + Ok(repository) => repository, + Err(error) => return match error { + RepositoryError::NotFound => out::error::repository_not_found(&repository_name), + _ => out::error::unknown(), + }, + }; - repository.init(); + match repository.init() { + Ok(_) => (), + Err(_) => (), + }; - // Get template name from user input - let template_name = if template_name.is_none() { - match input::text("template name", false) { - Some(value) => value, - None => return, - } - } else { - String::from(template_name.unwrap()) - }; + // Get template name from user input + let template_name = if template_name.is_none() { + match input::text("template name", false) { + Some(value) => value, + None => return, + } + } else { + String::from(template_name.unwrap()) + }; - // validate name - let templates = repository.get_templates(); - if templates.contains(&template_name) { - // TODO error - return; - } + // validate name + let templates = repository.get_templates(); + if templates.contains(&template_name) { + // TODO error + return; + } - match repository.create_template(&template_name) { - Ok(()) => (), - Err(error) => { - log::error!("{}", error); - } - }; + match repository.create_template(&template_name) { + Ok(()) => (), + Err(error) => { + log::error!("{}", error); + } + }; } diff --git a/src/action/template/list.rs b/src/action/template/list.rs index 4cdddeb..9b618bc 100644 --- a/src/action/template/list.rs +++ b/src/action/template/list.rs @@ -3,7 +3,7 @@ use std::io::ErrorKind; use crate::cli::input::select; use crate::config::Config; use crate::out; -use crate::repository::Repository; +use crate::repository::{Repository, RepositoryError}; use crate::utils; use clap::ArgMatches; @@ -31,10 +31,16 @@ pub fn list(config: &Config, args: &ArgMatches) { // Load repository let mut repository = match Repository::new(config, &repository_name) { Ok(repository) => repository, - Err(_error) => return, + Err(error) => return match error { + RepositoryError::NotFound => out::error::repository_not_found(&repository_name), + _ => out::error::unknown(), + }, }; - repository.init(); + match repository.init() { + Ok(_) => (), + Err(_) => (), + }; let templates = repository.get_templates(); diff --git a/src/main.rs b/src/main.rs index 2ee4d7a..5123e3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,10 @@ fn main() { logger::init(); // Check for update - update::check_version(false); + match update::check_version(false) { + Some(_) => println!("New release found"), + None => (), + } let matches = App::new("tmpo") .version(crate_version!()) diff --git a/src/out/error.rs b/src/out/error.rs index 4f09eeb..704463c 100644 --- a/src/out/error.rs +++ b/src/out/error.rs @@ -28,11 +28,6 @@ pub fn repository_exists(repository: &str) { } // template -pub fn update_templates() { - let text = format!("{}", "Error updating templates".red()); - println!("{}", text); -} - pub fn template_not_found(template: &str) { let text = format!("Template: {} not found", template).red(); println!("{}", text); @@ -63,4 +58,4 @@ pub fn copy_template() { pub fn unknown() { let text = format!("Unknown error. See the logs for more details").red(); println!("{}", text); -} \ No newline at end of file +} diff --git a/src/repository/mod.rs b/src/repository/mod.rs index eb2b6d4..9b14122 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -30,7 +30,10 @@ impl Repository { pub fn new(config: &Config, name: &str) -> Result { let cfg = match config.get_repository_config(name) { Option::Some(cfg) => cfg, - Option::None => return Err(RepositoryError::NotFound), + Option::None => { + log::error!("Repository not found: {}", name); + return Err(RepositoryError::NotFound); + }, }; let directory = Path::new(&config.template_dir).join(&utils::lowercase(name)); @@ -81,12 +84,10 @@ impl Repository { Ok(()) } - pub fn delete_repository(config: &Config, name: &str) -> Result<(), Error> { - let mut repository_dir = PathBuf::from(&config.template_dir); - repository_dir.push(&utils::lowercase(name)); - - log::info!("Delete repository directory {}", &repository_dir.to_owned().to_str().unwrap()); - match fs::remove_dir_all(repository_dir) { + /// delete + pub fn delete_repository(&self) -> Result<(), Error> { + log::info!("Delete repository directory {}", &self.directory.to_owned().to_str().unwrap()); + match fs::remove_dir_all(&self.directory) { Ok(()) => (), Err(error) => { log::error!{"{}", error} @@ -97,6 +98,7 @@ impl Repository { return Ok(()); } + /// Create a new template with given name in the repository directory pub fn create_template(&self, name: &str) -> Result<(), Error> { let repository_path = Path::new(&self.directory); let template_path = repository_path.join(&name); diff --git a/src/update/mod.rs b/src/update/mod.rs index aeef046..2195f20 100644 --- a/src/update/mod.rs +++ b/src/update/mod.rs @@ -38,7 +38,9 @@ pub fn check_version(verbose: bool) -> Option let text = format!("New release found! {} --> {}", &version, &releases[0].version); log::info!("{}", text); - println!("{}", text); + if verbose { + println!("{}", text); + } let asset = match releases[0].asset_for(self_update::get_target()) { Some(value) => value,