diff --git a/Cargo.lock b/Cargo.lock index c450297..e35811e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -46,6 +46,7 @@ name = "git-build-rs" version = "0.1.0" dependencies = [ "glob", + "hostname", "lazy_static", "serde_json", "structopt", @@ -75,6 +76,17 @@ dependencies = [ "libc", ] +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "itoa" version = "0.4.5" @@ -93,6 +105,12 @@ version = "0.2.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "proc-macro-error" version = "1.0.2" diff --git a/Cargo.toml b/Cargo.toml index 66c964b..ba3417d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,8 @@ edition = "2018" [dependencies] glob = "0.3" lazy_static = "1.4" +# Detect hostname +hostname = "0.3" # Translations serde_json = "1.0" structopt = "0.3" diff --git a/src/main.rs b/src/main.rs index 7ecdcb9..70c7177 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,13 +20,14 @@ fn main() -> Result<(), std::io::Error> { }; for (task_name, task) in tasks.iter() { + println!("TASK: {:?}", task_name); + let mut context = HashMap::new(); context.insert("$i18n_task", task_name.as_str()); log::debug("found_task", Some(&context)); - if task.cloned == false { - // Maybe the task has a source we should clone? - if let Some(source) = &task.source { - + // Maybe the task has a source we should clone? + if let Some(source) = &task.source { + if task.cloned == false { if !task.dvcs.clone(source) { context.insert("$i18n_source", &source); log::error("clone_failed", Some(&context)); @@ -34,11 +35,18 @@ fn main() -> Result<(), std::io::Error> { continue } // New repo just cloned + // TODO: submodule and submodule updates + println!("Downloaded source for {}", task_name); + task.run(); } - // Otherwise, it's a sourceless task - continue + // So the cloned repo is already here maybe update? + // Let's say there was an update and run + println!("Task {} already exists, run it", task_name); + task.run(); + } else { + println!("Taks {} doesn't have a source, run it", task_name); + task.run_once(); } - // So the cloned repo is already here maybe update? } Ok(()) } diff --git a/src/task.rs b/src/task.rs index c7fa46e..66591a9 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use std::ffi::OsString; use std::collections::HashMap; +use std::process::Command; //use crate::log; use crate::dvcs; @@ -15,10 +16,21 @@ pub struct Task { pub dvcs: dvcs::Backend, pub config: HashMap, pub branch: Option, - pub host: Option, + pub hosts: Vec, pub cloned: bool, } +#[derive(Debug)] +pub struct Config { + HashMap, +} + +impl Config { + pub fn from_dir(basedir: &str) -> Config { + db::from(basedir, std::) + } +} + impl Task { pub fn from_entry(entry: &Entry) -> Task { let source = entry.read_setting("source"); @@ -34,11 +46,50 @@ impl Task { dvcs: dvcs::from_setting(entry.read_setting("dvcs")), config: HashMap::new(), branch: entry.read_setting("branch"), - host: entry.read_setting("host"), + hosts: entry.read_setting("hosts").map_or(Vec::new(), |c| c.split("\n").map(|line| line.to_string()).collect()), cloned, } } + + pub fn run_on_host(&self) -> bool { + println!("{:#?}", self.hosts); + if self.hosts.len() == 0 { + return true; + } + // $HOSTNAME env is a bashism, we need to call libc (through hostname crate) + // to find out the actual hostname + let hostname = std::env::var("HOST").unwrap_or_else( + |_| hostname::get().unwrap().into_string().unwrap() + ); + println!("HOSTNAME: {}", hostname); + if self.hosts.contains(&hostname) { + return true; + } + return false; + } + + pub fn run(&self) { + if !self.run_on_host() { return; } + let cmd_out = Command::new("bash") + .arg(&self.bin) + .arg(&self.name) + .output() + .expect(&format!("Failed to run {:?}", &self.bin)); + let mut log_path = self.bin.clone(); + log_path.set_extension("log"); + std::fs::write(&log_path, cmd_out.stderr).expect(&format!("Failed to write log to {:?}", &log_path)); + } + + pub fn run_once(&self) { + if !self.run_on_host() { return; } + let mut done_path = self.bin.clone(); + done_path.set_extension("done"); + if !done_path.exists() { + self.run(); + std::fs::write(&done_path, ""); + } + } } // Takes an already instanced database (ie Vec)