tvc 0.13.1

CLI for Turnkey Verifiable Cloud
Documentation
//! Deploy restore command.

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 uuid::Uuid;

use crate::outcome::Outcome;
use crate::output::StdCtx;

/// Restore a deleted deployment.
#[derive(Debug, ClapArgs)]
#[command(about, long_about = None)]
pub struct Args {
    /// ID of the deployment.
    #[arg(short, long, env = "TVC_DEPLOY_ID")]
    pub deploy_id: Uuid,
}

/// Run the deploy restore command.
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.to_string(),
    };

    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,
    /// Stable proto status name, e.g. `ACTIVITY_STATUS_COMPLETED`.
    activity_status: ActivityStatus,
}

/// Manual because the generated `ActivityStatus` does not implement
/// `Default`; the zero value is the enum's `Unspecified` variant.
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()
        )
    }
}