Skip to content

Commit

Permalink
Merge pull request #41 from perryrh0dan/dev
Browse files Browse the repository at this point in the history
fix(*): usability
  • Loading branch information
perryrh0dan authored Sep 3, 2020
2 parents ffb2842 + 1f1c920 commit e5d3484
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 67 deletions.
11 changes: 5 additions & 6 deletions src/action/default/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 8 additions & 11 deletions src/action/repository/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand Down
13 changes: 11 additions & 2 deletions src/action/repository/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 => (),
Expand Down
63 changes: 33 additions & 30 deletions src/action/template/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
12 changes: 9 additions & 3 deletions src/action/template/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!())
Expand Down
7 changes: 1 addition & 6 deletions src/out/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
16 changes: 9 additions & 7 deletions src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ impl Repository {
pub fn new(config: &Config, name: &str) -> Result<Repository, RepositoryError> {
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));
Expand Down Expand Up @@ -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}
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub fn check_version(verbose: bool) -> Option<self_update::update::ReleaseAsset>

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,
Expand Down

0 comments on commit e5d3484

Please sign in to comment.