Skip to content

Commit 3eb52b7

Browse files
committed
refactor: consolidate env across codebase
1 parent 6519af5 commit 3eb52b7

File tree

23 files changed

+196
-49
lines changed

23 files changed

+196
-49
lines changed

crates/alacritty_terminal/src/term/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ impl<T> Term<T> {
834834

835835
// Add work around for emojis
836836
if let Ok(cursor_offset) = std::env::var("Q_PROMPT_OFFSET_WORKAROUND") {
837+
// ALLOWED: alacritty_terminal doesn't have fig_os_shim dependency
837838
if let Ok(offset) = cursor_offset.parse::<i32>() {
838839
self.shell_state.cmd_cursor = self.shell_state.cmd_cursor.map(|cursor| Point {
839840
column: Column((cursor.column.0 as i32 - offset).max(0) as usize),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub async fn poll_create_token(
530530

531531
pub async fn is_logged_in(database: &mut Database) -> bool {
532532
// Check for BuilderId if not using Sigv4
533-
if std::env::var("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty()) {
533+
if crate::os::Env::new().amazon_q_sigv4() {
534534
debug!("logged in using sigv4 credentials");
535535
return true;
536536
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub async fn run_command<W: Write>(
2727
max_result_size: usize,
2828
mut updates: Option<W>,
2929
) -> Result<CommandResult> {
30-
let shell = std::env::var("AMAZON_Q_CHAT_SHELL").unwrap_or("bash".to_string());
30+
let shell = crate::os::Env::new().amazon_q_chat_shell();
3131

3232
// We need to maintain a handle on stderr and stdout, but pipe it to the terminal as well
3333
let mut child = tokio::process::Command::new(shell)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ impl Cli {
214214
),
215215
false => None,
216216
},
217-
log_to_stdout: std::env::var_os("Q_LOG_STDOUT").is_some() || self.verbose > 0,
217+
log_to_stdout: crate::os::Env::new().q_log_stdout() || self.verbose > 0,
218218
log_file_path: match subcommand {
219219
RootSubcommand::Chat { .. } => Some(logs_dir().expect("home dir must be set").join("qchat.log")),
220220
_ => None,

crates/chat-cli/src/logging.rs

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

17-
use crate::util::env_var::Q_LOG_LEVEL;
18-
1917
const MAX_FILE_SIZE: u64 = 10 * 1024 * 1024;
2018
const DEFAULT_FILTER: LevelFilter = LevelFilter::ERROR;
2119

@@ -192,11 +190,11 @@ pub fn initialize_logging<T: AsRef<Path>>(args: LogArgs<T>) -> Result<LogGuard,
192190
///
193191
/// Returns a string identifying the current log level.
194192
pub fn get_log_level() -> String {
195-
Q_LOG_LEVEL_GLOBAL
196-
.lock()
197-
.unwrap()
198-
.clone()
199-
.unwrap_or_else(|| std::env::var(Q_LOG_LEVEL).unwrap_or_else(|_| DEFAULT_FILTER.to_string()))
193+
Q_LOG_LEVEL_GLOBAL.lock().unwrap().clone().unwrap_or_else(|| {
194+
crate::os::Env::new()
195+
.q_log_level()
196+
.unwrap_or_else(|_| DEFAULT_FILTER.to_string())
197+
})
200198
}
201199

202200
/// Set the log level to the given level.
@@ -247,7 +245,7 @@ fn create_filter_layer() -> EnvFilter {
247245
.lock()
248246
.unwrap()
249247
.clone()
250-
.or_else(|| std::env::var(Q_LOG_LEVEL).ok());
248+
.or_else(|| crate::os::Env::new().q_log_level().ok());
251249

252250
match log_level {
253251
Some(level) => EnvFilter::builder()

crates/chat-cli/src/os/env.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,83 @@ impl Env {
158158
pub fn in_appimage(&self) -> bool {
159159
self.get_os("APPIMAGE").is_some()
160160
}
161+
162+
// Q-specific environment variable methods
163+
pub fn q_fake_is_remote(&self) -> bool {
164+
self.get_os("Q_FAKE_IS_REMOTE").is_some()
165+
}
166+
167+
pub fn q_log_level(&self) -> Result<String, VarError> {
168+
self.get("Q_LOG_LEVEL")
169+
}
170+
171+
pub fn q_log_stdout(&self) -> bool {
172+
self.get_os("Q_LOG_STDOUT").is_some()
173+
}
174+
175+
pub fn amazon_q_sigv4(&self) -> bool {
176+
self.get("AMAZON_Q_SIGV4").is_ok_and(|v| !v.is_empty())
177+
}
178+
179+
pub fn amazon_q_chat_shell(&self) -> String {
180+
self.get("AMAZON_Q_CHAT_SHELL").unwrap_or_else(|_| "bash".to_string())
181+
}
182+
183+
pub fn q_cli_client_application(&self) -> Result<String, VarError> {
184+
self.get("Q_CLI_CLIENT_APPLICATION")
185+
}
186+
187+
pub fn q_parent(&self) -> Result<String, VarError> {
188+
self.get("Q_PARENT")
189+
}
190+
191+
pub fn q_term(&self) -> Result<String, VarError> {
192+
self.get("Q_TERM")
193+
}
194+
195+
pub fn q_using_zsh_autosuggestions(&self) -> bool {
196+
self.get_os("Q_USING_ZSH_AUTOSUGGESTIONS").is_some()
197+
}
198+
199+
pub fn q_init_snapshot_test(&self) -> bool {
200+
self.get_os("Q_INIT_SNAPSHOT_TEST").is_some()
201+
}
202+
203+
pub fn q_desktop_release_url(&self) -> Result<String, VarError> {
204+
self.get("Q_DESKTOP_RELEASE_URL")
205+
}
206+
207+
pub fn q_inline_shell_completion_cache_enabled(&self) -> bool {
208+
self.get_os("Q_INLINE_SHELL_COMPLETION_CACHE_DISABLE").is_none()
209+
}
210+
211+
pub fn q_inline_shell_completion_history_count(&self) -> Result<String, VarError> {
212+
self.get("Q_INLINE_SHELL_COMPLETION_HISTORY_COUNT")
213+
}
214+
215+
pub fn q_inline_shell_completion_debounce_ms(&self) -> Result<String, VarError> {
216+
self.get("Q_INLINE_SHELL_COMPLETION_DEBOUNCE_MS")
217+
}
218+
219+
pub fn q_backend(&self) -> Result<String, VarError> {
220+
self.get("Q_BACKEND")
221+
}
222+
223+
pub fn q_prompt_offset_workaround(&self) -> Result<String, VarError> {
224+
self.get("Q_PROMPT_OFFSET_WORKAROUND")
225+
}
226+
227+
pub fn q_use_sendmessage(&self) -> bool {
228+
self.get("Q_USE_SENDMESSAGE").is_ok_and(|v| !v.is_empty())
229+
}
230+
231+
pub fn q_custom_cert(&self) -> Result<String, VarError> {
232+
self.get("Q_CUSTOM_CERT")
233+
}
234+
235+
pub fn has_q_parent(&self) -> bool {
236+
self.q_parent().is_ok()
237+
}
161238
}
162239

163240
impl Default for Env {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ pub use crate::telemetry::core::{
6666
QProfileSwitchIntent,
6767
TelemetryResult,
6868
};
69-
use crate::util::env_var::Q_CLI_CLIENT_APPLICATION;
7069
use crate::util::system_info::os_version;
7170

7271
#[derive(thiserror::Error, Debug)]
@@ -381,7 +380,7 @@ async fn set_event_metadata(database: &Database, event: &mut Event) {
381380
}
382381

383382
// Set the client application from environment variable
384-
if let Ok(client_app) = std::env::var(Q_CLI_CLIENT_APPLICATION) {
383+
if let Ok(client_app) = crate::os::Env::new().q_cli_client_application() {
385384
event.set_client_application(client_app);
386385
}
387386
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,15 @@ pub fn in_wsl() -> bool {
175175
/// Is the calling binary running on a remote instance
176176
pub fn is_remote() -> bool {
177177
// TODO(chay): Add detection for inside docker container
178-
in_ssh() || in_wsl() || std::env::var_os("Q_FAKE_IS_REMOTE").is_some()
178+
in_ssh() || in_wsl() || crate::os::Env::new().q_fake_is_remote()
179179
}
180180

181181
pub fn in_codespaces() -> bool {
182182
static IN_CODESPACES: OnceLock<bool> = OnceLock::new();
183-
*IN_CODESPACES
184-
.get_or_init(|| std::env::var_os("CODESPACES").is_some() || std::env::var_os("Q_CODESPACES").is_some())
183+
*IN_CODESPACES.get_or_init(|| std::env::var_os("CODESPACES").is_some() || crate::os::Env::new().in_codespaces())
185184
}
186185

187186
pub fn in_ci() -> bool {
188187
static IN_CI: OnceLock<bool> = OnceLock::new();
189-
*IN_CI.get_or_init(|| std::env::var_os("CI").is_some() || std::env::var_os("Q_CI").is_some())
188+
*IN_CI.get_or_init(|| std::env::var_os("CI").is_some() || crate::os::Env::new().in_ci())
190189
}

crates/fig_api_client/src/clients/streaming_client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl StreamingClient {
6161
let client = if fig_util::system_info::in_cloudshell()
6262
|| std::env::var("Q_USE_SENDMESSAGE").is_ok_and(|v| !v.is_empty())
6363
{
64+
// ALLOWED: fig_api_client doesn't have fig_os_shim dependency
6465
Self::new_qdeveloper_client(&Endpoint::load_q()).await?
6566
} else {
6667
Self::new_codewhisperer_client(&Endpoint::load_codewhisperer()).await

crates/fig_desktop/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async fn main() -> ExitCode {
149149

150150
#[cfg(target_os = "linux")]
151151
{
152-
match std::env::var("Q_BACKEND").ok().as_deref() {
152+
match fig_os_shim::Env::new().q_backend().ok().as_deref() {
153153
Some("default") => {},
154154
// SAFETY: we are calling set_var in a single-threaded context.
155155
Some(backend) => unsafe { std::env::set_var("GDK_BACKEND", backend) },

0 commit comments

Comments
 (0)