Skip to content

Commit 304b991

Browse files
committed
feat: refactored for code quality 2
1 parent bbf7f69 commit 304b991

File tree

5 files changed

+29
-43
lines changed

5 files changed

+29
-43
lines changed

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

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::cli::chat::tools::{
2323
use crate::cli::chat::util::truncate_safe;
2424
use crate::os::Os;
2525

26-
use super::user_agent_env_vars;
26+
use super::env_vars_with_user_agent;
2727

2828
// Platform-specific modules
2929
#[cfg(windows)]
@@ -130,9 +130,8 @@ impl ExecuteCommand {
130130
false
131131
}
132132

133-
pub async fn invoke(&self, output: &mut impl Write) -> Result<InvokeOutput> {
134-
let os = Os::new().await?;
135-
let output = run_command(&os, &self.command, MAX_TOOL_RESPONSE_SIZE / 3, Some(output)).await?;
133+
pub async fn invoke(&self, os: &Os, output: &mut impl Write) -> Result<InvokeOutput> {
134+
let output = run_command(os, &self.command, MAX_TOOL_RESPONSE_SIZE / 3, Some(output)).await?;
136135
let clean_stdout = sanitize_unicode_tags(&output.stdout);
137136
let clean_stderr = sanitize_unicode_tags(&output.stderr);
138137

@@ -397,19 +396,14 @@ mod tests {
397396

398397
let os = Os::new().await.unwrap();
399398

400-
// Test that user_agent_env_vars sets the AWS_EXECUTION_ENV variable correctly
401-
let env_vars = user_agent_env_vars(&os);
399+
// Test that env_vars_with_user_agent sets the AWS_EXECUTION_ENV variable correctly
400+
let env_vars = env_vars_with_user_agent(&os);
402401

403402
// Check that AWS_EXECUTION_ENV is set
404403
assert!(env_vars.contains_key(USER_AGENT_ENV_VAR));
405404

406405
let user_agent_value = env_vars.get(USER_AGENT_ENV_VAR).unwrap();
407406

408-
// Check that it contains our app name and version
409-
assert!(user_agent_value.contains(USER_AGENT_APP_NAME));
410-
assert!(user_agent_value.contains(USER_AGENT_VERSION_KEY));
411-
assert!(user_agent_value.contains(USER_AGENT_VERSION_VALUE));
412-
413407
// Check the format is correct
414408
let expected_metadata = format!(
415409
"{} {}/{}",
@@ -423,27 +417,15 @@ mod tests {
423417
use crate::cli::chat::consts::{
424418
USER_AGENT_APP_NAME, USER_AGENT_ENV_VAR,
425419
};
426-
use crate::os::Env;
427-
428-
// Create a fake environment with an existing AWS_EXECUTION_ENV value
429-
let env = Env::from_slice(&[(USER_AGENT_ENV_VAR, "ExistingValue")]);
430420

431-
// Create a minimal Os struct for testing - we'll manually construct it
432-
let fs = crate::os::Fs::new();
433-
let mut database = crate::database::Database::new().await.unwrap();
434-
let client = crate::api_client::ApiClient::new(&env, &fs, &mut database, None).await.unwrap();
435-
let telemetry = crate::telemetry::TelemetryThread::new(&env, &fs, &mut database).await.unwrap();
421+
let os = Os::new().await.unwrap();
436422

437-
let os = crate::os::Os {
438-
env,
439-
fs,
440-
sysinfo: crate::os::SysInfo::new(),
441-
database,
442-
client,
443-
telemetry,
444-
};
423+
// Set an existing AWS_EXECUTION_ENV value (safe because Os uses in-memory hashmap in tests)
424+
unsafe {
425+
os.env.set_var(USER_AGENT_ENV_VAR, "ExistingValue");
426+
}
445427

446-
let env_vars = user_agent_env_vars(&os);
428+
let env_vars = env_vars_with_user_agent(&os);
447429
let user_agent_value = env_vars.get(USER_AGENT_ENV_VAR).unwrap();
448430

449431
// Should contain both the existing value and our metadata

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tracing::error;
1313
use super::{
1414
CommandResult,
1515
format_output,
16-
user_agent_env_vars,
16+
env_vars_with_user_agent,
1717
};
1818
use crate::os::Os;
1919

@@ -33,7 +33,7 @@ pub async fn run_command<W: Write>(
3333
let shell = std::env::var("AMAZON_Q_CHAT_SHELL").unwrap_or("bash".to_string());
3434

3535
// Set up environment variables with user agent metadata for CloudTrail tracking
36-
let env_vars = user_agent_env_vars(os);
36+
let env_vars = env_vars_with_user_agent(os);
3737

3838
// We need to maintain a handle on stderr and stdout, but pipe it to the terminal as well
3939
let mut child = tokio::process::Command::new(shell)
@@ -129,10 +129,12 @@ pub async fn run_command<W: Write>(
129129
mod tests {
130130
use crate::cli::chat::tools::OutputKind;
131131
use crate::cli::chat::tools::execute::ExecuteCommand;
132+
use crate::os::Os;
132133

133134
#[ignore = "todo: fix failing on musl for some reason"]
134135
#[tokio::test]
135136
async fn test_execute_bash_tool() {
137+
let os = Os::new().await.unwrap();
136138
let mut stdout = std::io::stdout();
137139

138140
// Verifying stdout
@@ -141,7 +143,7 @@ mod tests {
141143
});
142144
let out = serde_json::from_value::<ExecuteCommand>(v)
143145
.unwrap()
144-
.invoke(&mut stdout)
146+
.invoke(&os, &mut stdout)
145147
.await
146148
.unwrap();
147149

@@ -159,7 +161,7 @@ mod tests {
159161
});
160162
let out = serde_json::from_value::<ExecuteCommand>(v)
161163
.unwrap()
162-
.invoke(&mut stdout)
164+
.invoke(&os, &mut stdout)
163165
.await
164166
.unwrap();
165167

@@ -177,7 +179,7 @@ mod tests {
177179
});
178180
let out = serde_json::from_value::<ExecuteCommand>(v)
179181
.unwrap()
180-
.invoke(&mut stdout)
182+
.invoke(&os, &mut stdout)
181183
.await
182184
.unwrap();
183185
if let OutputKind::Json(json) = out.output {

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tracing::error;
1313
use super::{
1414
CommandResult,
1515
format_output,
16-
user_agent_env_vars,
16+
env_vars_with_user_agent,
1717
};
1818
use crate::os::Os;
1919

@@ -32,7 +32,7 @@ pub async fn run_command<W: Write>(
3232
) -> Result<CommandResult> {
3333

3434
// Set up environment variables with user agent metadata for CloudTrail tracking
35-
let env_vars = user_agent_env_vars(os);
35+
let env_vars = env_vars_with_user_agent(os);
3636

3737
// We need to maintain a handle on stderr and stdout, but pipe it to the terminal as well
3838
let mut child = tokio::process::Command::new("cmd")
@@ -124,9 +124,11 @@ pub async fn run_command<W: Write>(
124124
mod tests {
125125
use crate::cli::chat::tools::OutputKind;
126126
use crate::cli::chat::tools::execute::ExecuteCommand;
127+
use crate::os::Os;
127128

128129
#[tokio::test]
129130
async fn test_execute_cmd_tool() {
131+
let os = Os::new().await.unwrap();
130132
let mut stdout = std::io::stdout();
131133

132134
// Verifying stdout
@@ -135,7 +137,7 @@ mod tests {
135137
});
136138
let out = serde_json::from_value::<ExecuteCommand>(v)
137139
.unwrap()
138-
.invoke(&mut stdout)
140+
.invoke(&os, &mut stdout)
139141
.await
140142
.unwrap();
141143

@@ -153,7 +155,7 @@ mod tests {
153155
});
154156
let out = serde_json::from_value::<ExecuteCommand>(v)
155157
.unwrap()
156-
.invoke(&mut stdout)
158+
.invoke(&os, &mut stdout)
157159
.await
158160
.unwrap();
159161

@@ -171,7 +173,7 @@ mod tests {
171173
});
172174
let out = serde_json::from_value::<ExecuteCommand>(v)
173175
.unwrap()
174-
.invoke(&mut stdout)
176+
.invoke(&os, &mut stdout)
175177
.await
176178
.unwrap();
177179
if let OutputKind::Json(json) = out.output {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Tool {
114114
match self {
115115
Tool::FsRead(fs_read) => fs_read.invoke(os, stdout).await,
116116
Tool::FsWrite(fs_write) => fs_write.invoke(os, stdout).await,
117-
Tool::ExecuteCommand(execute_command) => execute_command.invoke(stdout).await,
117+
Tool::ExecuteCommand(execute_command) => execute_command.invoke(os, stdout).await,
118118
Tool::UseAws(use_aws) => use_aws.invoke(os, stdout).await,
119119
Tool::Custom(custom_tool) => custom_tool.invoke(os, stdout).await,
120120
Tool::GhIssue(gh_issue) => gh_issue.invoke(os, stdout).await,
@@ -422,7 +422,7 @@ pub fn queue_function_result(result: &str, updates: &mut impl Write, is_error: b
422422
}
423423

424424
/// Helper function to set up environment variables with user agent metadata for CloudTrail tracking
425-
pub fn user_agent_env_vars(os: &Os) -> std::collections::HashMap<String, String> {
425+
pub fn env_vars_with_user_agent(os: &Os) -> std::collections::HashMap<String, String> {
426426
let mut env_vars: std::collections::HashMap<String, String> = std::env::vars().collect();
427427

428428
// Set up additional metadata for the AWS CLI user agent

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::cli::agent::{
2929
};
3030
use crate::os::Os;
3131

32-
use super::user_agent_env_vars;
32+
use super::env_vars_with_user_agent;
3333

3434
const READONLY_OPS: [&str; 6] = ["get", "describe", "list", "ls", "search", "batch_get"];
3535

@@ -54,7 +54,7 @@ impl UseAws {
5454
let mut command = tokio::process::Command::new("aws");
5555

5656
// Set up environment variables with user agent metadata for CloudTrail tracking
57-
let env_vars = user_agent_env_vars(os);
57+
let env_vars = env_vars_with_user_agent(os);
5858

5959
command.envs(env_vars).arg("--region").arg(&self.region);
6060
if let Some(profile_name) = self.profile_name.as_deref() {

0 commit comments

Comments
 (0)