use std::{
fmt::{self, Display, Formatter},
time::Duration,
};
use dialoguer::console::{Style, style};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use crate::error::Error;
const SPINNER_INTERVAL: Duration = Duration::from_millis(100);
const HOST_TEMPLATE: &str = "{spinner} {prefix} {wide_msg}";
const SPINNER_TICKS: &str =
"\u{280b}\u{2819}\u{2839}\u{2838}\u{283c}\u{2834}\u{2826}\u{2827}\u{2807}\u{280f} ";
#[derive(Clone, Copy)]
pub(crate) enum DeploymentPhase {
UpdatingInputs,
CheckingStatus,
Locking,
Preparing,
ArmingRollback,
DisarmingRollback,
Testing,
BuildingVerifier,
RunningVerifier,
Switching,
RollingBack,
CollectingGarbage,
}
impl DeploymentPhase {
fn style(self) -> Style {
let style = Style::new().for_stderr();
match self {
Self::UpdatingInputs
| Self::CheckingStatus
| Self::Locking
| Self::Preparing
| Self::Testing
| Self::BuildingVerifier => style.blue(),
Self::ArmingRollback | Self::DisarmingRollback => style.magenta(),
Self::RunningVerifier | Self::CollectingGarbage => style.yellow(),
Self::Switching => style.magenta(),
Self::RollingBack => style.red(),
}
}
}
impl Display for DeploymentPhase {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::UpdatingInputs => "updating inputs",
Self::CheckingStatus => "checking status",
Self::Locking => "locking",
Self::Preparing => "preparing",
Self::ArmingRollback => "arming rollback",
Self::DisarmingRollback => "disarming rollback",
Self::Testing => "testing",
Self::BuildingVerifier => "building verifier",
Self::RunningVerifier => "running verifier",
Self::Switching => "switching",
Self::RollingBack => "rolling back",
Self::CollectingGarbage => "collecting garbage",
})
}
}
fn spinner_style(template: &str) -> Result<ProgressStyle, Error> {
Ok(ProgressStyle::with_template(template)
.map_err(|source| Error::ProgressStyle { source })?
.tick_chars(SPINNER_TICKS))
}
fn set_status(progress: &ProgressBar, host: &str, status: impl Display, status_style: &Style) {
let host = style(format!("{host:<12}")).for_stderr().bold();
let status = status_style.apply_to(format!("{status:<16}"));
progress.set_prefix(format!("{host} {status}"));
progress.set_message("");
}
pub(crate) fn set_phase(progress: &ProgressBar, host: &str, phase: DeploymentPhase) {
set_status(progress, host, phase, &phase.style());
}
fn finish_with_status(progress: &ProgressBar, host: &str, status: &str, status_style: &Style) {
let message = progress.message();
set_status(progress, host, status, status_style);
progress.finish_with_message(message);
}
pub(crate) fn finish(progress: &ProgressBar, host: &str) {
finish_with_status(
progress,
host,
"activated",
&Style::new().for_stderr().green(),
);
}
pub(crate) fn rolled_back(progress: &ProgressBar, host: &str) {
finish_with_status(
progress,
host,
"rolled back",
&Style::new().for_stderr().red(),
);
}
pub(crate) fn deployment_failed(progress: &ProgressBar, host: &str) {
finish_with_status(progress, host, "failed", &Style::new().for_stderr().red());
}
pub(crate) fn cleanup_failed(progress: &ProgressBar, host: &str) {
finish_with_status(
progress,
host,
"cleanup failed",
&Style::new().for_stderr().red(),
);
}
pub(crate) fn verified(progress: &ProgressBar, host: &str) {
finish_with_status(
progress,
host,
"verified",
&Style::new().for_stderr().green(),
);
}
pub(crate) fn verifier_missing(progress: &ProgressBar, host: &str) {
set_status(
progress,
host,
"skipped",
&Style::new().for_stderr().yellow(),
);
progress.finish_with_message("no verifier configured");
}
pub(crate) fn fail(progress: &ProgressBar) {
let failed = style("failed").for_stderr().red();
progress.finish_with_message(format!("{failed}"));
}
pub(crate) fn recovery_required(progress: &ProgressBar, host: &str) {
set_status(
progress,
host,
"recovery required",
&Style::new().for_stderr().red(),
);
let message = style("rollback unconfirmed; target watchdog may still be armed")
.for_stderr()
.red();
progress.finish_with_message(format!("{message}"));
}
pub(crate) struct ProgressDisplay {
progress: MultiProgress,
style: ProgressStyle,
}
impl ProgressDisplay {
pub(crate) fn new() -> Result<Self, Error> {
let style = spinner_style(HOST_TEMPLATE)?;
Ok(Self {
progress: MultiProgress::new(),
style,
})
}
pub(crate) fn hosts<'a>(&self, names: impl IntoIterator<Item = &'a str>) -> Vec<ProgressBar> {
names
.into_iter()
.map(|name| {
let progress = self.progress.add(ProgressBar::new_spinner());
progress.set_style(self.style.clone());
set_status(&progress, name, "", &Style::new());
progress.enable_steady_tick(SPINNER_INTERVAL);
progress
})
.collect()
}
}