1use std::process::Command;
2use std::convert::AsRef;
3use std::ffi::OsStr;
4
5pub struct Update {
6 cmd: Command,
7}
8
9pub fn update() -> Update {
10 let mut update = Update { cmd: Command::new("git") };
11 update.arg("submodule").arg("update");
12 update
13}
14
15impl Update {
16 fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Update {
17 self.cmd.arg(arg);
18 self
19 }
20
21 pub fn init(&mut self) -> &mut Update { self.arg("--init") }
22 pub fn rebase(&mut self) -> &mut Update { self.arg("--rebase") }
23 pub fn merge(&mut self) -> &mut Update { self.arg("--merge") }
24 pub fn checkout(&mut self) -> &mut Update { self.arg("--checkout") }
25 pub fn remote(&mut self) -> &mut Update { self.arg("--remote") }
26 pub fn no_fetch(&mut self) -> &mut Update { self.arg("--no-fetch") }
27 pub fn recursive(&mut self) -> &mut Update { self.arg("--recursive") }
28 pub fn force(&mut self) -> &mut Update { self.arg("--force") }
29
30 pub fn run(&mut self) {
31 println!("running: {:?}", self.cmd);
32 let status = match self.cmd.status() {
33 Ok(status) => status,
34 Err(e) => fail(&format!("failed to execute command: {}", e)),
35 };
36
37 if !status.success() {
38 fail(&format!("command did not execute successfully, got: {}", status));
39 }
40 }
41}
42
43fn fail(msg: &str) -> ! {
44 println!("\n\n{}\n\n", msg);
45 panic!()
46}
47
48#[test]
49fn build_update() {
50 let mut update = update();
51 update.init()
52 .recursive();
53
54 assert_eq!(format!("{:?}", update.cmd),
55 "\"git\" \"submodule\" \"update\" \"--init\" \"--recursive\"")
56}