From c81b2b7c704bab46acf72d5cecb964cff47e7afd Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 27 Feb 2020 13:31:23 +0100 Subject: [PATCH] src/bin/proxmox-backup-client.rs - list_groups: use format_and_print_result_full() Depend on proxmox 0.1.16-1 --- Cargo.toml | 2 +- src/bin/proxmox-backup-client.rs | 92 +++++++++++++------------------- 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0359892b..e907bcc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,7 +34,7 @@ pam = "0.7" pam-sys = "0.5" percent-encoding = "2.1" pin-utils = "0.1.0-alpha" -proxmox = { version = "0.1.15", features = [ "sortable-macro", "api-macro" ] } +proxmox = { version = "0.1.16", 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" ] } regex = "1.2" diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index c2496661..a569296e 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -167,6 +167,16 @@ fn complete_repository(_arg: &str, _param: &HashMap) -> Vec String { + let mut files: Vec = files.iter() + .map(|v| strip_server_file_expenstion(&v)) + .collect(); + + files.sort(); + + tools::join(&files, ' ') +} + fn connect(server: &str, userid: &str) -> Result { let fingerprint = std::env::var(ENV_VAR_PBS_FINGERPRINT).ok(); @@ -340,6 +350,8 @@ fn strip_server_file_expenstion(name: &str) -> String { /// List backup groups. async fn list_backup_groups(param: Value) -> Result { + let output_format = get_output_format(¶m); + let repo = extract_repository_from_value(¶m)?; let client = connect(repo.host(), repo.user())?; @@ -350,62 +362,36 @@ async fn list_backup_groups(param: Value) -> Result { record_repository(&repo); - // fixme: implement and use output formatter instead .. - let list = result["data"].as_array_mut().unwrap(); + let render_group_path = |_v: &Value, record: &Value| -> Result { + let item: GroupListItem = serde_json::from_value(record.to_owned())?; + let group = BackupGroup::new(item.backup_type, item.backup_id); + Ok(group.group_path().to_str().unwrap().to_owned()) + }; - list.sort_unstable_by(|a, b| { - let a_id = a["backup-id"].as_str().unwrap(); - let a_backup_type = a["backup-type"].as_str().unwrap(); - let b_id = b["backup-id"].as_str().unwrap(); - let b_backup_type = b["backup-type"].as_str().unwrap(); - - let type_order = a_backup_type.cmp(b_backup_type); - if type_order == std::cmp::Ordering::Equal { - a_id.cmp(b_id) - } else { - type_order - } - }); - - let output_format = get_output_format(¶m); - - let mut result = vec![]; - - for item in list { - - let id = item["backup-id"].as_str().unwrap(); - let btype = item["backup-type"].as_str().unwrap(); - let epoch = item["last-backup"].as_i64().unwrap(); + let render_backup_timestamp = |v: &Value, _record: &Value| -> Result { + let epoch = v.as_i64().unwrap(); let last_backup = Utc.timestamp(epoch, 0); - let backup_count = item["backup-count"].as_u64().unwrap(); + Ok(BackupDir::backup_time_to_string(last_backup)) + }; - let group = BackupGroup::new(btype, id); + let render_files = |_v: &Value, record: &Value| -> Result { + let item: GroupListItem = serde_json::from_value(record.to_owned())?; + Ok(render_backup_file_list(&item.files)) + }; - let path = group.group_path().to_str().unwrap().to_owned(); + let options = default_table_format_options() + .sortby("backup-type", false) + .sortby("backup-id", false) + .column(ColumnConfig::new("backup-id").renderer(render_group_path).header("group")) + .column(ColumnConfig::new("last-backup").renderer(render_backup_timestamp)) + .column(ColumnConfig::new("backup-count")) + .column(ColumnConfig::new("files").renderer(render_files)); - let files = item["files"].as_array().unwrap().iter() - .map(|v| strip_server_file_expenstion(v.as_str().unwrap())).collect(); + let mut data: Value = result["data"].take(); - if output_format == "text" { - println!( - "{:20} | {} | {:5} | {}", - path, - BackupDir::backup_time_to_string(last_backup), - backup_count, - tools::join(&files, ' '), - ); - } else { - result.push(json!({ - "backup-type": btype, - "backup-id": id, - "last-backup": epoch, - "backup-count": backup_count, - "files": files, - })); - } - } + let info = &proxmox_backup::api2::admin::datastore::API_RETURN_SCHEMA_LIST_GROUPS; - if output_format != "text" { format_and_print_result(&result.into(), &output_format); } + format_and_print_result_full(&mut data, info, &output_format, &options); Ok(Value::Null) } @@ -456,13 +442,7 @@ async fn list_snapshots(param: Value) -> Result { let render_files = |_v: &Value, record: &Value| -> Result { let item: SnapshotListItem = serde_json::from_value(record.to_owned())?; - let mut files: Vec = item.files.iter() - .map(|v| strip_server_file_expenstion(&v)) - .collect(); - - files.sort(); - - Ok(tools::join(&files, ' ')) + Ok(render_backup_file_list(&item.files)) }; let options = default_table_format_options()