use crate::config::deploy::DeployConfig;
use crate::config::turnkey;
use anyhow::{Context, Result};
use chrono::Local;
use clap::Args as ClapArgs;
use std::path::PathBuf;
#[derive(Debug, ClapArgs)]
#[command(about, long_about = None)]
pub struct Args {
#[arg(short, long)]
pub output: Option<PathBuf>,
}
pub async fn run(args: Args) -> Result<()> {
let output = args.output.unwrap_or_else(|| {
let timestamp = Local::now().format("%Y-%m-%d-%H%M%S");
PathBuf::from(format!("deploy-{timestamp}.json"))
});
if output.exists() {
anyhow::bail!("File already exists: {}", output.display());
}
let config = turnkey::Config::load().await?;
let last_app_id = config.get_last_app_id();
let config = DeployConfig::template(last_app_id.as_deref());
let json = serde_json::to_string_pretty(&config).context("failed to serialize config")?;
std::fs::write(&output, json)
.with_context(|| format!("failed to write file: {}", output.display()))?;
println!("Created deployment config template: {}", output.display());
println!();
println!("Edit the file to fill in your values, then run:");
println!(" tvc deploy create {}", output.display());
Ok(())
}