auths_cli/commands/
cache.rs1use anyhow::{Context, Result};
6use clap::{Parser, Subcommand};
7
8use auths_sdk::core_config::EnvironmentConfig;
9use auths_sdk::keri::cache;
10
11#[derive(Parser, Debug, Clone)]
12#[command(about = "Manage cached identity snapshots")]
13pub struct CacheCommand {
14 #[command(subcommand)]
15 command: CacheSubcommand,
16}
17
18#[derive(Subcommand, Debug, Clone)]
19enum CacheSubcommand {
20 List,
22
23 Inspect {
25 did: String,
27 },
28
29 Clear {
31 did: Option<String>,
33 },
34}
35
36pub fn handle_cache(cmd: CacheCommand, env_config: &EnvironmentConfig) -> Result<()> {
37 let auths_home = auths_sdk::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 snapshots found.");
51 return Ok(());
52 }
53
54 println!("Cached identity snapshots:\n");
55 for entry in entries {
56 println!(" Identity ID: {}", entry.did);
57 println!(" Sequence: {}", entry.sequence);
58 println!(" Verified 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 "Verified against log entry: {}",
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!(" Current keys: {:?}", cached.state.current_keys);
83 println!(
84 " Pre-committed rotation key: {:?}",
85 cached.state.next_commitment
86 );
87 println!(" Is abandoned: {}", cached.state.is_abandoned);
88 println!(
89 "\nCache file: {}",
90 cache::cache_path_for_did(auths_home, did).display()
91 );
92 }
93 None => {
94 println!("No cache entry found for: {}", did);
95 println!(
96 "Expected path: {}",
97 cache::cache_path_for_did(auths_home, did).display()
98 );
99 }
100 }
101
102 Ok(())
103}
104
105fn handle_clear(auths_home: &std::path::Path, did: Option<&str>) -> Result<()> {
106 match did {
107 Some(did) => {
108 cache::invalidate_cache(auths_home, did)?;
109 println!("Cleared cache for: {}", did);
110 }
111 None => {
112 let count = cache::clear_all_caches(auths_home)?;
113 if count == 0 {
114 println!("No cache entries to clear.");
115 } else {
116 println!("Cleared {} cache entries.", count);
117 }
118 }
119 }
120
121 Ok(())
122}
123
124impl crate::commands::executable::ExecutableCommand for CacheCommand {
125 fn execute(&self, ctx: &crate::config::CliConfig) -> anyhow::Result<()> {
126 handle_cache(self.clone(), &ctx.env_config)
127 }
128}