Skip to content

Commit f6c433c

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 f6c433c

File tree

17 files changed

+123
-26
lines changed

17 files changed

+123
-26
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
//! - Calls [Client::create_token]
2222
//! - RETURNS: [BuilderIdToken]
2323
24+
use crate::util::env_var::is_sigv4_enabled;
25+
2426
use aws_sdk_ssooidc::client::Client;
2527
use aws_sdk_ssooidc::config::retry::RetryConfig;
2628
use aws_sdk_ssooidc::config::{
@@ -545,7 +547,7 @@ pub async fn poll_create_token(
545547

546548
pub async fn is_logged_in(database: &mut Database) -> bool {
547549
// Check for BuilderId if not using Sigv4
548-
if std::env::var("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty()) {
550+
if is_sigv4_enabled() {
549551
debug!("logged in using sigv4 credentials");
550552
return true;
551553
}

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
@@ -16,6 +16,7 @@ use eyre::{
1616
Result,
1717
bail,
1818
};
19+
use crate::util::env_var::get_all_env_vars;
1920
use schemars::JsonSchema;
2021
use serde::{
2122
Deserialize,
@@ -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
@@ -10,6 +10,7 @@ use tokio::io::AsyncBufReadExt;
1010
use tokio::select;
1111
use tracing::error;
1212

13+
use crate::util::env_var::get_chat_shell;
1314
use super::{
1415
CommandResult,
1516
env_vars_with_user_agent,
@@ -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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use aws_smithy_types::{
1414
};
1515
use eyre::Result;
1616

17+
use crate::util::env_var::get_term;
18+
1719
use super::ChatError;
1820
use super::token_counter::TokenCounter;
1921

@@ -119,7 +121,7 @@ pub fn play_notification_bell(requires_confirmation: bool) {
119121
/// Determine if we should play the bell based on terminal type
120122
fn should_play_bell() -> bool {
121123
// Get the TERM environment variable
122-
if let Ok(term) = std::env::var("TERM") {
124+
if let Some(term) = get_term() {
123125
// List of terminals known to handle bell character well
124126
let bell_compatible_terms = [
125127
"xterm",

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::theme::StyledText;
2+
use crate::util::env_var::{is_log_stdout_enabled, get_var};
23
mod agent;
34
pub mod chat;
45
mod debug;
@@ -231,7 +232,7 @@ impl Cli {
231232
),
232233
false => None,
233234
},
234-
log_to_stdout: std::env::var_os("Q_LOG_STDOUT").is_some() || self.verbose > 0,
235+
log_to_stdout: is_log_stdout_enabled() || self.verbose > 0,
235236
log_file_path: match subcommand {
236237
RootSubcommand::Chat { .. } => Some(logs_dir().expect("home dir must be set").join("qchat.log")),
237238
_ => None,
@@ -240,7 +241,7 @@ impl Cli {
240241
});
241242

242243
// Check for region support.
243-
if let Ok(region) = std::env::var("AWS_REGION") {
244+
if let Ok(region) = get_var("AWS_REGION") {
244245
if GOV_REGIONS.contains(&region.as_str()) {
245246
bail!("AWS GovCloud ({region}) is not supported.")
246247
}

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
@@ -59,6 +59,7 @@ use crate::cli::chat::tools::custom_tool::{
5959
TransportType,
6060
};
6161
use crate::os::Os;
62+
use crate::util::env_var::get_all_env_vars;
6263
use crate::util::directories::DirectoryError;
6364

6465
/// Fetches all pages of specified resources from a server
@@ -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)