chore(commit-msg.crs): add verbose output
This commit is contained in:
@@ -23,6 +23,7 @@ const DEFAULT_COMMIT_MSG_REGEXP: &str = "^(feat|fix|docs|style|refactor|test|cho
|
||||
/// Install:
|
||||
/// `commit-msg.crs install`
|
||||
fn main() {
|
||||
if is_verbose() { print_info(&format!("Arguments:\n- {}", env::args().collect::<Vec<_>>().join("\n- "))); }
|
||||
let arg1 = env::args().nth(1).unwrap_or_else(|| {
|
||||
exit_with_error_message("Commit message is EMPTY!");
|
||||
});
|
||||
@@ -59,6 +60,10 @@ fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
fn is_verbose() -> bool {
|
||||
if let Ok(v) = env::var("VERBOSE") { v == "1" } else { false }
|
||||
}
|
||||
|
||||
fn print_usage() {
|
||||
if let Some(usage) = get_commit_msg_usage() {
|
||||
print_info(&usage); return;
|
||||
@@ -92,7 +97,9 @@ fn search_git_root_path() -> Option<PathBuf> {
|
||||
exit_with_error_message(&format!("Get current dir failed: {}", e))
|
||||
);
|
||||
loop {
|
||||
if is_verbose() { print_info(&format!("Check git path: {:?}", p)); }
|
||||
if p.join(".git").is_dir() {
|
||||
if is_verbose() { print_ok(&format!("Found git path: {:?}", p)); }
|
||||
return Some(p);
|
||||
}
|
||||
if !p.pop() { return None; }
|
||||
@@ -111,18 +118,21 @@ fn get_home_e() -> PathBuf {
|
||||
|
||||
fn get_commit_msg_file(file: &str) -> Option<String> {
|
||||
let settings_regexp = search_git_root_path_e().join("settings").join(file);
|
||||
if is_verbose() { print_info(&format!("Check file: {:?}, exists: {}", settings_regexp, settings_regexp.exists())); }
|
||||
if settings_regexp.exists() {
|
||||
print_info(&format!("Found file: {:?}", settings_regexp));
|
||||
return Some(fs::read_to_string(settings_regexp).unwrap());
|
||||
}
|
||||
|
||||
let path_regexp = search_git_root_path_e().join(".git").join("hooks").join(file);
|
||||
if is_verbose() { print_info(&format!("Check file: {:?}, exists: {}", path_regexp, path_regexp.exists())); }
|
||||
if path_regexp.exists() {
|
||||
print_info(&format!("Found file: {:?}", path_regexp));
|
||||
return Some(fs::read_to_string(path_regexp).unwrap());
|
||||
}
|
||||
|
||||
let home_path_regexp = get_home_e().join(&(".".to_owned() + file));
|
||||
if is_verbose() { print_info(&format!("Check file: {:?}, exists: {}", home_path_regexp, home_path_regexp.exists())); }
|
||||
if home_path_regexp.exists() {
|
||||
print_info(&format!("Found file: {:?}", home_path_regexp));
|
||||
return Some(fs::read_to_string(home_path_regexp).unwrap());
|
||||
@@ -139,6 +149,19 @@ fn get_commit_msg_usage() -> Option<String> {
|
||||
get_commit_msg_file("commit-msg-regexp-usage")
|
||||
}
|
||||
|
||||
fn copy_file_and_apply_executable_permission(from: &PathBuf, to: &PathBuf) {
|
||||
print_info(&format!("Copy file: {:?} to: {:?}", from, to));
|
||||
let from_content = fs::read(&from).unwrap_or_else(|e|
|
||||
exit_with_error_message(&format!("Read file: {:?}, failed: {}", from, e))
|
||||
);
|
||||
fs::write(to, from_content).unwrap_or_else(|e|
|
||||
exit_with_error_message(&format!("Write file: {:?}, failed: {}", to, e))
|
||||
);
|
||||
fs::set_permissions(to, PermissionsExt::from_mode(0o755)).unwrap_or_else(|e|
|
||||
exit_with_error_message(&format!("Apply executable permission on file: {:?}, failed: {}", to, e))
|
||||
);
|
||||
}
|
||||
|
||||
fn install_commit_msg(force: bool) {
|
||||
let commit_msg_crs = get_home_e().join("bin").join("commit-msg.crs");
|
||||
let commig_msg_exec_file = if commit_msg_crs.exists() {
|
||||
@@ -157,16 +180,7 @@ fn install_commit_msg(force: bool) {
|
||||
exit_with_error_message(&format!("File {:?} exists! or try {}forceinstall{}.", git_hooks_commit_msg, BOLD, END));
|
||||
}
|
||||
|
||||
print_info(&format!("Copy file: {:?} to: {:?}", commig_msg_exec_file, git_hooks_commit_msg));
|
||||
let commig_msg_exec_file_content = fs::read(&commig_msg_exec_file).unwrap_or_else(|e|
|
||||
exit_with_error_message(&format!("Read file: {:?}, failed: {}", commig_msg_exec_file, e))
|
||||
);
|
||||
fs::write(&git_hooks_commit_msg, commig_msg_exec_file_content).unwrap_or_else(|e|
|
||||
exit_with_error_message(&format!("Write file: {:?}, failed: {}", git_hooks_commit_msg, e))
|
||||
);
|
||||
fs::set_permissions(&git_hooks_commit_msg, PermissionsExt::from_mode(0o755)).unwrap_or_else(|e|
|
||||
exit_with_error_message(&format!("Apply executable permission on file: {:?}, failed: {}", git_hooks_commit_msg, e))
|
||||
);
|
||||
copy_file_and_apply_executable_permission(&commig_msg_exec_file, &git_hooks_commit_msg);
|
||||
|
||||
print_ok("Install commit-msg to repo successed!");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user