|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use crate::Error; |
| 4 | + |
| 5 | +/// Create legacy q wrapper script for backward compatibility |
| 6 | +pub async fn create_q_wrapper(install_dir: &Path) -> Result<(), Error> { |
| 7 | + let wrapper_path = install_dir.join("q"); |
| 8 | + |
| 9 | + // Check what exists and handle appropriately |
| 10 | + if wrapper_path.exists() { |
| 11 | + let metadata = tokio::fs::symlink_metadata(&wrapper_path).await?; |
| 12 | + |
| 13 | + if metadata.is_symlink() { |
| 14 | + // It's a symlink (likely from old installation) - safe to replace |
| 15 | + tokio::fs::remove_file(&wrapper_path).await?; |
| 16 | + } else if is_our_wrapper(&wrapper_path).await? { |
| 17 | + // It's our wrapper from previous install - safe to replace |
| 18 | + tokio::fs::remove_file(&wrapper_path).await?; |
| 19 | + } else { |
| 20 | + // It's something else (assume old Q CLI) - safe to replace per our assumption |
| 21 | + tokio::fs::remove_file(&wrapper_path).await?; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Create wrapper script content |
| 26 | + let wrapper_content = format!( |
| 27 | + "#!/bin/sh\n\"{}/kiro-cli\" --show-legacy-warning \"$@\"\n", |
| 28 | + install_dir.display() |
| 29 | + ); |
| 30 | + |
| 31 | + // Write wrapper script |
| 32 | + tokio::fs::write(&wrapper_path, wrapper_content).await?; |
| 33 | + |
| 34 | + // Make executable |
| 35 | + #[cfg(unix)] |
| 36 | + { |
| 37 | + use std::os::unix::fs::PermissionsExt; |
| 38 | + let mut perms = tokio::fs::metadata(&wrapper_path).await?.permissions(); |
| 39 | + perms.set_mode(0o755); |
| 40 | + tokio::fs::set_permissions(&wrapper_path, perms).await?; |
| 41 | + } |
| 42 | + |
| 43 | + Ok(()) |
| 44 | +} |
| 45 | + |
| 46 | +/// Check if the existing q command is our wrapper script |
| 47 | +async fn is_our_wrapper(path: &Path) -> Result<bool, Error> { |
| 48 | + if let Ok(content) = tokio::fs::read_to_string(path).await { |
| 49 | + // Check if it contains our signature |
| 50 | + Ok(content.contains("--show-legacy-warning") && content.contains("kiro-cli")) |
| 51 | + } else { |
| 52 | + Ok(false) |
| 53 | + } |
| 54 | +} |
0 commit comments