From 17bdf048723511c40d9f241e3fa54b6109cd29c0 Mon Sep 17 00:00:00 2001 From: BogusOverflow Date: Sat, 11 Jul 2020 15:12:16 +0200 Subject: [PATCH] Changed the way we deal with the folder location for tasks --- src/db.rs | 4 ++-- src/dvcs.rs | 19 +++++++++++++++++++ src/main.rs | 34 +++++++++++++++++++++++++++------- src/task.rs | 6 +++--- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/db.rs b/src/db.rs index d886480..512c047 100644 --- a/src/db.rs +++ b/src/db.rs @@ -32,7 +32,7 @@ impl Entry { } /// Load single entry from folder -pub fn entry(basedir: &str, filter: impl Fn(&Path) -> bool, name: &str) -> Option { +pub fn entry(basedir: &Path, filter: impl Fn(&Path) -> bool, name: &str) -> Option { let basepath = PathBuf::from(&basedir); let path = basepath.clone().join(name); @@ -48,7 +48,7 @@ pub fn entry(basedir: &str, filter: impl Fn(&Path) -> bool, name: &str) -> Optio } /// Loads entire database from folder -pub fn from(path_name: &str, filter: impl Fn(&Path) -> bool) -> Result, std::io::Error> { +pub fn from(path_name: &Path, filter: impl Fn(&Path) -> bool) -> Result, std::io::Error> { let path = PathBuf::from(&path_name); let mut entries = Vec::new(); diff --git a/src/dvcs.rs b/src/dvcs.rs index ad556fa..c44bd7f 100644 --- a/src/dvcs.rs +++ b/src/dvcs.rs @@ -36,4 +36,23 @@ impl Backend { } } } + + pub fn update(&self) -> bool { + match self { + Backend::Git => { + let status = Command::new("git") + .arg("pull") + .status() + .expect("Failed to pull on git repo."); + status.success() + }, + Backend::Mercurial => { + unreachable!("Unimplemented"); + }, + Backend::Unknown(name) => { + eprintln!("Unknown DVCS: {}", name); + false + } + } + } } diff --git a/src/main.rs b/src/main.rs index f7d1830..695eb26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,16 +13,32 @@ mod task; fn main() -> Result<(), std::io::Error> { - let home_dir = env::var("HOME").expect("$HOME not defined. WTF?"); - let base_dir = format!("{}/.git-build", home_dir); + let default_folder; + // Check if we defined a given folder in which to find the tasks + let base_dir = match env::var_os("GITBUILDDIR") { + Some(val) => { + default_folder = false; + Path::new(&val) + }, + None => { + default_folder = true; + let home_dir = env::var("HOME").expect("$HOME not defined. WTF?"); + Path::new(&format!("{}/.git-build", home_dir)) + } + }; + + // If we're not using a GITBUILDDIR folder, // We're gonna check if they already is a .git-build folder, and create one otherwise. - if !Path::new(&base_dir).exists() { - log::info("create_git_build_folder", Some(&HashMap::new())); - - DirBuilder::new() + if !base_dir.exists() { + if default_folder == false { + // log::error("no_folder", HashMap::new().insert("$i18n_folder", &base_dir.to_str().unwrap() ) ); + } else { + log::info("create_git_build_folder", Some(&HashMap::new())); + DirBuilder::new() .recursive(true) .create(&base_dir).unwrap(); + } } let cmd = cli::Cli::from_args(); @@ -52,7 +68,11 @@ fn main() -> Result<(), std::io::Error> { // Otherwise, it's a sourceless task continue } - // So the cloned repo is here maybe update? + // The repo has been cloned already, we update it. + task.dvcs.update(); + + + } Ok(()) } diff --git a/src/task.rs b/src/task.rs index c7fa46e..1670c8d 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::{PathBuf, Path}; use std::ffi::OsString; use std::collections::HashMap; @@ -57,7 +57,7 @@ pub fn from_entries(db: Vec) -> HashMap { /// Returns a hashmap of tasks, or std::io::Error /// Reads all entries in a directory -pub fn from_dir(base_dir: &str) -> Result, std::io::Error> { +pub fn from_dir(base_dir: &Path) -> Result, std::io::Error> { Ok(from_entries( db::from(base_dir, is_executable)? )) @@ -66,7 +66,7 @@ pub fn from_dir(base_dir: &str) -> Result, std::io::Error> /// Returns a hashmap of tasks, or std::io::Error /// Reads entries in a given list from a directory, fails if a requested entry doesn't exist /// (does not load the whole folder) -pub fn from_dir_and_list(basedir: &str, list: Vec) -> Result, db::Error> { +pub fn from_dir_and_list(basedir: &Path, list: Vec) -> Result, db::Error> { let mut entries: HashMap = HashMap::new(); for item in list { if let Some(entry) = db::entry(&basedir, is_executable, &item) {