all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Lorenz Stechauner <l.stechauner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH storage 2/2] fix #3633: pvesm: use print_api_results
Date: Mon, 27 Sep 2021 09:09:17 +0200	[thread overview]
Message-ID: <20210927070917.1504165-2-l.stechauner@proxmox.com> (raw)
In-Reply-To: <20210927070917.1504165-1-l.stechauner@proxmox.com>

Signed-off-by: Lorenz Stechauner <l.stechauner@proxmox.com>
---
 PVE/CLI/pvesm.pm | 144 ++++++-----------------------------------------
 1 file changed, 18 insertions(+), 126 deletions(-)

diff --git a/PVE/CLI/pvesm.pm b/PVE/CLI/pvesm.pm
index 7afbe22..7bb363c 100755
--- a/PVE/CLI/pvesm.pm
+++ b/PVE/CLI/pvesm.pm
@@ -178,68 +178,6 @@ __PACKAGE__->register_method ({
 	return;
     }});
 
-my $print_content = sub {
-    my ($list) = @_;
-
-    my ($maxlenname, $maxsize) = (0, 0);
-    foreach my $info (@$list) {
-	my $volid = $info->{volid};
-	my $sidlen =  length ($volid);
-	$maxlenname = $sidlen if $sidlen > $maxlenname;
-	$maxsize = $info->{size} if ($info->{size} // 0) > $maxsize;
-    }
-    my $sizemaxdigits = length($maxsize);
-
-    my $basefmt = "%-${maxlenname}s %-7s %-9s %${sizemaxdigits}s";
-    printf "$basefmt %s\n", "Volid", "Format", "Type", "Size", "VMID";
-
-    foreach my $info (@$list) {
-	next if !$info->{vmid};
-	my $volid = $info->{volid};
-
-	printf "$basefmt %d\n", $volid, $info->{format}, $info->{content}, $info->{size}, $info->{vmid};
-    }
-
-    foreach my $info (sort { $a->{format} cmp $b->{format} } @$list) {
-	next if $info->{vmid};
-	my $volid = $info->{volid};
-
-	printf "$basefmt\n", $volid, $info->{format}, $info->{content}, $info->{size};
-    }
-};
-
-my $print_status = sub {
-    my $res = shift;
-
-    my $maxlen = 0;
-    foreach my $res (@$res) {
-	my $storeid = $res->{storage};
-	$maxlen = length ($storeid) if length ($storeid) > $maxlen;
-    }
-    $maxlen+=1;
-
-    printf "%-${maxlen}s %10s %10s %15s %15s %15s %8s\n", 'Name', 'Type',
-	'Status', 'Total', 'Used', 'Available', '%';
-
-    foreach my $res (sort { $a->{storage} cmp $b->{storage} } @$res) {
-	my $storeid = $res->{storage};
-
-	my $active = $res->{active} ? 'active' : 'inactive';
-	my ($per, $per_fmt) = (0, '% 7.2f%%');
-	$per = ($res->{used}*100)/$res->{total} if $res->{total} > 0;
-
-	if (!$res->{enabled}) {
-	    $per = 'N/A';
-	    $per_fmt = '% 8s';
-	    $active = 'disabled';
-	}
-
-	printf "%-${maxlen}s %10s %10s %15d %15d %15d $per_fmt\n", $storeid,
-	    $res->{type}, $active, $res->{total}/1024, $res->{used}/1024,
-	    $res->{avail}/1024, $per;
-    }
-};
-
 __PACKAGE__->register_method ({
     name => 'export',
     path => 'export',
@@ -594,14 +532,16 @@ our $cmddef = {
 	'index',
 	[],
 	{ node => $nodename },
-	$print_status,
+	$print_api_result,
+	$PVE::RESTHandler::standard_output_options,
     ],
     list => [
 	"PVE::API2::Storage::Content",
 	'index',
 	['storage'],
 	{ node => $nodename },
-	$print_content,
+	$print_api_result,
+	$PVE::RESTHandler::standard_output_options,
     ],
     alloc => [
 	"PVE::API2::Storage::Content",
@@ -625,91 +565,48 @@ our $cmddef = {
 	    'nfsscan',
 	    ['server'],
 	    { node => $nodename },
-	    sub  {
-		my $res = shift;
-
-		my $maxlen = 0;
-		foreach my $rec (@$res) {
-		    my $len = length ($rec->{path});
-		    $maxlen = $len if $len > $maxlen;
-		}
-		foreach my $rec (@$res) {
-		    printf "%-${maxlen}s %s\n", $rec->{path}, $rec->{options};
-		}
-	    },
+	    $print_api_result,
+	    $PVE::RESTHandler::standard_output_options,
 	],
 	cifs => [
 	    "PVE::API2::Storage::Scan",
 	    'cifsscan',
 	    ['server'],
 	    { node => $nodename },
-	    sub  {
-		my $res = shift;
-
-		my $maxlen = 0;
-		foreach my $rec (@$res) {
-		    my $len = length ($rec->{share});
-		    $maxlen = $len if $len > $maxlen;
-		}
-		foreach my $rec (@$res) {
-		    printf "%-${maxlen}s %s\n", $rec->{share}, $rec->{description};
-		}
-	    },
+	    $print_api_result,
+	    $PVE::RESTHandler::standard_output_options,
 	],
 	glusterfs => [
 	    "PVE::API2::Storage::Scan",
 	    'glusterfsscan',
 	    ['server'],
 	    { node => $nodename },
-	    sub  {
-		my $res = shift;
-
-		foreach my $rec (@$res) {
-		    printf "%s\n", $rec->{volname};
-		}
-	    },
+	    $print_api_result,
+	    $PVE::RESTHandler::standard_output_options,
 	],
 	iscsi => [
 	    "PVE::API2::Storage::Scan",
 	    'iscsiscan',
 	    ['portal'],
 	    { node => $nodename },
-	    sub  {
-		my $res = shift;
-
-		my $maxlen = 0;
-		foreach my $rec (@$res) {
-		    my $len = length ($rec->{target});
-		    $maxlen = $len if $len > $maxlen;
-		}
-		foreach my $rec (@$res) {
-		    printf "%-${maxlen}s %s\n", $rec->{target}, $rec->{portal};
-		}
-	    },
+	    $print_api_result,
+	    $PVE::RESTHandler::standard_output_options,
 	],
 	lvm => [
 	    "PVE::API2::Storage::Scan",
 	    'lvmscan',
 	    [],
 	    { node => $nodename },
-	    sub  {
-		my $res = shift;
-		foreach my $rec (@$res) {
-		    printf "$rec->{vg}\n";
-		}
-	    },
+	    $print_api_result,
+	    $PVE::RESTHandler::standard_output_options,
 	],
 	lvmthin => [
 	    "PVE::API2::Storage::Scan",
 	    'lvmthinscan',
 	    ['vg'],
 	    { node => $nodename },
-	    sub  {
-		my $res = shift;
-		foreach my $rec (@$res) {
-		    printf "$rec->{lv}\n";
-		}
-	    },
+	    $print_api_result,
+	    $PVE::RESTHandler::standard_output_options,
 	],
 	pbs => [
 	    "PVE::API2::Storage::Scan",
@@ -724,13 +621,8 @@ our $cmddef = {
 	    'zfsscan',
 	    [],
 	    { node => $nodename },
-	    sub  {
-		my $res = shift;
-
-		foreach my $rec (@$res) {
-		    printf "$rec->{pool}\n";
-		}
-	    },
+	    $print_api_result,
+	    $PVE::RESTHandler::standard_output_options,
 	],
     },
     nfsscan => { alias => 'scan nfs' },
-- 
2.30.2





  reply	other threads:[~2021-09-27  7:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-27  7:09 [pve-devel] [PATCH storage 1/2] pvesm: reformat cmd definitions Lorenz Stechauner
2021-09-27  7:09 ` Lorenz Stechauner [this message]
2021-09-27 12:36 ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210927070917.1504165-2-l.stechauner@proxmox.com \
    --to=l.stechauner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal