Skip to content

Commit

Permalink
feat(error handling): properly handle status code
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed May 16, 2024
1 parent d481315 commit 8ea5349
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use clap::Parser;
use console::style;
mod cli;
mod utils;
use crate::utils::{config, validate_git_project};
use inquire::Confirm;
use std::process::Command;

#[derive(Parser, Debug)]
Expand All @@ -29,26 +27,9 @@ fn main() {

if let Some(error) = is_project_valid {
if error == "Some changes were not added to commit" {
println!("\n");
println!("{error}");
println!("\n");

let confirm = Confirm::new(&format!(
"{}",
style("Do you want to add all changes to commit?").bold()
))
.with_default(false)
.prompt();
println!("\n{error}\n");

if confirm.unwrap() {
let commit_output = Command::new("git")
.args(["add", "-A"])
.output()
.expect("failed to execute process");
println!("{}", String::from_utf8_lossy(&commit_output.stdout));
} else {
std::process::exit(0);
}
utils::add_to_commit();
} else {
println!("{error}");
std::process::exit(1);
Expand All @@ -67,5 +48,11 @@ fn main() {
.args(["commit", "-m", &commit_message])
.output()
.expect("failed to execute process");
println!("{}", String::from_utf8_lossy(&commit_output.stdout));

if commit_output.status.success() {
println!("changes were committed!");
} else if !commit_output.status.success() {
println!("Failed to commit changes");
println!("{}", String::from_utf8_lossy(&commit_output.stdout));
}
}
27 changes: 27 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use console::style;
use inquire::Confirm;
use serde::Deserialize;
use std::collections::HashMap;
use std::process::Command;
Expand Down Expand Up @@ -57,6 +59,7 @@ pub fn build_commit_message(
}

pub fn config() -> GCommitConfig {
//TODO(@veritem): make this dynamic or sort of configurable
let data = r#"
classes:
feat: "A new feature"
Expand Down Expand Up @@ -95,3 +98,27 @@ pub fn validate_git_project() -> Option<&'static str> {

None
}

pub fn add_to_commit() {
let confirm = Confirm::new(&format!(
"{}",
style("Do you want to add all changes to commit?").bold()
))
.with_default(false)
.prompt();

if confirm.unwrap() {
let commit_output = Command::new("git")
.args(["add", "-A"])
.output()
.expect("failed to execute process");

if commit_output.status.success() {
println!("{}", String::from_utf8_lossy(&commit_output.stdout));
} else {
println!("Something weird happened");
}
} else {
std::process::exit(0);
}
}

0 comments on commit 8ea5349

Please sign in to comment.