src/bin/proxmox-backup-client.rs - list_snapshots: use format_and_print_result_full()

Depend on proxmox v0.1.14.
This commit is contained in:
Dietmar Maurer 2020-02-27 11:27:44 +01:00
parent af934f8cf6
commit f24fc1166b
3 changed files with 33 additions and 27 deletions

View File

@ -34,7 +34,7 @@ pam = "0.7"
pam-sys = "0.5" pam-sys = "0.5"
percent-encoding = "2.1" percent-encoding = "2.1"
pin-utils = "0.1.0-alpha" pin-utils = "0.1.0-alpha"
proxmox = { version = "0.1.13", features = [ "sortable-macro", "api-macro" ] } proxmox = { version = "0.1.14", features = [ "sortable-macro", "api-macro" ] }
#proxmox = { git = "ssh://gitolite3@proxdev.maurer-it.com/rust/proxmox", version = "0.1.2", features = [ "sortable-macro", "api-macro" ] } #proxmox = { git = "ssh://gitolite3@proxdev.maurer-it.com/rust/proxmox", version = "0.1.2", features = [ "sortable-macro", "api-macro" ] }
#proxmox = { path = "../proxmox/proxmox", features = [ "sortable-macro", "api-macro" ] } #proxmox = { path = "../proxmox/proxmox", features = [ "sortable-macro", "api-macro" ] }
regex = "1.2" regex = "1.2"

View File

@ -229,7 +229,7 @@ fn delete_snapshot(
}, },
)] )]
/// List backup snapshots. /// List backup snapshots.
fn list_snapshots ( pub fn list_snapshots (
param: Value, param: Value,
_info: &ApiMethod, _info: &ApiMethod,
_rpcenv: &mut dyn RpcEnvironment, _rpcenv: &mut dyn RpcEnvironment,

View File

@ -210,7 +210,7 @@ async fn api_datastore_list_snapshots(
client: &HttpClient, client: &HttpClient,
store: &str, store: &str,
group: Option<BackupGroup>, group: Option<BackupGroup>,
) -> Result<Vec<SnapshotListItem>, Error> { ) -> Result<Value, Error> {
let path = format!("api2/json/admin/datastore/{}/snapshots", store); let path = format!("api2/json/admin/datastore/{}/snapshots", store);
@ -222,9 +222,7 @@ async fn api_datastore_list_snapshots(
let mut result = client.get(&path, Some(args)).await?; let mut result = client.get(&path, Some(args)).await?;
let list: Vec<SnapshotListItem> = serde_json::from_value(result["data"].take())?; Ok(result["data"].take())
Ok(list)
} }
async fn api_datastore_latest_snapshot( async fn api_datastore_latest_snapshot(
@ -233,7 +231,8 @@ async fn api_datastore_latest_snapshot(
group: BackupGroup, group: BackupGroup,
) -> Result<(String, String, DateTime<Utc>), Error> { ) -> Result<(String, String, DateTime<Utc>), Error> {
let mut list = api_datastore_list_snapshots(client, store, Some(group.clone())).await?; let list = api_datastore_list_snapshots(client, store, Some(group.clone())).await?;
let mut list: Vec<SnapshotListItem> = serde_json::from_value(list)?;
if list.is_empty() { if list.is_empty() {
bail!("backup group {:?} does not contain any snapshots.", group.group_path()); bail!("backup group {:?} does not contain any snapshots.", group.group_path());
@ -445,34 +444,41 @@ async fn list_snapshots(param: Value) -> Result<Value, Error> {
None None
}; };
let mut list = api_datastore_list_snapshots(&client, repo.store(), group).await?; let mut data = api_datastore_list_snapshots(&client, repo.store(), group).await?;
list.sort_unstable_by(|a, b| a.backup_time.cmp(&b.backup_time));
record_repository(&repo); record_repository(&repo);
if output_format != "text" { let render_snapshot_path = |_v: &Value, record: &Value| -> Result<String, Error> {
format_and_print_result(&serde_json::to_value(list)?, &output_format); let item: SnapshotListItem = serde_json::from_value(record.to_owned())?;
return Ok(Value::Null);
}
for item in list {
let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time); let snapshot = BackupDir::new(item.backup_type, item.backup_id, item.backup_time);
Ok(snapshot.relative_path().to_str().unwrap().to_owned())
};
let path = snapshot.relative_path().to_str().unwrap().to_owned(); let render_files = |_v: &Value, record: &Value| -> Result<String, Error> {
let item: SnapshotListItem = serde_json::from_value(record.to_owned())?;
let files = item.files.iter() let mut files: Vec<String> = item.files.iter()
.map(|v| strip_server_file_expenstion(&v)) .map(|v| strip_server_file_expenstion(&v))
.collect(); .collect();
let size_str = if let Some(size) = item.size { files.sort();
size.to_string()
} else { Ok(tools::join(&files, ' '))
String::from("-") };
};
println!("{} | {} | {}", path, size_str, tools::join(&files, ' ')); let options = TableFormatOptions::new()
} .noborder(false)
.noheader(false)
.sortby("backup-type", false)
.sortby("backup-id", false)
.sortby("backup-time", false)
.column(ColumnConfig::new("backup-id").renderer(render_snapshot_path).header("snapshot"))
.column(ColumnConfig::new("size"))
.column(ColumnConfig::new("files").renderer(render_files))
;
let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_LIST_SNAPSHOTS;
format_and_print_result_full(&mut data, info, &output_format, &options);
Ok(Value::Null) Ok(Value::Null)
} }