Skip to content

Commit

Permalink
release: v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlienard committed Mar 17, 2024
1 parent c133919 commit 33a197f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ghl"
version = "0.1.4"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
59 changes: 23 additions & 36 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ use home::home_dir;
use std::{ffi::OsStr, fs, io::Error};

use colored::*;
use inquire::{validator::Validation, Confirm, Editor, InquireError, Text};

const PR_PREFIX: [&str; 9] = [
"Add ", "Clean ", "Fix ", "Improve ", "Remove ", "Update ", "Rework ", "Ignore ", "Bump ",
];
use inquire::{validator::Validation, Confirm, Editor, InquireError, Select, Text};

pub struct Config {
pub pr_name: String,
Expand Down Expand Up @@ -99,29 +95,33 @@ impl Config {
false => Ok(Validation::Valid),
};

let pr_name_validator =
|value: &str| match PR_PREFIX.iter().any(|current| value.starts_with(current)) {
true => Ok(Validation::Valid),
false => {
let mut output = PR_PREFIX
.iter()
.map(|current| format!("- {}...", current))
.collect::<Vec<String>>()
.join("\n");
output =
"The name must start with one of the following:\n".to_owned() + &output;
Ok(Validation::Invalid(output.into()))
}
};

let linear_branch = Text::new("Linear branch name:")
.with_validator(not_empty_validator)
.prompt()?;

let mut pr_name = Text::new("Pull request name:")
.with_validators(&[Box::new(not_empty_validator), Box::new(pr_name_validator)])
let type_options: Vec<&str> = vec![
"feat", "fix", "refactor", "perf", "style", "test", "docs", "build", "ops", "chore",
];

let _type = Select::new("Type:", type_options).prompt()?;

let scope = Text::new("Scope (optional):").prompt_skippable()?;

let name = Text::new("Name:")
.with_validators(&[Box::new(not_empty_validator)])
.prompt()?;

let mut pr_name = match scope {
Some(scope) => {
if scope.is_empty() {
format!("{}: {}", _type, name)
} else {
format!("{}({}): {}", _type, scope, name)
}
}
None => format!("{}: {}", _type, name),
};

let splited_branch = linear_branch.split('-').collect::<Vec<&str>>();
if splited_branch.len() > 1 {
pr_name = format!(
Expand All @@ -132,20 +132,7 @@ impl Config {
)
}

let prefix = pr_name.split(' ').collect::<Vec<&str>>()[0];
let branch_prefix = match prefix {
"Add" => "feature",
"Clean" => "rework",
"Fix" => "fix",
"Improve" => "rework",
"Remove" => "feature",
"Update" => "feature",
"Rework" => "rework",
"Ignore" => "feature",
"Bump" => "core",
_ => return Err(InquireError::Custom("Invalid input".into())),
};
let branch = format!("{}/{}", branch_prefix, &linear_branch);
let branch = format!("{}/{}", _type, &linear_branch);

Ok(Config { pr_name, branch })
}
Expand Down

0 comments on commit 33a197f

Please sign in to comment.