use anyhow::Context;
use std::path::Path;
use clap::Parser;
#[derive(Parser, Debug)]
pub struct InitArgs {
#[arg(long)]
force: bool,
#[arg(long)]
allow_no_git: bool,
}
pub async fn handle(args: InitArgs) -> anyhow::Result<()> {
let darq_dir = Path::new(".darq");
let config_path = darq_dir.join("config.yaml");
if config_path.exists() && !args.force {
anyhow::bail!(".darq/config.yaml already exists. Use --force to overwrite.");
}
if !args.allow_no_git && !Path::new(".git").exists() {
anyhow::bail!("Not a git repository. Run `git init` first, or use --allow-no-git.");
}
let project_name = std::env::current_dir()
.context("Failed to get current directory")?
.file_name()
.context("Failed to get directory name")?
.to_string_lossy()
.into_owned();
let repo = std::process::Command::new("git")
.args(["remote", "get-url", "origin"])
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.unwrap_or_default();
let config_content = format!(
r#"project:
name: {project_name}
repo: {repo}
pipeline:
max_retries: 3
build_command: cargo build
test_command: cargo test
lint_command: cargo clippy
base_branch: main
"#
);
tokio::fs::create_dir_all(darq_dir).await?;
tokio::fs::write(&config_path, config_content).await?;
println!("Created .darq/config.yaml");
Ok(())
}