Skip to content

Commit 3a7242b

Browse files
committed
Add new env_var.rs module with semantic helper functions
- Replace direct std::env calls with abstracted functions environment variables including Q_*, AMAZON_Q_*
1 parent a275492 commit 3a7242b

File tree

17 files changed

+118
-27
lines changed

17 files changed

+118
-27
lines changed

crates/chat-cli/src/api_client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl ApiClient {
138138
model_cache: Arc::new(RwLock::new(None)),
139139
};
140140

141-
if let Ok(json) = env.get("Q_MOCK_CHAT_RESPONSE") {
141+
if let Some(json) = crate::util::env_var::get_mock_chat_response() {
142142
this.set_mock_output(serde_json::from_str(fs.read_to_string(json).await.unwrap().as_str()).unwrap());
143143
}
144144

crates/chat-cli/src/auth/builder_id.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use crate::database::{
6363
Database,
6464
Secret,
6565
};
66+
use crate::util::env_var::is_sigv4_enabled;
6667

6768
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
6869
pub enum OAuthFlow {
@@ -545,7 +546,7 @@ pub async fn poll_create_token(
545546

546547
pub async fn is_logged_in(database: &mut Database) -> bool {
547548
// Check for BuilderId if not using Sigv4
548-
if std::env::var("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty()) {
549+
if is_sigv4_enabled() {
549550
debug!("logged in using sigv4 credentials");
550551
return true;
551552
}

crates/chat-cli/src/cli/chat/cli/editor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::cli::chat::{
1111
ChatState,
1212
};
1313
use crate::theme::StyledText;
14+
use crate::util::env_var::get_editor;
1415

1516
#[deny(missing_docs)]
1617
#[derive(Debug, PartialEq, Args)]
@@ -86,7 +87,7 @@ impl EditorArgs {
8687
/// Launch the user's preferred editor with the given file path
8788
fn launch_editor(file_path: &std::path::Path) -> Result<(), ChatError> {
8889
// Get the editor from environment variable or use a default
89-
let editor_cmd = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
90+
let editor_cmd = get_editor();
9091

9192
// Parse the editor command to handle arguments
9293
let mut parts =

crates/chat-cli/src/cli/chat/tools/delegate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use crate::cli::{
4141
};
4242
use crate::os::Os;
4343
use crate::theme::StyledText;
44+
use crate::util::env_var::get_all_env_vars;
4445

4546
/// Launch and manage async agent processes. Delegate tasks to agents that run independently in
4647
/// background.
@@ -333,7 +334,7 @@ pub async fn spawn_agent_process(os: &Os, agent: &str, task: &str) -> Result<Age
333334
cmd.stdout(std::process::Stdio::piped());
334335
cmd.stderr(std::process::Stdio::piped());
335336
cmd.stdin(std::process::Stdio::null()); // No user input
336-
cmd.envs(std::env::vars());
337+
cmd.envs(get_all_env_vars());
337338

338339
#[cfg(not(windows))]
339340
cmd.process_group(0);

crates/chat-cli/src/cli/chat/tools/execute/unix.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::{
1616
format_output,
1717
};
1818
use crate::os::Os;
19+
use crate::util::env_var::get_chat_shell;
1920

2021
/// Run a bash command on Unix systems.
2122
/// # Arguments
@@ -30,7 +31,7 @@ pub async fn run_command<W: Write>(
3031
max_result_size: usize,
3132
mut updates: Option<W>,
3233
) -> Result<CommandResult> {
33-
let shell = std::env::var("AMAZON_Q_CHAT_SHELL").unwrap_or("bash".to_string());
34+
let shell = get_chat_shell();
3435

3536
// Set up environment variables with user agent metadata for CloudTrail tracking
3637
let env_vars = env_vars_with_user_agent(os);

crates/chat-cli/src/cli/chat/tools/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ fn format_path(cwd: impl AsRef<Path>, path: impl AsRef<Path>) -> String {
444444
.unwrap_or(path.as_ref().to_string_lossy().to_string())
445445
}
446446

447-
fn supports_truecolor(os: &Os) -> bool {
447+
fn supports_truecolor(_os: &Os) -> bool {
448448
// Simple override to disable truecolor since shell_color doesn't use Context.
449-
!os.env.get("Q_DISABLE_TRUECOLOR").is_ok_and(|s| !s.is_empty())
449+
!crate::util::env_var::is_truecolor_disabled()
450450
&& shell_color::get_color_support().contains(shell_color::ColorSupport::TERM24BIT)
451451
}
452452

crates/chat-cli/src/cli/chat/util/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use eyre::Result;
1616

1717
use super::ChatError;
1818
use super::token_counter::TokenCounter;
19+
use crate::util::env_var::get_term;
1920

2021
pub fn truncate_safe(s: &str, max_bytes: usize) -> &str {
2122
if s.len() <= max_bytes {
@@ -119,7 +120,7 @@ pub fn play_notification_bell(requires_confirmation: bool) {
119120
/// Determine if we should play the bell based on terminal type
120121
fn should_play_bell() -> bool {
121122
// Get the TERM environment variable
122-
if let Ok(term) = std::env::var("TERM") {
123+
if let Some(term) = get_term() {
123124
// List of terminals known to handle bell character well
124125
let bell_compatible_terms = [
125126
"xterm",

crates/chat-cli/src/cli/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
use crate::theme::StyledText;
2+
use crate::util::env_var::{
3+
get_var,
4+
is_log_stdout_enabled,
5+
};
26
mod agent;
37
pub mod chat;
48
mod debug;
@@ -231,7 +235,7 @@ impl Cli {
231235
),
232236
false => None,
233237
},
234-
log_to_stdout: std::env::var_os("Q_LOG_STDOUT").is_some() || self.verbose > 0,
238+
log_to_stdout: is_log_stdout_enabled() || self.verbose > 0,
235239
log_file_path: match subcommand {
236240
RootSubcommand::Chat { .. } => Some(logs_dir().expect("home dir must be set").join("qchat.log")),
237241
_ => None,
@@ -240,7 +244,7 @@ impl Cli {
240244
});
241245

242246
// Check for region support.
243-
if let Ok(region) = std::env::var("AWS_REGION") {
247+
if let Ok(region) = get_var("AWS_REGION") {
244248
if GOV_REGIONS.contains(&region.as_str()) {
245249
bail!("AWS GovCloud ({region}) is not supported.")
246250
}

crates/chat-cli/src/logging.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tracing_subscriber::{
1414
fmt,
1515
};
1616

17-
use crate::util::env_var::Q_LOG_LEVEL;
17+
use crate::util::env_var::get_log_level as get_env_log_level;
1818

1919
const MAX_FILE_SIZE: u64 = 10 * 1024 * 1024;
2020
const DEFAULT_FILTER: LevelFilter = LevelFilter::ERROR;
@@ -196,7 +196,7 @@ pub fn get_log_level() -> String {
196196
.lock()
197197
.unwrap()
198198
.clone()
199-
.unwrap_or_else(|| std::env::var(Q_LOG_LEVEL).unwrap_or_else(|_| DEFAULT_FILTER.to_string()))
199+
.unwrap_or_else(|| get_env_log_level().unwrap_or_else(|_| DEFAULT_FILTER.to_string()))
200200
}
201201

202202
/// Set the log level to the given level.
@@ -247,7 +247,7 @@ fn create_filter_layer() -> EnvFilter {
247247
.lock()
248248
.unwrap()
249249
.clone()
250-
.or_else(|| std::env::var(Q_LOG_LEVEL).ok());
250+
.or_else(|| get_env_log_level().ok());
251251

252252
match log_level {
253253
Some(level) => EnvFilter::builder()

crates/chat-cli/src/mcp_client/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use crate::cli::chat::tools::custom_tool::{
6060
};
6161
use crate::os::Os;
6262
use crate::util::directories::DirectoryError;
63+
use crate::util::env_var::get_all_env_vars;
6364

6465
/// Fetches all pages of specified resources from a server
6566
macro_rules! paginated_fetch {
@@ -431,7 +432,7 @@ impl McpClientService {
431432
process_env_vars(envs, &os.env);
432433
cmd.envs(envs);
433434
}
434-
cmd.envs(std::env::vars()).args(args);
435+
cmd.envs(get_all_env_vars()).args(args);
435436

436437
#[cfg(not(windows))]
437438
cmd.process_group(0);

0 commit comments

Comments
 (0)