A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

use std::error::Error;
use std::fmt;
use std::path::Path;
use command_runner::CommandRunner;
use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub struct GitSubmodules<'a> {
target: &'a str,
command_runner: &'a CommandRunner
}
impl<'a> GitSubmodules<'a> {
pub fn new(target: &'a str, command_runner: &'a CommandRunner) -> GitSubmodules<'a> {
GitSubmodules {
target: target,
command_runner: command_runner
}
}
}
impl<'a> fmt::Display for GitSubmodules<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Submodules for {}", self.target)
}
}
impl<'a> GitSubmodules<'a> {
fn _run_in_target_repo(&self, args: &[&str]) -> Result<Vec<u8>, Box<Error>> {
let mut new_args = vec!["-C", self.target];
new_args.extend_from_slice(args);
self.command_runner.get_output("git", &new_args)
}
}
impl<'a> Symbol for GitSubmodules<'a> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
if !Path::new(self.target).exists() {
return Ok(false);
}
let output = try!(String::from_utf8(try!(self._run_in_target_repo(&["submodule", "status"]))));
Ok(output.lines().all(|line| line.len() == 0 || line.starts_with(' ')))
}
fn execute(&self) -> Result<(), Box<Error>> {
try!(self._run_in_target_repo(&["submodule", "update", "--init"]));
Ok(())
}
fn as_action<'b>(&'b self, runner: &'b SymbolRunner) -> Box<Action + 'b> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b SymbolRunner) -> Box<Action + 'b> where Self: 'b {
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
#[cfg(test)]
mod test {
}