1use crate::pkg::{self, transaction};
2use crate::utils;
3use anyhow::{Result, anyhow};
4
5pub fn run(
6 package_name: &str,
7 yes: bool,
8 plugin_manager: Option<&crate::pkg::plugin::PluginManager>,
9) -> Result<()> {
10 let request = pkg::resolve::parse_source_string(package_name)?;
11 let mut candidates = Vec::new();
12 for scope in [
13 pkg::types::Scope::User,
14 pkg::types::Scope::System,
15 pkg::types::Scope::Project,
16 ] {
17 candidates.extend(pkg::local::find_installed_manifests_matching(
18 &request, scope,
19 )?);
20 }
21 if candidates.is_empty() {
22 return Err(anyhow!("Package '{}' is not installed.", package_name));
23 }
24 let chosen =
25 crate::cmd::installed_select::choose_installed_manifest(package_name, &candidates, yes)?;
26
27 if let Some(pm) = plugin_manager {
28 pm.set_context(chosen.scope)?;
29 pm.trigger_hook("on_rollback", None)?;
30 }
31 pkg::rollback::run(&pkg::local::installed_manifest_source(&chosen), yes)
32}
33
34pub fn run_transaction_rollback(
35 yes: bool,
36 plugin_manager: Option<&crate::pkg::plugin::PluginManager>,
37) -> Result<()> {
38 if !utils::ask_for_confirmation(
39 "This will roll back the last recorded transaction. Are you sure?",
40 yes,
41 ) {
42 println!("Operation aborted.");
43 return Ok(());
44 }
45
46 match transaction::get_last_transaction_id()? {
47 Some(id) => {
48 println!("Rolling back transaction {}...", id);
49 if let Some(pm) = plugin_manager {
50 pm.trigger_hook("on_rollback", None)?;
51 }
52 transaction::rollback(&id)
53 }
54 None => {
55 println!("No transactions found to roll back.");
56 Ok(())
57 }
58 }
59}