-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache.rs
More file actions
111 lines (89 loc) · 2.87 KB
/
cache.rs
File metadata and controls
111 lines (89 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::{sync::Arc, time::Duration};
use clap::{Args, Subcommand};
use comfy_table::{ColumnConstraint, Table, Width, presets::UTF8_FULL};
use snafu::{ResultExt, Snafu};
use stackable_cockpit::xfer::{self, cache::DeleteFilter};
use tracing::{info, instrument};
use crate::cli::Cli;
#[derive(Debug, Args)]
pub struct CacheArgs {
#[command(subcommand)]
subcommand: CacheCommands,
}
#[derive(Debug, Subcommand)]
pub enum CacheCommands {
/// List cached files
#[command(aliases(["ls"]))]
List,
/// Clean cached files
#[command(aliases(["rm", "purge"]))]
Clean(CacheCleanArgs),
}
#[derive(Debug, Args)]
pub struct CacheCleanArgs {
/// Only remove outdated files in the cache
#[arg(long = "old", visible_aliases(["outdated"]))]
only_remove_old_files: bool,
}
#[derive(Debug, Snafu)]
pub enum CmdError {
#[snafu(display("failed to list cached files"))]
ListCachedFiles { source: xfer::Error },
#[snafu(display("failed to purge cached files"))]
PurgeCachedFiles { source: xfer::Error },
}
impl CacheArgs {
pub async fn run(&self, transfer_client: Arc<xfer::Client>) -> Result<String, CmdError> {
match &self.subcommand {
CacheCommands::List => list_cmd(transfer_client).await,
CacheCommands::Clean(args) => clean_cmd(args, transfer_client).await,
}
}
}
#[instrument(skip_all)]
async fn list_cmd(transfer_client: Arc<xfer::Client>) -> Result<String, CmdError> {
info!("Listing cached files");
let files = transfer_client
.list_cached_files()
.await
.context(ListCachedFilesSnafu)?;
if files.is_empty() {
return Ok("No cached files".into());
}
let mut table = Table::new();
table
.set_header(vec!["FILE", "LAST SYNC"])
.set_constraints(vec![ColumnConstraint::UpperBoundary(Width::Percentage(80))])
.load_preset(UTF8_FULL);
for (path, modified) in files {
let file_path = path.to_str().unwrap_or("Invalid UTF-8 Path").to_string();
let modified = modified
.elapsed()
.unwrap_or(Duration::ZERO)
.as_secs()
.to_string();
table.add_row(vec![file_path, format!("{modified} seconds ago")]);
}
let mut result = Cli::result();
result
.with_command_hint("stackablectl cache clean", "to clean all cached files")
.with_output(table.to_string());
Ok(result.render())
}
#[instrument(skip_all)]
async fn clean_cmd(
args: &CacheCleanArgs,
transfer_client: Arc<xfer::Client>,
) -> Result<String, CmdError> {
info!("Cleaning cached files");
let delete_filter = if args.only_remove_old_files {
DeleteFilter::OnlyExpired
} else {
DeleteFilter::All
};
transfer_client
.purge_cached_files(delete_filter)
.await
.context(PurgeCachedFilesSnafu)?;
Ok("Cleaned cached files".into())
}