use std::{collections::HashMap, fs, path::PathBuf};
use dialoguer::Confirm;
use serde_json::{Map, Value};
use crate::{
action::{
SERVICE_CONFIGURATION_PATH, ServiceConfiguration, carry_forward_actions,
resolve_action_dev_tag, resolve_dev_tag,
},
bundle::{BundleSource, SetupBundle},
env_file,
runner::{Runner, default_runner},
template::render_setup_templates,
ui, update_check,
};
const ENV_PATH: &str = ".codezero/.env";
pub fn upgrade(
tag: Option<String>,
registry: Option<String>,
edition: Option<String>,
bundle: Option<PathBuf>,
dev: bool,
) -> anyhow::Result<()> {
if !fs::exists(ENV_PATH)? {
anyhow::bail!("No CodeZero setup found. Run `codezero setup` first.");
}
let (core_target, action_target) = match tag {
Some(tag) => (tag.clone(), tag),
None if dev => {
ui::muted_line("Resolving the latest CodeZero build...");
(resolve_dev_tag()?, resolve_action_dev_tag()?)
}
None => anyhow::bail!(
"Pick a build to upgrade to: pass `--tag <version>`, or `--dev` for the latest in-progress build."
),
};
if !confirm_upgrade(
&core_target,
&action_target,
registry.as_deref(),
edition.as_deref(),
)? {
ui::warn_line("Upgrade cancelled.");
return Ok(());
}
let source = BundleSource::from_arg(bundle);
let bundle = SetupBundle::load(&source)?;
let seed = env_file::read_all(ENV_PATH)?;
let context = build_upgrade_context(
&bundle.static_values,
&seed,
&core_target,
&action_target,
registry.as_deref(),
edition.as_deref(),
);
let installed_actions =
ServiceConfiguration::load_or_default(SERVICE_CONFIGURATION_PATH)?.actions;
render_setup_templates(&source, &bundle.templates, &context, ".codezero")?;
carry_forward_actions(installed_actions)?;
println!();
ui::muted_line("Pulling new images and restarting whatever changed...");
default_runner().start()?;
update_check::record_current_tag(&core_target, &action_target);
ui::success_line(&format!(
"CodeZero upgraded to {core_target} (actions {action_target})."
));
Ok(())
}
fn confirm_upgrade(
core_target: &str,
action_target: &str,
registry: Option<&str>,
edition: Option<&str>,
) -> anyhow::Result<bool> {
let current_core = env_file::read_value(ENV_PATH, "IMAGE_TAG")?.unwrap_or_default();
let current_actions = env_file::read_value(ENV_PATH, "ACTION_IMAGE_TAG")?.unwrap_or_default();
ui::warn_line("This refetches the latest bundle and may include breaking changes.");
ui::muted_line(&format!(" core platform: {current_core} -> {core_target}"));
ui::muted_line(&format!(" actions: {current_actions} -> {action_target}"));
if let Some(registry) = registry {
ui::muted_line(&format!(" image registry: {registry}"));
}
if let Some(edition) = edition {
ui::muted_line(&format!(" image edition: {edition}"));
}
ui::muted_line("Your secrets, admin account, and installed actions are kept.");
Ok(Confirm::with_theme(&ui::wizard_theme())
.with_prompt("Continue with the upgrade?")
.default(false)
.interact()?)
}
fn build_upgrade_context(
static_values: &Map<String, Value>,
seed: &HashMap<String, String>,
core_target: &str,
action_target: &str,
registry: Option<&str>,
edition: Option<&str>,
) -> Map<String, Value> {
let mut context = static_values.clone();
for (key, value) in seed {
context.insert(key.to_lowercase(), Value::String(value.clone()));
}
context.insert("image_tag".into(), Value::String(core_target.into()));
context.insert("action_image_tag".into(), Value::String(action_target.into()));
if let Some(registry) = registry {
context.insert("image_registry".into(), Value::String(registry.into()));
}
if let Some(edition) = edition {
context.insert("image_edition".into(), Value::String(edition.into()));
}
context
}
#[cfg(test)]
mod tests {
use super::*;
fn seed() -> HashMap<String, String> {
[
("IMAGE_TAG", "0.0.0-experimental-100-aaa"),
("ACTION_IMAGE_TAG", "0.0.0-experimental-100-aaa"),
("INITIAL_ROOT_PASSWORD", "hunter2"),
("AQUILA_ACTION_GLS_IDENTIFIER", "gls-action"),
("AQUILA_ACTION_GLS_TOKEN", "gls-secret"),
]
.into_iter()
.map(|(key, value)| (key.to_string(), value.to_string()))
.collect()
}
#[test]
fn carries_every_existing_env_value_forward_lowercased() {
let context = build_upgrade_context(
&Map::new(),
&seed(),
"0.0.0-experimental-200-bbb",
"0.0.0-experimental-200-bbb",
None,
None,
);
assert_eq!(
context.get("aquila_action_gls_token"),
Some(&Value::String("gls-secret".into()))
);
assert_eq!(
context.get("initial_root_password"),
Some(&Value::String("hunter2".into()))
);
}
#[test]
fn sets_each_image_tag_from_its_own_target() {
let core = "0.0.0-experimental-200-bbb";
let action = "0.0.0-experimental-777-ccc";
let context = build_upgrade_context(&Map::new(), &seed(), core, action, None, None);
assert_eq!(context.get("image_tag"), Some(&Value::String(core.into())));
assert_eq!(
context.get("action_image_tag"),
Some(&Value::String(action.into()))
);
}
#[test]
fn fills_new_bundle_defaults_the_seed_does_not_have() {
let mut static_values = Map::new();
static_values.insert("brand_new_key".into(), Value::String("default".into()));
let context = build_upgrade_context(&static_values, &seed(), "t", "t", None, None);
assert_eq!(
context.get("brand_new_key"),
Some(&Value::String("default".into()))
);
}
#[test]
fn seed_wins_over_static_defaults_for_overlapping_keys() {
let mut static_values = Map::new();
static_values.insert("initial_root_password".into(), Value::String("default".into()));
let context = build_upgrade_context(&static_values, &seed(), "t", "t", None, None);
assert_eq!(
context.get("initial_root_password"),
Some(&Value::String("hunter2".into()))
);
}
#[test]
fn applies_registry_and_edition_only_when_provided() {
let without = build_upgrade_context(&Map::new(), &seed(), "t", "t", None, None);
assert!(!without.contains_key("image_registry"));
assert!(!without.contains_key("image_edition"));
let with =
build_upgrade_context(&Map::new(), &seed(), "t", "t", Some("ghcr.io/x"), Some("cc"));
assert_eq!(
with.get("image_registry"),
Some(&Value::String("ghcr.io/x".into()))
);
assert_eq!(with.get("image_edition"), Some(&Value::String("cc".into())));
}
}