use crate::config::app::AppConfig;
use crate::config::turnkey::{Config, StoredQosOperatorKey};
use crate::prompts::{bail_interactive_conflicts_with_non_interactive, ensure_stdin_is_tty};
use anyhow::{Context, Result, bail};
use clap::Args as ClapArgs;
use std::path::PathBuf;
#[derive(Debug, ClapArgs)]
#[command(about, long_about = None)]
pub struct Args {
#[arg(
short,
long,
value_name = "PATH",
default_value = "app.json",
env = "TVC_APP_CONFIG_OUT"
)]
pub output: PathBuf,
#[arg(long)]
pub interactive: bool,
}
pub async fn run(args: Args, is_non_interactive: bool) -> Result<()> {
if args.interactive {
if is_non_interactive {
bail_interactive_conflicts_with_non_interactive()?;
} else {
ensure_stdin_is_tty()?;
}
}
execute(args).await
}
async fn execute(args: Args) -> Result<()> {
if args.output.exists() {
bail!("File already exists: {}", args.output.display());
}
let operator_public_key = load_operator_public_key().await;
let mut config = AppConfig::template(operator_public_key.as_deref());
if args.interactive {
config.fill_interactively(operator_public_key.as_deref())?;
}
let json = serde_json::to_string_pretty(&config).context("failed to serialize config")?;
std::fs::write(&args.output, json)
.with_context(|| format!("failed to write file: {}", args.output.display()))?;
if args.interactive {
println!("Created app config: {}", args.output.display());
println!();
println!(
"Run: tvc app create --config-file {}",
args.output.display()
);
} else {
println!("Created app config template: {}", args.output.display());
println!();
println!("Edit the file to fill in your values, then run:");
println!(" tvc app create --config-file {}", args.output.display());
}
Ok(())
}
async fn load_operator_public_key() -> Option<String> {
let config = Config::load().await.ok()?;
let (_, org_config) = config.active_org_config()?;
let operator_key = StoredQosOperatorKey::load(org_config).await.ok()??;
Some(operator_key.public_key)
}