From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id CC3AA6A4AC for ; Tue, 16 Feb 2021 18:07:33 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 20A9818E47 for ; Tue, 16 Feb 2021 18:07:33 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 06D0418DF1 for ; Tue, 16 Feb 2021 18:07:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BC42C46259 for ; Tue, 16 Feb 2021 18:07:30 +0100 (CET) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Tue, 16 Feb 2021 18:07:00 +0100 Message-Id: <20210216170710.31767-13-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210216170710.31767-1-s.reiter@proxmox.com> References: <20210216170710.31767-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.029 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [proxmox-file-restore.rs] Subject: [pbs-devel] [PATCH proxmox-backup 12/22] file-restore: allow specifying output-format X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Feb 2021 17:07:33 -0000 Makes CLI use more comfortable by not just printing JSON to the terminal. Signed-off-by: Stefan Reiter --- src/bin/proxmox-file-restore.rs | 42 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/bin/proxmox-file-restore.rs b/src/bin/proxmox-file-restore.rs index f2d2ce3a..ec3378b0 100644 --- a/src/bin/proxmox-file-restore.rs +++ b/src/bin/proxmox-file-restore.rs @@ -4,11 +4,14 @@ use std::path::PathBuf; use std::sync::Arc; use anyhow::{bail, format_err, Error}; -use serde_json::Value; +use serde_json::{json, Value}; use proxmox::api::{ api, - cli::{run_cli_command, CliCommand, CliCommandMap, CliEnvironment}, + cli::{ + default_table_format_options, format_and_print_result_full, get_output_format, + run_cli_command, CliCommand, CliCommandMap, CliEnvironment, ColumnConfig, OUTPUT_FORMAT, + }, }; use pxar::accessor::aio::Accessor; @@ -96,11 +99,22 @@ fn parse_path(path: String, base64: bool) -> Result { type: CryptMode, optional: true, }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, + } + }, + returns: { + description: "A list of elements under the given path", + type: Array, + items: { + type: ArchiveEntry, } } )] /// List a directory from a backup snapshot. -async fn list(param: Value) -> Result, Error> { +async fn list(param: Value) -> Result { let repo = extract_repository_from_value(¶m)?; let base64 = param["base64"].as_bool().unwrap_or(false); let path = parse_path( @@ -137,7 +151,7 @@ async fn list(param: Value) -> Result, Error> { let (manifest, _) = client.download_manifest().await?; manifest.check_fingerprint(crypt_config.as_ref().map(Arc::as_ref))?; - match path { + let result = match path { ExtractPath::ListArchives => { let mut entries = vec![]; for file in manifest.files() { @@ -173,7 +187,25 @@ async fn list(param: Value) -> Result, Error> { helpers::list_dir_content(&mut catalog_reader, &fullpath) } - } + }?; + + let options = default_table_format_options() + .sortby("type", false) + .sortby("text", false) + .column(ColumnConfig::new("type")) + .column(ColumnConfig::new("text").header("name")) + .column(ColumnConfig::new("mtime").header("last modified")) + .column(ColumnConfig::new("size")); + + let output_format = get_output_format(¶m); + format_and_print_result_full( + &mut json!(result), + &API_METHOD_LIST.returns, + &output_format, + &options, + ); + + Ok(Value::Null) } #[api( -- 2.20.1