use blueprint_remote_providers::DeploymentTracker;
use clap::Subcommand;
use color_eyre::{
Result,
eyre::{WrapErr, eyre},
};
use std::path::PathBuf;
mod config;
mod estimate;
mod policy;
mod preflight;
mod status;
pub use config::CloudProvider;
pub use policy::{CostOptimization, RemoteDeploymentPolicy};
#[derive(Subcommand, Debug)]
pub enum CloudCommands {
#[command(visible_alias = "cfg")]
Configure {
provider: CloudProvider,
#[arg(short, long)]
region: Option<String>,
#[arg(short = 'd', long)]
set_default: bool,
},
#[command(visible_alias = "policy")]
ConfigurePolicy {
#[command(flatten)]
args: policy::PolicyConfigureArgs,
},
#[command(visible_alias = "show")]
ShowPolicy,
#[command(visible_alias = "cost")]
Estimate(estimate::EstimateOptions),
#[command(visible_alias = "s")]
Status {
service_id: Option<String>,
#[arg(short, long)]
watch: bool,
},
#[command(visible_alias = "term")]
Terminate {
#[arg(required_unless_present = "all")]
service_id: Option<String>,
#[arg(long, conflicts_with = "service_id")]
all: bool,
#[arg(short, long)]
yes: bool,
},
#[command(visible_alias = "ls")]
List,
#[command(visible_alias = "check")]
Preflight {
#[arg(short, long)]
provider: Option<CloudProvider>,
#[arg(long)]
tee_required: bool,
#[arg(long)]
bootstrap_env: bool,
#[arg(long)]
write_env_file: Option<PathBuf>,
},
}
async fn open_tracker() -> Result<DeploymentTracker> {
let path = std::env::var_os("TANGLE_DEPLOYMENT_TRACKER_PATH")
.map(PathBuf::from)
.or_else(|| dirs::home_dir().map(|home| home.join(".tangle/remote_deployments")))
.ok_or_else(|| eyre!("Could not find the home directory"))?;
DeploymentTracker::new(&path)
.await
.wrap_err("Failed to read the deployment tracker")
}
pub async fn execute(command: CloudCommands) -> Result<()> {
match command {
CloudCommands::Configure {
provider,
region,
set_default,
} => config::configure(provider, region, set_default).await,
CloudCommands::ConfigurePolicy { args } => policy::configure_policy(args).await,
CloudCommands::ShowPolicy => policy::show_policy().await,
CloudCommands::Estimate(options) => estimate::estimate(options).await,
CloudCommands::Status { service_id, watch } => status::show_status(service_id, watch).await,
CloudCommands::Terminate {
service_id,
all: _,
yes,
} => status::terminate(service_id, yes).await,
CloudCommands::List => config::list_providers().await,
CloudCommands::Preflight {
provider,
tee_required,
bootstrap_env,
write_env_file,
} => preflight::run(provider, tee_required, bootstrap_env, write_env_file).await,
}
}