use anyhow::Context;
use clap::Args as ClapArgs;
use serde::Serialize;
use std::fmt::{self, Display, Formatter};
use std::time::{SystemTime, UNIX_EPOCH};
use turnkey_client::generated::{ActivityStatus, RestoreTvcDeploymentIntent};
use crate::outcome::Outcome;
use crate::output::StdCtx;
#[derive(Debug, ClapArgs)]
#[command(about, long_about = None)]
pub struct Args {
#[arg(short, long, env = "TVC_DEPLOY_ID")]
pub deploy_id: String,
}
pub async fn run(_ctx: &mut StdCtx, args: Args) -> anyhow::Result<Outcome> {
let auth = crate::client::build_client().await?;
let intent = RestoreTvcDeploymentIntent {
deployment_id: args.deploy_id,
};
let timestamp_ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.context("system time before unix epoch")?
.as_millis();
let result = auth
.client
.restore_tvc_deployment(auth.org_id, timestamp_ms, intent)
.await
.context("failed to restore TVC deployment")?;
Ok(Outcome::DeployRestore(DeploymentRestored {
deployment_id: result.result.deployment_id,
activity_id: result.activity_id,
activity_status: result.status,
}))
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DeploymentRestored {
deployment_id: String,
activity_id: String,
activity_status: ActivityStatus,
}
impl Default for DeploymentRestored {
fn default() -> Self {
Self {
deployment_id: String::default(),
activity_id: String::default(),
activity_status: ActivityStatus::Unspecified,
}
}
}
impl Display for DeploymentRestored {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
r#"
Deployment restore accepted; deployment is no longer marked for deletion.
Deployment ID: {}
Activity ID: {}
Activity Status: {}"#,
self.deployment_id,
self.activity_id,
self.activity_status.as_str_name()
)
}
}