Skip to content

Commit 86dbb88

Browse files
committed
refactor(utils): extract environment utilities into submodule
- move get_env and get_env_bool into new env module as get and get_bool - add exists function to env module for environment variable checks - update all function calls across repository, openai, and main modules - remove old standalone functions and refactor check_env_variables Signed-off-by: mingcheng <mingcheng@apache.org>
1 parent 5334e4e commit 86dbb88

File tree

4 files changed

+59
-50
lines changed

4 files changed

+59
-50
lines changed

src/git/repository.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* File Created: 2025-10-16 15:07:05
1010
*
1111
* Modified By: mingcheng <mingcheng@apache.org>
12-
* Last Modified: 2025-11-07 11:09:51
12+
* Last Modified: 2025-11-07 14:28:13
1313
*/
1414

1515
use git2::{Oid, Repository as _Repo, RepositoryOpenFlags, Signature};
@@ -19,7 +19,7 @@ use std::fmt::{Display, Formatter};
1919
use tracing::{trace, warn};
2020

2121
use crate::git::message::GitMessage;
22-
use crate::utils::{self, get_env};
22+
use crate::utils::env;
2323

2424
/// Author information from git configuration
2525
pub struct Author {
@@ -138,7 +138,7 @@ impl Repository {
138138
})
139139
.unwrap_or_else(|_| {
140140
warn!("using default email: {}", UNKNOWN_EMAIL);
141-
get_env("GIT_FALLBACK_EMAIL", UNKNOWN_EMAIL)
141+
env::get("GIT_FALLBACK_EMAIL", UNKNOWN_EMAIL)
142142
});
143143

144144
// Validate email format using regex
@@ -149,7 +149,7 @@ impl Repository {
149149
email
150150
} else {
151151
warn!("invalid email format: {}, using default", UNKNOWN_EMAIL);
152-
get_env("GIT_FALLBACK_EMAIL", UNKNOWN_EMAIL)
152+
env::get("GIT_FALLBACK_EMAIL", UNKNOWN_EMAIL)
153153
};
154154

155155
// Try to get user.name from config, fall back to environment or default
@@ -167,7 +167,7 @@ impl Repository {
167167
// Detect if name is empty or whitespace
168168
let name = if name.trim().is_empty() {
169169
warn!("author name is empty, using default: Unknown Author");
170-
get_env("GIT_FALLBACK_NAME", UNKNOWN_AUTHOR)
170+
env::get("GIT_FALLBACK_NAME", UNKNOWN_AUTHOR)
171171
} else {
172172
name
173173
};
@@ -257,7 +257,7 @@ impl Repository {
257257
}
258258

259259
// Fall back to environment variable
260-
utils::get_env_bool("AIGITCOMMIT_SIGNOFF")
260+
env::get_bool("AIGITCOMMIT_SIGNOFF")
261261
}
262262

263263
/// Get the list of filenames to exclude from diffs

src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* File Created: 2025-03-01 17:17:30
1010
*
1111
* Modified By: mingcheng <mingcheng@apache.org>
12-
* Last Modified: 2025-11-07 11:37:33
12+
* Last Modified: 2025-11-07 14:29:29
1313
*/
1414

1515
use aigitcommit::built_info::{PKG_NAME, PKG_VERSION};
@@ -29,7 +29,7 @@ use std::io::Write;
2929
use tracing::{Level, debug, error, info, trace};
3030

3131
use aigitcommit::utils::{
32-
OutputFormat, check_env_variables, format_openai_error, get_env, save_to_file, should_signoff,
32+
OutputFormat, check_env_variables, env, format_openai_error, save_to_file, should_signoff,
3333
};
3434

3535
#[tokio::main]
@@ -51,7 +51,7 @@ async fn main() -> std::result::Result<(), Box<dyn Error>> {
5151
}
5252

5353
// Get the specified model name from environment variable, default to "gpt-5"
54-
let model_name = get_env("OPENAI_MODEL_NAME", "gpt-5");
54+
let model_name = env::get("OPENAI_MODEL_NAME", "gpt-5");
5555

5656
// Instantiate OpenAI client, ready to send requests to the OpenAI API
5757
let client = openai::OpenAI::new();

src/openai.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414

1515
use crate::built_info;
16-
use crate::utils::get_env;
16+
use crate::utils::env;
1717
use askama::Template;
1818
use async_openai::config::OPENAI_API_BASE;
1919
use async_openai::error::OpenAIError;
@@ -50,9 +50,9 @@ impl OpenAI {
5050
/// This function sets up the OpenAI client with the API key, base URL, and optional proxy settings.
5151
pub fn new() -> Self {
5252
// Set up OpenAI client configuration
53-
let ai_config = OpenAIConfig::new()
54-
.with_api_key(get_env("OPENAI_API_TOKEN", ""))
55-
.with_api_base(get_env("OPENAI_API_BASE", OPENAI_API_BASE))
53+
let ai_config: OpenAIConfig = OpenAIConfig::new()
54+
.with_api_key(env::get("OPENAI_API_TOKEN", ""))
55+
.with_api_base(env::get("OPENAI_API_BASE", OPENAI_API_BASE))
5656
.with_org_id(built_info::PKG_NAME);
5757

5858
// Set up HTTP client builder with default headers
@@ -74,14 +74,14 @@ impl OpenAI {
7474
});
7575

7676
// Set up proxy if specified
77-
let proxy_addr = get_env("OPENAI_API_PROXY", "");
77+
let proxy_addr = env::get("OPENAI_API_PROXY", "");
7878
if !proxy_addr.is_empty() {
7979
trace!("Using proxy: {proxy_addr}");
8080
http_client_builder = http_client_builder.proxy(Proxy::all(proxy_addr).unwrap());
8181
}
8282

8383
// Set up request timeout if specified
84-
let request_timeout = get_env("OPENAI_REQUEST_TIMEOUT", "");
84+
let request_timeout = env::get("OPENAI_REQUEST_TIMEOUT", "");
8585
if !request_timeout.is_empty()
8686
&& let Ok(timeout) = request_timeout.parse::<u64>()
8787
{

src/utils.rs

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,51 @@
99
* File Created: 2025-10-21 11:34:11
1010
*
1111
* Modified By: mingcheng <mingcheng@apache.org>
12-
* Last Modified: 2025-11-07 11:22:27
12+
* Last Modified: 2025-11-07 14:31:43
1313
*/
1414

15-
use std::env;
16-
use std::io::Write;
17-
use tracing::debug;
18-
1915
use crate::git::message::GitMessage;
2016
use crate::git::repository::Repository;
17+
use std::io::Write;
2118

22-
/// Get environment variable with default value fallback
23-
pub fn get_env(key: &str, default: &str) -> String {
24-
env::var(key).unwrap_or_else(|_| default.to_string())
25-
}
19+
// Get environment variable with default value fallback
20+
pub mod env {
21+
use std::env;
22+
23+
use tracing::{debug, warn};
24+
25+
/// Get environment variable with default value fallback
26+
pub fn get(key: &str, default: &str) -> String {
27+
env::var(key).unwrap_or_else(|_| default.to_string())
28+
}
29+
30+
/// Parse boolean environment variable
31+
/// Accepts "1", "true", "yes", "on" (case-insensitive) as true
32+
pub fn get_bool(key: &str) -> bool {
33+
env::var(key)
34+
.map(|v| {
35+
v == "1"
36+
|| v.eq_ignore_ascii_case("true")
37+
|| v.eq_ignore_ascii_case("yes")
38+
|| v.eq_ignore_ascii_case("on")
39+
})
40+
.unwrap_or(false)
41+
}
2642

27-
/// Parse boolean environment variable
28-
/// Accepts "1", "true", "yes", "on" (case-insensitive) as true
29-
pub fn get_env_bool(key: &str) -> bool {
30-
env::var(key)
31-
.map(|v| {
32-
v == "1"
33-
|| v.eq_ignore_ascii_case("true")
34-
|| v.eq_ignore_ascii_case("yes")
35-
|| v.eq_ignore_ascii_case("on")
36-
})
37-
.unwrap_or(false)
43+
/// Check and print environment variable value
44+
pub fn exists(var_name: &str) -> bool {
45+
match env::var(var_name) {
46+
Ok(value) => {
47+
debug!("{} is set to {}", var_name, value);
48+
// println!("{:20}\t{}", var_name, value);
49+
true
50+
}
51+
Err(_) => {
52+
warn!("{} is not set", var_name);
53+
false
54+
}
55+
}
56+
}
3857
}
3958

4059
/// Check if commit should be signed off
@@ -49,6 +68,7 @@ pub enum OutputFormat {
4968
Stdout,
5069
Table,
5170
Json,
71+
// File
5272
}
5373

5474
impl OutputFormat {
@@ -94,19 +114,6 @@ pub fn print_table(title: &str, content: &str) {
94114
println!("{}", table);
95115
}
96116

97-
/// Check and print environment variable value
98-
fn check_and_print_env(var_name: &str) {
99-
match env::var(var_name) {
100-
Ok(value) => {
101-
debug!("{} is set to {}", var_name, value);
102-
println!("{:20}\t{}", var_name, value);
103-
}
104-
Err(_) => {
105-
debug!("{} is not set", var_name);
106-
}
107-
}
108-
}
109-
110117
/// Check and print all relevant environment variables
111118
pub fn check_env_variables() {
112119
[
@@ -119,7 +126,9 @@ pub fn check_env_variables() {
119126
"AIGITCOMMIT_SIGNOFF",
120127
]
121128
.iter()
122-
.for_each(|v| check_and_print_env(v));
129+
.for_each(|v| {
130+
env::exists(v);
131+
});
123132
}
124133

125134
/// Convert OpenAI error to user-friendly error message
@@ -182,7 +191,7 @@ Signed-off-by: mingcheng <mingcheng@apache.org>
182191

183192
#[test]
184193
fn test_get_env() {
185-
let result = get_env("NONEXISTENT_VAR_XYZ", "default_value");
194+
let result = env::get("NONEXISTENT_VAR_XYZ", "default_value");
186195
assert_eq!(result, "default_value");
187196
}
188197
}

0 commit comments

Comments
 (0)