diff --git a/src/main.rs b/src/main.rs index 09b3e6a..523a7ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,11 @@ use git2::Repository; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, fs, path::Path}; +use std::{ + collections::HashMap, + fs, + path::{Path, PathBuf}, + process::Command, +}; use walkdir::WalkDir; #[derive(Debug, Serialize, Deserialize)] @@ -10,14 +15,26 @@ struct PersistableRepo { } fn main() -> anyhow::Result<()> { - let dir = match std::env::args().nth(1) { + let dir = match std::env::args().nth(2) { Some(d) => d, None => { - println!("Usage:\n gitice \n"); + println!("Usage:\n gitice \n"); return Ok(()); } }; + // temporary solution to support both freezing and thawing + match std::env::args().nth(1).as_ref().map(|s| &s[..]) { + Some("freeze") => freeze_repos(dir), + Some("thaw") => thaw_repos(dir), + _ => { + println!("Usage:\n gitice \n"); + Ok(()) + } + } +} + +fn freeze_repos(dir: String) -> anyhow::Result<()> { let mut repos: HashMap = HashMap::new(); for entry in WalkDir::new(dir.clone()).into_iter().filter_map(|e| e.ok()) { if entry.file_type().is_dir() { @@ -60,3 +77,27 @@ fn main() -> anyhow::Result<()> { fs::write("gitice.lock", toml::to_string(&repos)?).expect("could not write to lockfile!"); Ok(()) } + +fn thaw_repos(dir: String) -> anyhow::Result<()> { + let lockfile = fs::read_to_string("gitice.lock").expect("unable to read lockfile!"); + let repos: HashMap = toml::from_str(&lockfile)?; + + for (name, repo) in repos { + let output = Command::new("git") + .args(&[ + "clone", + &repo.remote_url, + PathBuf::from(&dir).join(&name).to_str().unwrap(), + ]) + .output() + .expect("Failed to run `git clone`. Perhaps git is not installed?"); + + if output.status.success() { + println!("Thawed {} successfully.", name) + } else { + println!("{}", std::str::from_utf8(&output.stderr)?) + } + } + + Ok(()) +}