From 792f6dc5c31cec082f4a632d992c3bbb6babcb6e Mon Sep 17 00:00:00 2001 From: Marcel Schneider Date: Thu, 5 Mar 2020 16:53:45 +0100 Subject: [PATCH] Start with getting templates --- src/app.rs | 41 ++++++++++++++++++++++------------------- src/errors.rs | 8 ++++++++ src/helpers.rs | 34 ++++++++++++++++++++++++++++++++++ src/template.rs | 7 +++++++ 4 files changed, 71 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index 030542f..f2887ea 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,7 +5,6 @@ use std::path::PathBuf; // 3rd-party crate imports -use directories::ProjectDirs; use structopt::StructOpt; use log::{debug, info, trace}; @@ -14,6 +13,7 @@ use log::{debug, info, trace}; use crate::cache::File as FileCache; use crate::errors::*; use crate::gitignore::Gitignore; +use crate::helpers; use crate::helpers::{git_dir, BoilerplateOpts, HELP_TEMPLATE}; use crate::template::*; @@ -96,29 +96,32 @@ fn run_add(glob: &str) -> Result<()> { /// Runs the command `get` fn run_get(lang: &str) -> Result<()> { trace!("Run command `get` with lang {}", &lang); - unimplemented!(); + let root = match git_dir()? { + Some(r) => r, + None => return Err(ErrorKind::NoGitRootFound.into()), + }; + info!("Working with git root in {:?}", root); + + let tmpls = helpers::get_templates()?; + let tmpl: &Template = + tmpls.get(lang).ok_or_else(|| ErrorKind::TemplateNotFound(lang.to_string()))?; + debug!("Found a template for {}", lang); + + let url = tmpl + .download_url + .as_ref() + .ok_or_else(|| ErrorKind::TemplateNoDownloadUrl(lang.to_string()))?; + trace!("Getting template for {} from {}", tmpl.pretty_name(), url); + + todo!(); + + Ok(()) } /// Runs the command `list-templates` #[allow(clippy::print_stdout)] fn run_list_templates() -> Result<()> { - let cache_root: PathBuf = match ProjectDirs::from("org", "webschneider", env!("CARGO_PKG_NAME")) - { - Some(dirs) => PathBuf::from(dirs.cache_dir()), - None => "/tmp".into(), - }; - - let cache: FileCache = FileCache::new(&cache_root, std::time::Duration::from_secs(60 * 24 * 2)) - .chain_err(|| "Error while creating FileCache")?; - let tmpl = if cache.exists("templates.json") { - cache.get("templates.json")? - } else { - let tmpls = GithubTemplates::new().chain_err(|| "Error while getting Templates")?; - cache - .set("templates.json", &tmpls) - .chain_err(|| "Error while writing templates to cache")?; - tmpls - }; + let tmpl = helpers::get_templates()?; let names = tmpl.list_names(); println!("{}", names.join("\n")); Ok(()) diff --git a/src/errors.rs b/src/errors.rs index 3da0bff..5bc2a6a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -7,5 +7,13 @@ error_chain! { NoGitRootFound { description("no git root found"), } + TemplateNotFound(t: String) { + description("could not find template"), + display("could not find a template for {}", t), + } + TemplateNoDownloadUrl(name: String) { + description("template has no download url"), + display("template {} has no download url", name) + } } } diff --git a/src/helpers.rs b/src/helpers.rs index 51a06d8..7038d14 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -7,8 +7,11 @@ #![allow(clippy::result_unwrap_used)] use log::{info, trace}; +use crate::cache::File as FileCache; use crate::errors::*; +use crate::{helpers, template::GithubTemplates}; +use directories::ProjectDirs; use std::path::PathBuf; use structopt::{clap, StructOpt}; @@ -97,3 +100,34 @@ pub fn git_dir() -> Result> { info!("Arrived at filesystem root while checking for git folder"); Ok(None) } + +/// Returns a `PathBuf` to the cache root for this project +/// +/// This will be either the appropriate cache dir according to XDG or as a fallback `/tmp`. +pub fn chache_root() -> PathBuf { + match ProjectDirs::from("org", "webschneider", env!("CARGO_PKG_NAME")) { + Some(dirs) => PathBuf::from(dirs.cache_dir()), + None => "/tmp".into(), + } +} + +/// Returns a default file cache +pub fn default_cache() -> Result { + let cache_root = crate::helpers::chache_root(); + FileCache::new(&cache_root, std::time::Duration::from_secs(60 * 24 * 2)) +} + +/// Returns a `GithubTemplates` struct with all available templates +pub fn get_templates() -> Result { + let cache = helpers::default_cache().chain_err(|| "Error while creating cache")?; + let tmpl = if cache.exists("templates.json") { + cache.get("templates.json")? + } else { + let tmpls = GithubTemplates::new().chain_err(|| "Error while getting Templates")?; + cache + .set("templates.json", &tmpls) + .chain_err(|| "Error while writing templates to cache")?; + tmpls + }; + Ok(tmpl) +} diff --git a/src/template.rs b/src/template.rs index 6987a2f..0f88d01 100644 --- a/src/template.rs +++ b/src/template.rs @@ -95,4 +95,11 @@ impl GithubTemplates { .map(Template::pretty_name) .collect() } + + /// Returns the template for the given name, if found + pub fn get(&self, name: &str) -> Option<&Template> { + // names have all a .gitignore suffix + let name = format!("{}.gitignore", name); + self.templates.iter().find(|t| t.name.eq_ignore_ascii_case(&name)) + } }