diff --git a/src/main.rs b/src/main.rs index 695eb26..2fff9a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::env; use std::collections::HashMap; use structopt::StructOpt; use std::fs::{DirBuilder}; -use std::path::Path; +use std::path::{Path,PathBuf}; // For UNIX extended metadata mod log; @@ -19,12 +19,12 @@ fn main() -> Result<(), std::io::Error> { let base_dir = match env::var_os("GITBUILDDIR") { Some(val) => { default_folder = false; - Path::new(&val) + PathBuf::from(val) }, None => { default_folder = true; let home_dir = env::var("HOME").expect("$HOME not defined. WTF?"); - Path::new(&format!("{}/.git-build", home_dir)) + PathBuf::from(&format!("{}/.git-build", home_dir).clone()) } }; @@ -32,7 +32,10 @@ fn main() -> Result<(), std::io::Error> { // We're gonna check if they already is a .git-build folder, and create one otherwise. if !base_dir.exists() { if default_folder == false { - // log::error("no_folder", HashMap::new().insert("$i18n_folder", &base_dir.to_str().unwrap() ) ); + let mut context = HashMap::new(); + context.insert("$i18n_folder", base_dir.to_str().unwrap()); + log::error("no_folder", Some(&context)); + return Err(std::io::Error::new(std::io::ErrorKind::NotFound, "Folder not found.")); } else { log::info("create_git_build_folder", Some(&HashMap::new())); DirBuilder::new() @@ -49,6 +52,10 @@ fn main() -> Result<(), std::io::Error> { task::from_dir_and_list(&base_dir, cmd.tasks).expect("Could not load given tasks") }; + // Change directory to the base_dir + // assert!(env::set_current_dir(&base_dir).is_ok()); + // println!("Successfully changed working directory to {}!", base_dir.display()); + for (task_name, task) in tasks.iter() { let mut context = HashMap::new(); //context.insert("$i18n_task", task_name.to_str().expect("WTF")); @@ -58,12 +65,39 @@ fn main() -> Result<(), std::io::Error> { // Maybe the task has a source we should clone? if let Some(source) = &task.source { + // If we're not using the default folder, change it + let curr_dirr = env::current_dir().unwrap(); // TODO: Might fail because of a lack of permission to read current dir. + + env::set_current_dir(&base_dir).unwrap(); + + let mut context = HashMap::new(); + context.insert("$i18n_folder", base_dir.to_str().unwrap()); + log::info("change_folder", Some(&context)); + if !task.dvcs.clone(source) { context.insert("$i18n_source", &source); log::error("clone_failed", Some(&context)); + + // Change back to the parent folder. + // TODO: refactor this. + env::set_current_dir(curr_dirr).unwrap(); + let mut context = HashMap::new(); + context.insert("$i18n_folder", curr_dirr.to_str().unwrap().clone()); + log::info("change_folder", Some(&context)); + // Skip further processing continue } + + // Change back to the parent folder. + // TODO: refactor this. + env::set_current_dir(curr_dirr).unwrap(); + let mut context = HashMap::new(); + context.insert("$i18n_folder", curr_dirr.clone().to_str().unwrap()); + log::info("change_folder", Some(&context)); + + // TODO: in case the clone fails + } // Otherwise, it's a sourceless task continue