Skip to main content

zoi_cli/cmd/
rollback.rs

1//! Rolling back packages and transactions.
2
3use anyhow::{Result, anyhow};
4
5use crate::pkg::{self};
6
7/// Rolls back a specific package to its previous state.
8///
9/// This looks for an installed package matching the name and triggers a
10/// rollback operation.
11///
12/// # Errors
13///
14/// Returns an error if the package is not found or the rollback operation
15/// fails.
16pub fn run(
17    package_name: &str,
18    yes: bool,
19    plugin_manager: Option<&crate::pkg::plugin::PluginManager>
20) -> Result<()> {
21    let request = pkg::resolve::parse_source_string(package_name)?;
22    let mut candidates = Vec::new();
23    for scope in [
24        pkg::types::Scope::User,
25        pkg::types::Scope::System,
26        pkg::types::Scope::Project
27    ] {
28        candidates.extend(pkg::local::find_installed_manifests_matching(
29            &request, scope
30        )?);
31    }
32    if candidates.is_empty() {
33        return Err(anyhow!("Package '{package_name}' is not installed."));
34    }
35    let chosen = crate::cmd::installed_select::choose_installed_manifest(
36        package_name,
37        &candidates,
38        yes
39    )?;
40
41    if let Some(pm) = plugin_manager {
42        pm.set_context(chosen.scope)?;
43        pm.trigger_hook("on_rollback", None)?;
44    }
45    pkg::rollback::run(&pkg::local::installed_manifest_source(&chosen), yes)
46}