feat(git): react to exit code of git

Propagates the git exit code up through the layers, making the config
handle the actual errors at the output layer.

Signed-off-by: Christina Sørensen <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2023-07-01 06:02:01 +02:00
parent 301c604f37
commit e11efb3229
Signed by: cafkafk
GPG key ID: CDDC792F655251ED
2 changed files with 42 additions and 43 deletions

View file

@ -119,88 +119,94 @@ impl Links {
impl GitRepo { impl GitRepo {
/// Clones the repository to its specified folder. /// Clones the repository to its specified folder.
fn clone(&self) { fn clone(&self) -> bool {
if self.flags.contains(&RepoFlags::Clone) { if self.flags.contains(&RepoFlags::Clone) {
// TODO: check if &self.name already exists in dir // TODO: check if &self.name already exists in dir
let out = Command::new("git") let output = Command::new("git")
.current_dir(&self.path) .current_dir(&self.path)
.arg("clone") .arg("clone")
.arg(&self.url) .arg(&self.url)
.arg(&self.name) .arg(&self.name)
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to clone: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to clone: {:?}", &self,));
// info!("{out}"); output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!("{} has clone set to false, not cloned", &self.name);
false
} }
} }
/// Pulls the repository if able. /// Pulls the repository if able.
fn pull(&self) { fn pull(&self) -> bool {
if self.flags.contains(&RepoFlags::Pull) { if self.flags.contains(&RepoFlags::Pull) {
let out = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("pull") .arg("pull")
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to pull: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to pull: {:?}", &self,));
output.status.success()
} else { } else {
info!("{} has clone set to false, not pulled", &self.name); info!("{} has clone set to false, not pulled", &self.name);
false
} }
// info!("{out}");
} }
/// Adds all files in the repository. /// Adds all files in the repository.
fn add_all(&self) { fn add_all(&self) -> bool {
if self.flags.contains(&RepoFlags::Push) { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("add") .arg("add")
.arg(".") .arg(".")
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to add: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to add: {:?}", &self,));
// info!("{out}"); output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!("{} has clone set to false, not cloned", &self.name);
false
} }
} }
/// Tries to commit changes in the repository. /// Tries to commit changes in the repository.
#[allow(dead_code)] #[allow(dead_code)]
fn commit(&self) { fn commit(&self) -> bool {
if self.flags.contains(&RepoFlags::Push) { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("commit") .arg("commit")
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,));
// info!("{out}"); output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!("{} has clone set to false, not cloned", &self.name);
false
} }
} }
/// Tries to commit changes with a message argument. /// Tries to commit changes with a message argument.
fn commit_with_msg(&self, msg: &String) { fn commit_with_msg(&self, msg: &String) -> bool {
if self.flags.contains(&RepoFlags::Push) { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("commit") .arg("commit")
.arg("-m") .arg("-m")
.arg(msg) .arg(msg)
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to commit: {:?}", &self,));
// info!("{out}"); output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!("{} has clone set to false, not cloned", &self.name);
false
} }
} }
/// Attempts to push the repository. /// Attempts to push the repository.
fn push(&self) { fn push(&self) -> bool {
if self.flags.contains(&RepoFlags::Push) { if self.flags.contains(&RepoFlags::Push) {
let out = Command::new("git") let output = Command::new("git")
.current_dir(format!("{}{}", &self.path, &self.name)) .current_dir(format!("{}{}", &self.path, &self.name))
.arg("push") .arg("push")
.output() .output()
.unwrap_or_else(|_| panic!("git repo failed to push: {:?}", &self,)); .unwrap_or_else(|_| panic!("git repo failed to push: {:?}", &self,));
// info!("{out}"); output.status.success()
} else { } else {
info!("{} has clone set to false, not cloned", &self.name); info!("{} has clone set to false, not cloned", &self.name);
false
} }
} }
/// Removes repository /// Removes repository
@ -242,51 +248,44 @@ impl Config {
} }
fn on_all_spinner<F>(&self, op: &str, f: F) fn on_all_spinner<F>(&self, op: &str, f: F)
where where
F: Fn(&GitRepo), F: Fn(&GitRepo) -> bool,
{ {
for (_, category) in self.categories.iter() { for (_, category) in self.categories.iter() {
for (_, repo) in category.repos.iter() { for (_, repo) in category.repos.iter() {
let mut sp = let mut sp =
Spinner::new(Spinners::Dots10, format!("{}: {}", repo.name, op).into()); Spinner::new(Spinners::Dots10, format!("{}: {}", repo.name, op).into());
f(repo); if f(repo) {
sp.stop_and_persist("", format!("{}: {}", repo.name, op).into()); sp.stop_and_persist("", format!("{}: {}", repo.name, op).into());
} else {
sp.stop_and_persist("", format!("{}: {}", repo.name, op).into());
}
} }
} }
} }
/// Tries to pull all repositories, skips if fail. /// Tries to pull all repositories, skips if fail.
pub fn pull_all(&self) { pub fn pull_all(&self) {
debug!("exectuting pull_all"); debug!("exectuting pull_all");
self.on_all_spinner("pull", |repo| { self.on_all_spinner("pull", |repo| repo.pull());
repo.pull();
});
} }
/// Tries to clone all repositories, skips if fail. /// Tries to clone all repositories, skips if fail.
pub fn clone_all(&self) { pub fn clone_all(&self) {
debug!("exectuting clone_all"); debug!("exectuting clone_all");
self.on_all_spinner("clone", |repo| { self.on_all_spinner("clone", |repo| repo.clone());
repo.clone();
});
} }
/// Tries to add all work in all repositories, skips if fail. /// Tries to add all work in all repositories, skips if fail.
pub fn add_all(&self) { pub fn add_all(&self) {
debug!("exectuting clone_all"); debug!("exectuting clone_all");
self.on_all_spinner("add", |repo| { self.on_all_spinner("add", |repo| repo.add_all());
repo.add_all();
});
} }
/// Tries to commit all repositories one at a time, skips if fail. /// Tries to commit all repositories one at a time, skips if fail.
pub fn commit_all(&self) { pub fn commit_all(&self) {
debug!("exectuting clone_all"); debug!("exectuting clone_all");
self.on_all_spinner("commit", |repo| { self.on_all_spinner("commit", |repo| repo.commit());
repo.commit();
});
} }
/// Tries to commit all repositories with msg, skips if fail. /// Tries to commit all repositories with msg, skips if fail.
pub fn commit_all_msg(&self, msg: &String) { pub fn commit_all_msg(&self, msg: &String) {
debug!("exectuting clone_all"); debug!("exectuting clone_all");
self.on_all_spinner("commit", |repo| { self.on_all_spinner("commit", |repo| repo.commit_with_msg(msg));
repo.commit_with_msg(msg);
});
} }
/// Tries to pull, add all, commit with msg "quick commit", and push all /// Tries to pull, add all, commit with msg "quick commit", and push all
/// repositories, skips if fail. /// repositories, skips if fail.

View file

@ -2,13 +2,6 @@ categories:
config: config:
flags: [] flags: []
repos: repos:
starship:
name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
flags:
- Clone
- Push
qmk_firmware: qmk_firmware:
name: qmk_firmware name: qmk_firmware
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
@ -16,6 +9,13 @@ categories:
flags: flags:
- Clone - Clone
- Push - Push
starship:
name: starship
path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git
flags:
- Clone
- Push
utils: utils:
flags: [] flags: []
repos: repos: