use std::path::PathBuf;
use std::process::Command;
use crate::cache::{Cache, CacheError};
use crate::error::{RkitError, RkitResult};
use lazy_static::lazy_static;
lazy_static! {
static ref CACHE: Cache = Cache::new();
}
pub fn view_repo(project_root: &PathBuf) -> RkitResult<()> {
if let Err(e) = Cache::new().validate_and_update() {
match e {
CacheError::LockError(msg) => log::warn!("Failed to acquire cache lock: {}", msg),
CacheError::DirectoryError(e) => log::warn!("Failed to access cache directory: {}", e),
CacheError::IoError(e) => log::warn!("Failed to write cache: {}", e),
e => log::warn!("Failed to update cache: {}", e),
}
}
if !project_root.join(".git").exists() {
return Err(RkitError::InvalidPathError(format!(
"{} is not a git repository",
project_root.display()
)));
}
let output = Command::new("git")
.current_dir(project_root)
.args(["remote", "get-url", "origin"])
.output()
.map_err(RkitError::IoError)?;
if !output.status.success() {
return Err(RkitError::GitError(format!(
"Failed to get git remote URL: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
let remote_url = String::from_utf8_lossy(&output.stdout).trim().to_string();
let url = if remote_url.starts_with("git@") {
remote_url
.replace("git@", "https://")
.replace(":", "/")
.replace(".git", "")
} else {
remote_url.replace(".git", "")
};
#[cfg(target_os = "macos")]
let status = Command::new("open")
.arg(&url)
.status()
.map_err(RkitError::IoError)?;
#[cfg(target_os = "linux")]
let status = Command::new("xdg-open")
.arg(&url)
.status()
.map_err(RkitError::IoError)?;
#[cfg(target_os = "windows")]
let status = Command::new("cmd")
.args(["/C", "start", &url])
.status()
.map_err(RkitError::IoError)?;
if !status.success() {
return Err(RkitError::ShellCommandError {
command: format!("Failed to open browser for URL: {}", url),
source: std::io::Error::other("Browser command failed"),
});
}
Ok(())
}