use std::env;
use std::path::Path;
use crate::config::Config;
pub fn resolve_workspace(cli_workspace: Option<&str>) -> String {
if let Some(ws) = cli_workspace {
return ws.to_string();
}
if let Some(ws) = find_workspace_file() {
return ws;
}
if let Ok(config) = Config::load() {
if let Ok(cwd) = env::current_dir() {
let cwd_str = cwd.to_string_lossy().to_string();
if let Some(ws) = config.directory_workspaces.get(&cwd_str) {
return ws.clone();
}
}
if let Some(ws) = config.default_workspace {
return ws;
}
}
"default".to_string()
}
fn find_workspace_file() -> Option<String> {
let mut dir = env::current_dir().ok()?;
loop {
let file = dir.join(".linear-workspace");
if file.exists() {
let contents = std::fs::read_to_string(&file).ok()?;
let trimmed = contents.trim().to_string();
if !trimmed.is_empty() {
return Some(trimmed);
}
}
if !dir.pop() {
break;
}
}
None
}
pub fn write_workspace_file(name: &str) -> Result<(), std::io::Error> {
let path = Path::new(".linear-workspace");
std::fs::write(path, name)
}