forked from alt-art/commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
79 lines (68 loc) · 1.86 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#![warn(
clippy::all,
clippy::pedantic,
clippy::nursery,
clippy::cargo,
clippy::str_to_string
)]
#![allow(clippy::module_name_repetitions)]
mod commit;
mod commit_message;
mod config;
use anyhow::anyhow;
use anyhow::Result;
use clap::Parser;
use colored::Colorize;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use commit_message::make_message_commit;
const DEFAULT_CONFIG_FILE: &str = include_str!("../commit-default.json");
#[derive(Parser)]
#[clap(about, author, version)]
struct Opt {
#[clap(
short,
long,
help = "Custom configuration file path",
parse(from_os_str)
)]
config: Option<PathBuf>,
#[clap(long, help = "Init custom configuration file")]
init: bool,
#[clap(short, long, help = "Use as hook")]
hook: bool,
}
fn main() -> Result<()> {
// Dumb hack to set the current/working directory (pwd) because appimage or cargo-appimage sucks
// https://github.com/AppImage/AppImageKit/issues/172
if let Ok(current_dir) = std::env::var("OWD") {
std::env::set_current_dir(current_dir)?;
}
if let Some(0) = Command::new("git")
.args(["diff", "--cached", "--quiet"])
.output()
.expect("failed to execute process")
.status
.code()
{
return Err(anyhow!(
"{}",
"You have not added anything please do `git add`".red()
));
}
let opt = Opt::parse();
if opt.init {
let mut file = std::fs::File::create("commit.json")?;
file.write_all(DEFAULT_CONFIG_FILE.as_bytes())?;
return Ok(());
}
let pattern = config::get_pattern(opt.config)?;
let commit_message = make_message_commit(pattern)?;
if opt.hook {
commit::commit_as_hook(&commit_message)?;
return Ok(());
}
commit::commit(&commit_message)?;
Ok(())
}