Skip to content

Commit

Permalink
feat: add commit command (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlienard authored Apr 11, 2024
1 parent 9d7c30e commit 896aaad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
38 changes: 25 additions & 13 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -89,16 +92,7 @@ impl Config {
Ok(default_desc)
}

pub fn ask() -> Result<Config, InquireError> {
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",
Expand All @@ -115,14 +109,15 @@ impl Config {

let _type = Select::new("Type:", type_options).prompt()?;
let _type = _type.split_whitespace().collect::<Vec<&str>>()[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)
Expand All @@ -133,6 +128,16 @@ impl Config {
None => format!("{}: {}", _type, name),
};

Ok((commit_name, _type))
}

pub fn ask_pr() -> Result<Config, InquireError> {
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::<Vec<&str>>();
if splited_branch.len() > 1 && splited_branch[1].parse::<u32>().is_ok() {
pr_name = format!(
Expand Down Expand Up @@ -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),
}
}
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

match arg {
"pr" => pr_command().await?,
"commit" => commit_command()?,
"config" => config_command()?,
"version" | "-v" => version_command().await?,
"update" | "-up" => update_command()?,
Expand All @@ -32,7 +33,7 @@ async fn pr_command() -> Result<(), Box<dyn Error>> {
process::exit(1);
});

let config = Config::ask()?;
let config = Config::ask_pr()?;

match Config::confirm(&config)? {
true => {}
Expand Down Expand Up @@ -72,6 +73,13 @@ async fn pr_command() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn commit_command() -> Result<(), Box<dyn Error>> {
let (commit_name, _) = Config::ask_commit()?;
git::create_commit(&commit_name)?;
println!("{}", "✔ Commit created.".green());
Ok(())
}

fn config_command() -> Result<(), Box<dyn Error>> {
match Config::set_github_token()? {
true => println!("{}", "✔ Token set.".green()),
Expand Down Expand Up @@ -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.");
}

0 comments on commit 896aaad

Please sign in to comment.