Skip to content

Commit ed746a0

Browse files
committed
refactor: rename check flag to check_model and restructure code
- rename CLI flag from check to check_model for improved clarity - restructure model validation logic to run only when check_model flag is enabled - add environment variable check with tabular output for check_env flag - update default model name to gpt-5 for latest compatibility - bump package version to 1.5.1 Signed-off-by: mingcheng <[email protected]>
1 parent a1d302c commit ed746a0

File tree

4 files changed

+66
-24
lines changed

4 files changed

+66
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
1010

1111
[[package]]
1212
name = "aigitcommit"
13-
version = "1.5.0"
13+
version = "1.5.1"
1414
dependencies = [
1515
"arboard",
1616
"askama",

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "aigitcommit"
3-
version = "1.5.0"
3+
version = "1.5.1"
44
edition = "2024"
55
description = "A simple git commit message generator by OpenAI compaction model."
66
license-file = "LICENSE"

src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ If not specified, the current directory will be used"#,
4343
default_value_t = false,
4444
required = false
4545
)]
46-
pub check: bool,
46+
pub check_model: bool,
4747

4848
#[arg(
4949
long,

src/main.rs

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ use std::fs::File;
2929
use std::io::Write;
3030
use std::{env, fs};
3131
use tracing::{Level, debug, trace};
32+
mod built_info {
33+
include!(concat!(env!("OUT_DIR"), "/built.rs"));
34+
}
3235

3336
/// The output format for the commit message
3437
#[derive(Debug)]
@@ -67,6 +70,66 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
6770
);
6871
}
6972

73+
// Get the specified model name from environment variable, default to "gpt-4"
74+
let model_name = env::var("OPENAI_MODEL_NAME").unwrap_or_else(|_| String::from("gpt-5"));
75+
76+
// Instantiate OpenAI client, ready to send requests to the OpenAI API
77+
let client = openai::OpenAI::new();
78+
79+
// Check if the environment variables are set and print the configured values
80+
if cli.check_env {
81+
fn check_and_print_env(var_name: &str) {
82+
match env::var(var_name) {
83+
Ok(value) => {
84+
debug!("{} is set to {}", var_name, value);
85+
// Print the value of the environment variable
86+
println!("{:20}\t{}", var_name, value);
87+
}
88+
Err(_) => {
89+
debug!("{} is not set", var_name);
90+
}
91+
}
92+
}
93+
94+
trace!("check env option is enabled, will check the OpenAI API key and model name");
95+
debug!("the model name is `{}`", &model_name);
96+
97+
[
98+
"OPENAI_API_BASE",
99+
"OPENAI_API_TOKEN",
100+
"OPENAI_MODEL_NAME",
101+
"OPENAI_API_PROXY",
102+
"OPENAI_API_TIMEOUT",
103+
"OPENAI_API_MAX_TOKENS",
104+
"GIT_AUTO_SIGNOFF",
105+
]
106+
.iter()
107+
.for_each(|v| check_and_print_env(v));
108+
109+
return Ok(());
110+
}
111+
112+
// Check if the model name is valid
113+
if cli.check_model {
114+
trace!("check option is enabled, will check the OpenAI API key and model name");
115+
debug!("the model name is `{}`", &model_name);
116+
117+
match client.check_model(&model_name).await {
118+
Ok(()) => {
119+
println!(
120+
"the model name `{}` is available, {} is ready for use!",
121+
model_name,
122+
built_info::PKG_NAME
123+
);
124+
}
125+
Err(e) => {
126+
return Err(format!("the model name `{model_name}` is not available: {e}").into());
127+
}
128+
}
129+
130+
return Ok(());
131+
}
132+
70133
// Check if the specified path is a valid directory
71134
let repo_dir = fs::canonicalize(&cli.repo_path)?;
72135

@@ -96,30 +159,9 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
96159
return Err("no commit logs found".into());
97160
}
98161

99-
// Instantiate OpenAI client, ready to send requests to the OpenAI API
100-
let client = openai::OpenAI::new();
101-
102162
// Generate the prompt which will be sent to OpenAI API
103163
let content = OpenAI::prompt(&logs, &diffs)?;
104164

105-
// Get the specified model name from environment variable, default to "gpt-4"
106-
let model_name = env::var("OPENAI_MODEL_NAME").unwrap_or_else(|_| String::from("gpt-4"));
107-
108-
// Check if the model name is valid
109-
if cli.check {
110-
trace!("check option is enabled, will check the OpenAI API key and model name");
111-
debug!("the model name is `{}`", &model_name);
112-
113-
match client.check_model(&model_name).await {
114-
Ok(()) => {
115-
debug!("the model name `{}` is available", model_name);
116-
}
117-
Err(e) => {
118-
return Err(format!("the model name `{model_name}` is not available: {e}").into());
119-
}
120-
}
121-
}
122-
123165
// Load the system prompt from the template file
124166
let system_prompt = include_str!("../templates/system.txt");
125167

0 commit comments

Comments
 (0)