From bc9931e4fca69d27feb69a9e2f365670c3986a1b Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Mon, 7 Jun 2021 11:12:00 -0700 Subject: [PATCH] Fix bug where running git commands without .gitm.toml caused an error --- src/main.rs | 62 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index daa117c..88695fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ use std::fs; use std::{ collections::HashMap, env, + path::Path, process::{exit, Child, Command}, }; use toml; @@ -50,27 +51,22 @@ fn main() { .format_timestamp(None) .init(); + // Collect arguments into vector + let args: Vec = env::args().collect(); + + // If config does not exist + if !Path::new(CFG_NAME).exists() { + // Pass arguments to git + default_git(&args[1..]); + exit(0); + } + // Get config contents let cfg_contents = fs::read_to_string(CFG_NAME).log_err("Error reading file"); // Decode config contents let config: Config = toml::from_str(&cfg_contents).unwrap_or_default(); - // If no repos provided, error and exit - if config.repos.len() < 1 { - error!("Please add repos to the {} file", CFG_NAME); - exit(1); - } - - // If origin repo is not defined, error and exit - if config.repos.get("origin").is_none() { - error!("Origin repo required in {} file", CFG_NAME); - exit(1); - } - - // Collect arguments into vector - let args: Vec = env::args().collect(); - // If no arguments provided if args.len() < 2 { // Run git --help @@ -89,6 +85,8 @@ fn main() { match args[1].as_str() { "push" => { info!("Intercepted push command"); + // Ensure that required repos exist + ensure_repos(&config.repos); // For every repo in config for (name, _) in config.repos { // Run git push with applicable arguments @@ -102,6 +100,8 @@ fn main() { } "init" => { info!("Intercepted init command"); + // Ensure that required repos exist + ensure_repos(&config.repos); // Run git init with any preceding arguments let mut proc = Command::new("git") .arg("init") @@ -132,14 +132,30 @@ fn main() { exit_if_code_nonzero(&mut proc); } // Default - _ => { - // Run git, passing through all arguments provided - let mut proc = Command::new("git") - .args(&args[1..]) - .spawn() - .log_err("Error running git command"); - exit_if_code_nonzero(&mut proc); - } + _ => default_git(&args[1..]), + } +} + +fn default_git(args: &[String]) { + // Run git, passing through all arguments provided + let mut proc = Command::new("git") + .args(&args[..]) + .spawn() + .log_err("Error running git command"); + exit_if_code_nonzero(&mut proc); +} + +fn ensure_repos(repos: &HashMap) { + // If no repos provided, error and exit + if repos.len() < 1 { + error!("Please add repos to the {} file", CFG_NAME); + exit(1); + } + + // If origin repo is not defined, error and exit + if repos.get("origin").is_none() { + error!("Origin repo required in {} file", CFG_NAME); + exit(1); } }