From 1480f7c496163a197408445d08c8be629d08c1a6 Mon Sep 17 00:00:00 2001 From: n0m1s Date: Tue, 31 Dec 2019 13:30:47 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20customizing=20repo=20&=20json=20pat?= =?UTF-8?q?hs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/gitmoji.rs | 28 ++++++---------------------- src/main.rs | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/gitmoji.rs b/src/gitmoji.rs index 14d3453..7528ec1 100644 --- a/src/gitmoji.rs +++ b/src/gitmoji.rs @@ -2,7 +2,7 @@ extern crate git2; extern crate curl; extern crate derive_more; -use std::path::PathBuf; +use std::path::Path; use std::io::Write; use std::fs::File; @@ -13,13 +13,6 @@ use self::git2::build::RepoBuilder; use self::curl::easy::Easy; -fn config_path() -> PathBuf { - let mut config_dir = dirs::config_dir().expect("cannot open config dir"); - config_dir.push("gitmoji-rust"); - - config_dir -} - #[derive(Debug, From, Display)] pub enum RetrievingError { Git(git2::Error), @@ -60,22 +53,17 @@ impl Url { } } -fn need_update(url: &Url) -> Result { - //path to the "gitmoji" git repo - let mut repo_path = config_path(); - repo_path.push("gitmoji"); - let repo_path = repo_path; - +fn need_update(url: &Url, repo_path: &Path) -> Result { //if repo has to be cloned, we need to force the JSON download let mut force_json_dl = false; let repo = if repo_path.is_dir() { - Repository::open_bare(&repo_path) + Repository::open_bare(repo_path) } else { force_json_dl = true; RepoBuilder::new() .bare(true) - .clone(&url.repo_url, &repo_path) + .clone(&url.repo_url, repo_path) }?; //OID of the local master branch @@ -100,15 +88,11 @@ fn need_update(url: &Url) -> Result { Ok(true) } -pub fn update(url: &Url) -> Result<(), RetrievingError> { - if !need_update(url)? { +pub fn update(url: &Url, repo_path: &Path, json_path: &Path) -> Result<(), RetrievingError> { + if !need_update(url, repo_path)? { return Ok(()); } - let mut json_path = config_path(); - json_path.push("gitmoji.json"); - let json_path = json_path; - let mut file = File::create(&json_path)?; let mut curl = Easy::new(); diff --git a/src/main.rs b/src/main.rs index 78540c9..61942d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,24 @@ mod gitmoji; +use std::path::PathBuf; + +fn config_path() -> PathBuf { + let mut config_dir = dirs::config_dir().expect("cannot open config dir"); + config_dir.push("gitmoji-rust"); + + config_dir +} + fn main() { - gitmoji::update(&gitmoji::Url::default_github()).unwrap(); + //path to the "gitmoji" git repo + let mut repo_path = config_path(); + repo_path.push("gitmoji"); + let repo_path = repo_path; + + //json file path + let mut json_path = config_path(); + json_path.push("gitmoji.json"); + let json_path = json_path; + + gitmoji::update(&gitmoji::Url::default_github(), &repo_path, &json_path).unwrap(); }