feat(commit-msg): if crs script file not exists use binaray file

This commit is contained in:
2020-07-12 17:02:36 +08:00
parent 90cc2d33f9
commit c8fa72fca8

View File

@@ -49,6 +49,7 @@ fn main() {
const RED: &str = "\x1B[91m";
const GREEN: &str = "\x1B[92m";
const YELLOW: &str = "\x1B[93m";
const BOLD: &str = "\x1B[1m";
const UNDER: &str = "\x1B[4m";
const END: &str = "\x1B[0m";
@@ -83,9 +84,13 @@ fn install_commit_msg(force: bool) {
exit_with_error_message(&format!("Get env HOME failed: {}!", e));
}));
let commit_msg_crs = home_path.join("bin").join("commit-msg.crs");
if !commit_msg_crs.exists() {
exit_with_error_message(&format!("File {:?} NOT exists!", commit_msg_crs));
}
let commig_msg_exec_file = if !commit_msg_crs.exists() {
//exit_with_error_message(&format!("File {:?} NOT exists!", commit_msg_crs));
print_warn(&format!("File {:?} NOT exists!", commit_msg_crs));
PathBuf::from(env::args().next().unwrap())
} else {
commit_msg_crs
};
let git_hooks = PathBuf::from(".git").join("hooks");
if !git_hooks.exists() {
@@ -96,10 +101,11 @@ fn install_commit_msg(force: bool) {
exit_with_error_message(&format!("File {:?} exists! or try forceinstall.", git_hooks_commit_msg));
}
let commit_msg_crs_content = fs::read_to_string(&commit_msg_crs).unwrap_or_else(|e| {
exit_with_error_message(&format!("Read file: {:?} failed: {}.", commit_msg_crs, e));
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, commit_msg_crs_content).unwrap_or_else(|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));
});
Command::new("chmod").args(&["+x", git_hooks_commit_msg.to_str().unwrap()]).output().unwrap_or_else(|e| {
@@ -114,4 +120,5 @@ fn exit_with_error_message(msg: &str) -> ! { print_error(msg); exit_with_error()
fn print_info(msg: &str) { println!("{b}[INFO ]{e} {m}", b = BOLD, e = END, m = msg); }
fn print_ok(msg: &str) { println!("{g}{b}[OK ]{e} {g}{m}{e}", g = GREEN, b = BOLD, e = END, m = msg); }
fn print_warn(msg: &str) { println!("{y}{b}[WARN ]{e} {y}{m}{e}", y = YELLOW, b = BOLD, e = END, m = msg); }
fn print_error(msg: &str) { println!("{r}{b}[ERROR]{e} {r}{m}{e}", r = RED, b = BOLD, e = END, m = msg); }