Skip to main content

auths_cli/commands/
cache.rs

1//! Cache management commands.
2//!
3//! Provides commands to list, inspect, and clear the local identity history cache.
4
5use anyhow::{Context, Result};
6use clap::{Parser, Subcommand};
7
8use auths_core::config::EnvironmentConfig;
9use auths_id::keri::cache;
10
11#[derive(Parser, Debug, Clone)]
12#[command(about = "Manage local identity history cache")]
13pub struct CacheCommand {
14    #[command(subcommand)]
15    command: CacheSubcommand,
16}
17
18#[derive(Subcommand, Debug, Clone)]
19enum CacheSubcommand {
20    /// List all cached identity states
21    List,
22
23    /// Inspect a cached identity state for a specific identity ID
24    Inspect {
25        /// The identity ID to inspect
26        did: String,
27    },
28
29    /// Clear cached identity states
30    Clear {
31        /// Optional: only clear cache for this specific identity ID
32        did: Option<String>,
33    },
34}
35
36pub fn handle_cache(cmd: CacheCommand, env_config: &EnvironmentConfig) -> Result<()> {
37    let auths_home = auths_core::paths::auths_home_with_config(env_config)
38        .context("Failed to resolve auths home directory")?;
39    match cmd.command {
40        CacheSubcommand::List => handle_list(&auths_home),
41        CacheSubcommand::Inspect { did } => handle_inspect(&auths_home, &did),
42        CacheSubcommand::Clear { did } => handle_clear(&auths_home, did.as_deref()),
43    }
44}
45
46fn handle_list(auths_home: &std::path::Path) -> Result<()> {
47    let entries = cache::list_cached_entries(auths_home)?;
48
49    if entries.is_empty() {
50        println!("No cached identity states found.");
51        return Ok(());
52    }
53
54    println!("Cached identity states:\n");
55    for entry in entries {
56        println!("  Identity ID: {}", entry.did);
57        println!("  Sequence: {}", entry.sequence);
58        println!("  Validated against: {}", entry.validated_against_tip_said);
59        println!("  Commit OID: {}", entry.last_commit_oid);
60        println!("  Cached at: {}", entry.cached_at);
61        println!("  File: {}", entry.path.display());
62        println!();
63    }
64
65    Ok(())
66}
67
68fn handle_inspect(auths_home: &std::path::Path, did: &str) -> Result<()> {
69    match cache::inspect_cache(auths_home, did)? {
70        Some(cached) => {
71            println!("Cache entry for: {}\n", did);
72            println!("Version: {}", cached.version);
73            println!("Identity ID: {}", cached.did);
74            println!("Sequence: {}", cached.sequence);
75            println!(
76                "Validated against tip: {}",
77                cached.validated_against_tip_said
78            );
79            println!("Last commit OID: {}", cached.last_commit_oid);
80            println!("Cached at: {}", cached.cached_at);
81            println!("\nKey State:");
82            println!("  Prefix: {}", cached.state.prefix);
83            println!("  Current keys: {:?}", cached.state.current_keys);
84            println!("  Next commitment: {:?}", cached.state.next_commitment);
85            println!("  Is abandoned: {}", cached.state.is_abandoned);
86            println!(
87                "\nCache file: {}",
88                cache::cache_path_for_did(auths_home, did).display()
89            );
90        }
91        None => {
92            println!("No cache entry found for: {}", did);
93            println!(
94                "Expected path: {}",
95                cache::cache_path_for_did(auths_home, did).display()
96            );
97        }
98    }
99
100    Ok(())
101}
102
103fn handle_clear(auths_home: &std::path::Path, did: Option<&str>) -> Result<()> {
104    match did {
105        Some(did) => {
106            cache::invalidate_cache(auths_home, did)?;
107            println!("Cleared cache for: {}", did);
108        }
109        None => {
110            let count = cache::clear_all_caches(auths_home)?;
111            if count == 0 {
112                println!("No cache entries to clear.");
113            } else {
114                println!("Cleared {} cache entries.", count);
115            }
116        }
117    }
118
119    Ok(())
120}
121
122impl crate::commands::executable::ExecutableCommand for CacheCommand {
123    fn execute(&self, ctx: &crate::config::CliConfig) -> anyhow::Result<()> {
124        handle_cache(self.clone(), &ctx.env_config)
125    }
126}