use crate::{
cli::common::{
COLLECTION_MODE_CACHE_ONLY, CurrentUnixSecsError, collection_help, current_unix_secs,
json_arg, output_format, write_text_or_json,
},
storage::{CacheRootError, cache_root},
};
use clap::{ArgMatches, Command as ClapCommand};
use ic_query::cache::CacheStatusError;
use ic_query::cache::CacheStatusRequest;
use ic_query::cache::build_cache_status_report;
use ic_query_text::cache::cache_status_report_text;
use std::io;
use thiserror::Error as ThisError;
const STATUS_HELP_AFTER: &str = "\
Examples:
icq cache status
icq cache status --json";
#[derive(Debug, ThisError)]
pub enum CacheCommandError {
#[error(transparent)]
CacheRoot(#[from] CacheRootError),
#[error(transparent)]
Status(#[from] CacheStatusError),
#[error(transparent)]
Clock(#[from] CurrentUnixSecsError),
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
}
pub fn command() -> ClapCommand {
ClapCommand::new("cache")
.bin_name("icq cache")
.about("Inspect the local ic-query cache")
.subcommand(
ClapCommand::new("status")
.bin_name("icq cache status")
.about("Show cache headers, age, invalid recovery, and refresh locks")
.arg(json_arg())
.after_help(collection_help(
COLLECTION_MODE_CACHE_ONLY,
STATUS_HELP_AFTER,
)),
)
}
pub fn run_matches(matches: &ArgMatches) -> Result<(), CacheCommandError> {
match matches.subcommand() {
Some(("status", matches)) => {
let report = build_cache_status_report(&CacheStatusRequest::new(
cache_root()?,
current_unix_secs()?,
))?;
write_text_or_json(output_format(matches), &report, cache_status_report_text)
}
_ => unreachable!("clap requires a known cache subcommand"),
}
}
#[cfg(test)]
mod tests;