use std::collections::BTreeMap;
use anyhow::Result;
use clap::Parser;
use harmont_cloud::builds::NewBuild;
use crate::settings;
#[derive(Debug, Clone, Parser)]
pub struct RunArgs {
pub pipeline: String,
#[arg(short, long, default_value = "main")]
pub branch: String,
#[arg(
short,
long,
default_value = "0000000000000000000000000000000000000000"
)]
pub commit: String,
#[arg(short, long)]
pub message: Option<String>,
#[arg(long)]
pub plan_file: Option<String>,
#[arg(long)]
pub no_watch: bool,
}
pub(crate) async fn run(env: &BTreeMap<String, String>, args: RunArgs) -> Result<()> {
let (client, ctx) = settings::client()?;
let org = ctx.org()?;
let plan_path = args.plan_file.as_deref().unwrap_or("plan.json");
let pipeline_ir = std::fs::read_to_string(plan_path)
.map_err(|e| anyhow::anyhow!("could not read plan file '{plan_path}': {e}"))?;
serde_json::from_str::<serde_json::Value>(&pipeline_ir)
.map_err(|e| anyhow::anyhow!("invalid JSON in plan file '{plan_path}': {e}"))?;
let build = client
.submit_build(NewBuild {
org: org.clone(),
pipeline: args.pipeline.clone(),
branch: args.branch.clone(),
commit: args.commit.clone(),
message: args.message.clone(),
pipeline_ir,
source_tgz: Vec::new(),
env: env
.iter()
.filter(|(k, _)| k.starts_with("HM_RUN_ENV_"))
.map(|(k, v)| (k.trim_start_matches("HM_RUN_ENV_").to_string(), v.clone()))
.collect(),
})
.await?;
tracing::info!("submitted build #{}", build.number);
if args.no_watch {
return Ok(());
}
crate::verbs::build::run(
env,
crate::cli::BuildCommand::Watch {
pipeline: args.pipeline.clone(),
number: build.number,
},
)
.await
}