MCPcopy Create free account
hub / github.com/aws/amazon-q-developer-cli / process_path

Function process_path

crates/chat-cli/src/cli/chat/context.rs:285–363  ·  view source on GitHub ↗

Process a path, handling glob patterns and file types. This method: 1. Expands the path (handling ~ for home directory) 2. If the path contains glob patterns, expands them 3. For each resulting path, adds the file to the context collection 4. Handles directories by including all files in the directory (non-recursive) 5. With force=true, includes paths that don't exist yet # Arguments `path` - Th

(
    os: &Os,
    path: &str,
    context_files: &mut Vec<(String, String)>,
    is_validation: bool,
)

Source from the content-addressed store, hash-verified

283/// # Returns
284/// A Result indicating success or an error
285async fn process_path(
286 os: &Os,
287 path: &str,
288 context_files: &mut Vec<(String, String)>,
289 is_validation: bool,
290) -> Result<()> {
291 // Expand ~ to home directory
292 let expanded_path = if path.starts_with('~') {
293 if let Some(home_dir) = os.env.home() {
294 home_dir.join(&path[2..]).to_string_lossy().to_string()
295 } else {
296 return Err(eyre!("Could not determine home directory"));
297 }
298 } else {
299 path.to_string()
300 };
301
302 // Handle absolute, relative paths, and glob patterns
303 let full_path = if expanded_path.starts_with('/') {
304 expanded_path
305 } else {
306 os.env.current_dir()?.join(&expanded_path).to_string_lossy().to_string()
307 };
308
309 // Required in chroot testing scenarios so that we can use `Path::exists`.
310 let full_path = os.fs.chroot_path_str(full_path);
311
312 // Check if the path contains glob patterns
313 if full_path.contains('*') || full_path.contains('?') || full_path.contains('[') {
314 // Expand glob pattern
315 match glob(&full_path) {
316 Ok(entries) => {
317 let mut found_any = false;
318
319 for entry in entries {
320 match entry {
321 Ok(path) => {
322 if path.is_file() {
323 add_file_to_context(os, &path, context_files).await?;
324 found_any = true;
325 }
326 },
327 Err(e) => return Err(eyre!("Glob error: {}", e)),
328 }
329 }
330
331 if !found_any && is_validation {
332 // When validating paths (e.g., for /context add), error if no files match
333 return Err(eyre!("No files found matching glob pattern '{}'", full_path));
334 }
335 // When just showing expanded files (e.g., for /context show --expand),
336 // silently skip non-matching patterns (don't add anything to context_files)
337 },
338 Err(e) => return Err(eyre!("Invalid glob pattern '{}': {}", full_path, e)),
339 }
340 } else {
341 // Regular path
342 let path = Path::new(&full_path);

Callers 3

add_pathsMethod · 0.85
collect_context_filesMethod · 0.85

Calls 9

add_file_to_contextFunction · 0.85
to_stringMethod · 0.80
joinMethod · 0.80
current_dirMethod · 0.80
chroot_path_strMethod · 0.80
read_dirMethod · 0.80
homeMethod · 0.45
existsMethod · 0.45
pathMethod · 0.45

Tested by

no test coverage detected