use crate::paths;
use crate::types::ProjectConfig;
use std::path::PathBuf;
use tracing::Level;
pub struct CommandContext {
pub config_path: PathBuf,
pub log_level: Level,
pub working_dir: PathBuf,
_phantom: std::marker::PhantomData<ProjectConfig>,
}
impl CommandContext {
pub fn new(config_path: Option<PathBuf>, log_level: Level) -> Self {
let working_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
let config_path = config_path.unwrap_or_else(|| working_dir.join(paths::PROJECT_CONFIG));
Self {
config_path,
log_level,
working_dir,
_phantom: std::marker::PhantomData,
}
}
pub async fn load_project_config(&self) -> anyhow::Result<ProjectConfig> {
if !self.config_path.exists() {
anyhow::bail!(
"Project configuration not found at {}. Run 'mecha10 init' first.",
self.config_path.display()
);
}
crate::types::load_project_config(&self.config_path).await
}
pub fn is_project_initialized(&self) -> bool {
self.config_path.exists()
}
pub fn project_path(&self, path: &str) -> PathBuf {
self.working_dir.join(path)
}
}
impl Default for CommandContext {
fn default() -> Self {
Self::new(None, Level::INFO)
}
}