use std::fs;
use std::path::Path;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GoModule {
pub module_path: String,
pub root_dir: String,
}
pub fn load_go_module(project_root: impl AsRef<Path>) -> Option<GoModule> {
let project_root = project_root.as_ref();
let content = fs::read_to_string(project_root.join("go.mod")).ok()?;
let stripped = content
.lines()
.map(|line| line.split_once("//").map(|(head, _)| head).unwrap_or(line))
.collect::<Vec<_>>()
.join("\n");
for line in stripped.lines() {
let trimmed = line.trim();
let Some(rest) = trimmed.strip_prefix("module") else {
continue;
};
let mut module_path = rest.trim().to_string();
if module_path.starts_with('"') || module_path.starts_with('\'') {
module_path.remove(0);
}
if module_path.ends_with('"') || module_path.ends_with('\'') {
module_path.pop();
}
if module_path.is_empty() {
return None;
}
return Some(GoModule {
module_path,
root_dir: project_root.to_string_lossy().into_owned(),
});
}
None
}