Skip to main content

zoi_cli/cmd/
transaction.rs

1use crate::pkg::{local, transaction, types};
2use anyhow::Result;
3use colored::*;
4use comfy_table::{ContentArrangement, Table, presets::UTF8_FULL};
5
6fn manifest_source(manifest: &types::InstallManifest) -> String {
7    local::installed_manifest_source(manifest)
8}
9
10pub fn list() -> Result<()> {
11    let transactions = transaction::list_transactions()?;
12    if transactions.is_empty() {
13        println!("No transaction logs found.");
14        return Ok(());
15    }
16
17    let mut table = Table::new();
18    table
19        .load_preset(UTF8_FULL)
20        .set_content_arrangement(ContentArrangement::Dynamic)
21        .set_header(vec!["ID", "Started", "Operations"]);
22
23    for entry in transactions {
24        table.add_row(vec![
25            entry.id,
26            entry.start_time,
27            entry.operation_count.to_string(),
28        ]);
29    }
30
31    println!("{table}");
32    Ok(())
33}
34
35pub fn files(transaction_id: &str) -> Result<()> {
36    let mut modified_files = transaction::get_modified_files(transaction_id)?;
37    modified_files.sort();
38
39    if modified_files.is_empty() {
40        println!(
41            "No modified files recorded for transaction '{}'.",
42            transaction_id
43        );
44        return Ok(());
45    }
46
47    println!(
48        "{} Files modified by transaction '{}':",
49        "::".bold().blue(),
50        transaction_id.cyan()
51    );
52    for path in modified_files {
53        println!("  - {}", path);
54    }
55    Ok(())
56}
57
58pub fn show(transaction_id: &str) -> Result<()> {
59    let transaction = transaction::read_transaction(transaction_id)?;
60
61    println!(
62        "{} Transaction {}",
63        "::".bold().blue(),
64        transaction.id.cyan()
65    );
66    println!("Started: {}", transaction.start_time);
67    println!("Operations: {}", transaction.operations.len());
68
69    for (index, operation) in transaction.operations.iter().enumerate() {
70        match operation {
71            types::TransactionOperation::Install { manifest } => {
72                println!(
73                    "{}. install {}",
74                    index + 1,
75                    manifest_source(manifest).green()
76                );
77            }
78            types::TransactionOperation::Uninstall { manifest } => {
79                println!(
80                    "{}. uninstall {}",
81                    index + 1,
82                    manifest_source(manifest).red()
83                );
84            }
85            types::TransactionOperation::Upgrade {
86                old_manifest,
87                new_manifest,
88            } => {
89                println!(
90                    "{}. upgrade {} -> {}",
91                    index + 1,
92                    manifest_source(old_manifest).yellow(),
93                    manifest_source(new_manifest).green()
94                );
95            }
96        }
97    }
98
99    Ok(())
100}