use clap::Args;
use serde::Serialize;
use homeboy::deploy::{self, ComponentDeployResult, DeployConfig, DeploySummary};
use homeboy::resolve::{infer_project_for_components, resolve_project_components};
use super::CmdResult;
#[derive(Args)]
pub struct DeployArgs {
pub project_id: String,
pub component_ids: Vec<String>,
#[arg(long, short = 'p')]
pub project: Option<String>,
#[arg(long, short = 'c')]
pub component: Option<Vec<String>>,
#[arg(long)]
pub json: Option<String>,
#[arg(long)]
pub all: bool,
#[arg(long)]
pub outdated: bool,
#[arg(long)]
pub dry_run: bool,
#[arg(long, visible_alias = "status")]
pub check: bool,
#[arg(long)]
pub force: bool,
}
#[derive(Serialize)]
pub struct DeployOutput {
pub command: String,
pub project_id: String,
pub all: bool,
pub outdated: bool,
pub dry_run: bool,
pub check: bool,
pub force: bool,
pub results: Vec<ComponentDeployResult>,
pub summary: DeploySummary,
}
pub fn run(mut args: DeployArgs, _global: &crate::commands::GlobalArgs) -> CmdResult<DeployOutput> {
let (project_id, component_ids) = match (&args.project, &args.component) {
(Some(ref proj), Some(ref comps)) => (proj.clone(), comps.clone()),
(Some(ref proj), None) => {
let mut comps = vec![args.project_id.clone()];
comps.extend(args.component_ids.clone());
(proj.clone(), comps)
}
(None, Some(ref comps)) => {
let projects = homeboy::project::list_ids().unwrap_or_default();
if projects.contains(&args.project_id) {
(args.project_id.clone(), comps.clone())
} else {
match infer_project_for_components(comps) {
Some(proj) => (proj, comps.clone()),
None => {
return Err(homeboy::Error::validation_invalid_argument(
"project_id",
"Could not infer project. Use --project flag or provide project as first argument.",
None,
None,
)
.into())
}
}
}
}
(None, None) => resolve_project_components(&args.project_id, &args.component_ids)?,
};
args.project_id = project_id.clone();
args.component_ids = component_ids;
if let Some(ref spec) = args.json {
args.component_ids = deploy::parse_bulk_component_ids(spec)?;
}
let config = DeployConfig {
component_ids: args.component_ids.clone(),
all: args.all,
outdated: args.outdated,
dry_run: args.dry_run,
check: args.check,
force: args.force,
skip_build: false,
};
let result = deploy::run(&project_id, &config).map_err(|e| {
if e.message.contains("No components configured for project") {
e.with_hint(format!(
"Run 'homeboy project components add {} <component-id>' to add components",
project_id
))
.with_hint("Run 'homeboy init' to see project context and available components")
} else {
e
}
})?;
let exit_code = if result.summary.failed > 0 { 1 } else { 0 };
Ok((
DeployOutput {
command: "deploy.run".to_string(),
project_id: project_id.clone(),
all: args.all,
outdated: args.outdated,
dry_run: args.dry_run,
check: args.check,
force: args.force,
results: result.results,
summary: result.summary,
},
exit_code,
))
}