From 896aaadb84d11ad58024011ed124fa2dc242f430 Mon Sep 17 00:00:00 2001 From: Colin Lienard <64312634+colinlienard@users.noreply.github.com> Date: Thu, 11 Apr 2024 21:43:27 +0200 Subject: [PATCH] feat: add commit command (#16) --- src/config.rs | 38 +++++++++++++++++++++++++------------- src/main.rs | 15 ++++++++++++--- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/config.rs b/src/config.rs index 3a064b7..4857537 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,7 +2,10 @@ use home::home_dir; use std::{ffi::OsStr, fs, io::Error}; use colored::*; -use inquire::{validator::Validation, Confirm, Editor, InquireError, Select, Text}; +use inquire::{ + validator::{StringValidator, Validation}, + Confirm, Editor, InquireError, Select, Text, +}; pub struct Config { pub pr_name: String, @@ -89,16 +92,7 @@ impl Config { Ok(default_desc) } - pub fn ask() -> Result { - let not_empty_validator = |value: &str| match value.is_empty() { - true => Ok(Validation::Invalid("You must enter a value.".into())), - false => Ok(Validation::Valid), - }; - - let linear_branch = Text::new("Linear branch name:") - .with_validator(not_empty_validator) - .prompt()?; - + pub fn ask_commit() -> Result<(String, String), InquireError> { let type_options: Vec<&str> = vec![ "feat Add a new feature", "fix Correct a bug or error", @@ -115,14 +109,15 @@ impl Config { let _type = Select::new("Type:", type_options).prompt()?; let _type = _type.split_whitespace().collect::>()[0]; + let _type = String::from(_type); let scope = Text::new("Scope (optional):").prompt_skippable()?; let name = Text::new("Name:") - .with_validators(&[Box::new(not_empty_validator)]) + .with_validators(&[Box::new(get_not_empty_validator())]) .prompt()?; - let mut pr_name = match scope { + let commit_name = match scope { Some(scope) => { if scope.is_empty() { format!("{}: {}", _type, name) @@ -133,6 +128,16 @@ impl Config { None => format!("{}: {}", _type, name), }; + Ok((commit_name, _type)) + } + + pub fn ask_pr() -> Result { + let linear_branch = Text::new("Linear branch name:") + .with_validator(get_not_empty_validator()) + .prompt()?; + + let (mut pr_name, _type) = Config::ask_commit()?; + let splited_branch = linear_branch.split('-').collect::>(); if splited_branch.len() > 1 && splited_branch[1].parse::().is_ok() { pr_name = format!( @@ -172,3 +177,10 @@ This will: (dir_path, token_path, default_desc_path) } } + +fn get_not_empty_validator() -> impl StringValidator { + |value: &str| match value.is_empty() { + true => Ok(Validation::Invalid("You must enter a value.".into())), + false => Ok(Validation::Valid), + } +} diff --git a/src/main.rs b/src/main.rs index 9e36bd3..534b82e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ async fn main() -> Result<(), Box> { match arg { "pr" => pr_command().await?, + "commit" => commit_command()?, "config" => config_command()?, "version" | "-v" => version_command().await?, "update" | "-up" => update_command()?, @@ -32,7 +33,7 @@ async fn pr_command() -> Result<(), Box> { process::exit(1); }); - let config = Config::ask()?; + let config = Config::ask_pr()?; match Config::confirm(&config)? { true => {} @@ -72,6 +73,13 @@ async fn pr_command() -> Result<(), Box> { Ok(()) } +fn commit_command() -> Result<(), Box> { + let (commit_name, _) = Config::ask_commit()?; + git::create_commit(&commit_name)?; + println!("{}", "✔ Commit created.".green()); + Ok(()) +} + fn config_command() -> Result<(), Box> { match Config::set_github_token()? { true => println!("{}", "✔ Token set.".green()), @@ -126,14 +134,15 @@ fn help_command() { println!(" ghl [command]"); println!(); println!("{}", "Commands".bold()); - println!(" config Set the GitHub token and the default pull request description."); println!(" help Display this message."); + println!(" config Set the GitHub token and the default pull request description."); println!(" pr Do the following:"); println!(" 1. Create a new branch."); println!(" 2. Create a new commit."); println!(" 3. Push to the remote repository."); println!(" 4. Create a new pull request."); println!(" 5. Assign you the pull request."); - println!(" update, -up Update the binary to the latest version."); + println!(" commit, -c Create a new conventional commit."); println!(" version, -v Display the current and the latest version."); + println!(" update, -up Update the binary to the latest version."); }