diff --git a/src/push.rs b/src/push.rs index c2e355b..a1a2a79 100644 --- a/src/push.rs +++ b/src/push.rs @@ -1,35 +1,58 @@ -use std::process::Command; +use std::process::{exit, Command}; use chrono::Utc; -use crate::{config::Config, utils::log_success}; +use crate::{ + config::Config, + utils::{log_error, log_process, log_success}, +}; pub fn main(message: Option) { let message = message.unwrap_or_else(|| Utc::now().format("pi - %d/%m/%Y %H:%M").to_string()); + let timestamp = Utc::now().timestamp(); + let suffix = format!("pi-{timestamp}"); + let tag = Config::get_local() + .unwrap_or_else(|| { + log_error( + "no config file found.\nPlease initialize with 'pi init '", + ); + exit(1) + }) + .identifier() + .to_string() + + &suffix; // commit + log_process("committing changes"); Command::new("git") .args(["commit", "-m", &message]) .status() - .unwrap(); + .unwrap() + .success() + .then_some(()) + .unwrap_or_else(|| exit(1)); // push Command::new("git").arg("push").status().unwrap(); // tag - let timestamp = Utc::now().timestamp(); - let suffix = format!("pi-{timestamp}"); - let tag = Config::get_local().unwrap().identifier().to_string() + &suffix; + log_process("tagging"); Command::new("git") .args(["tag", "-a", &tag, "-m", ""]) .status() - .unwrap(); + .unwrap() + .success() + .then_some(()) + .unwrap_or_else(|| exit(1)); // push tag Command::new("git") .args(["push", "--follow-tags"]) .status() - .unwrap(); + .unwrap() + .success() + .then_some(()) + .unwrap_or_else(|| exit(1)); log_success(&format!("pushed with tag '{tag}'")); } diff --git a/src/utils.rs b/src/utils.rs index e93f060..c09bbe3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -91,3 +91,9 @@ fn format_variable(input: &str) -> String { color::Fg(color::Reset), ) } + +pub fn log_error(input: impl AsRef) { + let input = input.as_ref(); + log_pi_prefix(); + println!("error: {input}"); +}