Browse Source

customizing repo & json paths

master
n0m1s 6 years ago
parent
commit
1480f7c496
Signed by: nomis GPG Key ID: BC0454CAD76FE803
2 changed files with 26 additions and 23 deletions
  1. +6
    -22
      src/gitmoji.rs
  2. +20
    -1
      src/main.rs

+ 6
- 22
src/gitmoji.rs View File

@ -2,7 +2,7 @@ extern crate git2;
extern crate curl; extern crate curl;
extern crate derive_more; extern crate derive_more;
use std::path::PathBuf;
use std::path::Path;
use std::io::Write; use std::io::Write;
use std::fs::File; use std::fs::File;
@ -13,13 +13,6 @@ use self::git2::build::RepoBuilder;
use self::curl::easy::Easy; 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)] #[derive(Debug, From, Display)]
pub enum RetrievingError { pub enum RetrievingError {
Git(git2::Error), Git(git2::Error),
@ -60,22 +53,17 @@ impl Url {
} }
} }
fn need_update(url: &Url) -> Result<bool, RetrievingError> {
//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<bool, RetrievingError> {
//if repo has to be cloned, we need to force the JSON download //if repo has to be cloned, we need to force the JSON download
let mut force_json_dl = false; let mut force_json_dl = false;
let repo = if repo_path.is_dir() { let repo = if repo_path.is_dir() {
Repository::open_bare(&repo_path)
Repository::open_bare(repo_path)
} else { } else {
force_json_dl = true; force_json_dl = true;
RepoBuilder::new() RepoBuilder::new()
.bare(true) .bare(true)
.clone(&url.repo_url, &repo_path)
.clone(&url.repo_url, repo_path)
}?; }?;
//OID of the local master branch //OID of the local master branch
@ -100,15 +88,11 @@ fn need_update(url: &Url) -> Result<bool, RetrievingError> {
Ok(true) 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(()); 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 file = File::create(&json_path)?;
let mut curl = Easy::new(); let mut curl = Easy::new();


+ 20
- 1
src/main.rs View File

@ -1,5 +1,24 @@
mod gitmoji; 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() { 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();
} }

Loading…
Cancel
Save