[pve-devel] [RFC PATCH common 2/2] Replace 'string' with 'filepath' for mapped params

Fabian Grünbichler f.gruenbichler at proxmox.com
Tue Apr 5 09:20:51 CEST 2016


For the CLI man pages and documentation, mapped parameters
('content' strings in API2 which are loaded from file paths
in CLI) get special treatment and are displayed with the
type 'filepath'.
---
 src/PVE/CLIHandler.pm  | 27 +++++++++++++++++++--------
 src/PVE/RESTHandler.pm | 19 +++++++++++++++----
 2 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 9a2f841..b684ca8 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -98,9 +98,11 @@ __PACKAGE__->register_method ({
 	raise_param_exc({ cmd => "no such command '$cmd'"}) if !$class;
 
 	my $pwcallback = $cli_handler_class->can('read_password');
+	my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
 
 	my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
-				    $verbose ? 'full' : 'short', $pwcallback);
+				    $verbose ? 'full' : 'short', $pwcallback,
+				    $stringfilemap);
 	if ($verbose) {
 	    print "$str\n";
 	} else {
@@ -117,10 +119,12 @@ sub print_simple_asciidoc_synopsys {
     die "not initialized" if !$cli_handler_class;
 
     my $pwcallback = $cli_handler_class->can('read_password');
+    my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
 
     my $synopsis = "*${name}* `help`\n\n";
 
-    $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param, 'asciidoc', $pwcallback);
+    $synopsis .= $class->usage_str($name, $name, $arg_param, $uri_param,
+				   'asciidoc', $pwcallback, $stringfilemap);
 
     return $synopsis;
 }
@@ -130,6 +134,7 @@ sub print_asciidoc_synopsys {
     die "not initialized" if !($cmddef && $exename && $cli_handler_class);
 
     my $pwcallback = $cli_handler_class->can('read_password');
+    my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
 
     my $synopsis = "";
 
@@ -139,7 +144,8 @@ sub print_asciidoc_synopsys {
     foreach my $cmd (sort keys %$cmddef) {
 	my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
 	my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
-				    $uri_param, 'asciidoc', $pwcallback);
+				    $uri_param, 'asciidoc', $pwcallback,
+				    $stringfilemap);
 	$synopsis .= "\n" if $oldclass && $oldclass ne $class;
 
 	$synopsis .= "$str\n\n";
@@ -157,9 +163,10 @@ sub print_simple_pod_manpage {
     die "not initialized" if !$cli_handler_class;
 
     my $pwcallback = $cli_handler_class->can('read_password');
+    my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
 
     my $synopsis = " $name help\n\n";
-    my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
+    my $str = $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
     $str =~ s/^USAGE://;
     $str =~ s/\n/\n /g;
     $synopsis .= $str;
@@ -176,6 +183,7 @@ sub print_pod_manpage {
     die "no pod file specified" if !$podfn;
 
     my $pwcallback = $cli_handler_class->can('read_password');
+    my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
 
     my $synopsis = "";
     
@@ -186,7 +194,8 @@ sub print_pod_manpage {
     foreach my $cmd (sorted_commands()) {
 	my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
 	my $str = $class->usage_str($name, "$exename $cmd", $arg_param,
-				    $uri_param, $style, $pwcallback);
+				    $uri_param, $style, $pwcallback,
+				    $stringfilemap);
 	$str =~ s/^USAGE: //;
 
 	$synopsis .= "\n" if $oldclass && $oldclass ne $class;
@@ -207,13 +216,14 @@ sub print_usage_verbose {
     die "not initialized" if !($cmddef && $exename && $cli_handler_class);
 
     my $pwcallback = $cli_handler_class->can('read_password');
+    my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
 
     print "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n\n";
 
     foreach my $cmd (sort keys %$cmddef) {
 	my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
 	my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param,
-				    'full', $pwcallback);
+				    'full', $pwcallback, $stringfilemap);
 	print "$str\n\n";
     }
 }
@@ -228,6 +238,7 @@ sub print_usage_short {
     die "not initialized" if !($cmddef && $exename && $cli_handler_class);
 
     my $pwcallback = $cli_handler_class->can('read_password');
+    my $stringfilemap = $cli_handler_class->can('string_param_file_mapping');
 
     print $fd "ERROR: $msg\n" if $msg;
     print $fd "USAGE: $exename <COMMAND> [ARGS] [OPTIONS]\n";
@@ -235,7 +246,7 @@ sub print_usage_short {
     my $oldclass;
     foreach my $cmd (sorted_commands()) {
 	my ($class, $name, $arg_param, $uri_param) = @{$cmddef->{$cmd}};
-	my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback);
+	my $str = $class->usage_str($name, "$exename $cmd", $arg_param, $uri_param, 'short', $pwcallback, $stringfilemap);
 	print $fd "\n" if $oldclass && $oldclass ne $class;
 	print $fd "       $str";
 	$oldclass = $class;
@@ -500,7 +511,7 @@ my $handle_simple_cmd = sub {
     if (scalar(@$args) >= 1) {
 	if ($args->[0] eq 'help') {
 	    my $str = "USAGE: $name help\n";
-	    $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback);
+	    $str .= $class->usage_str($name, $name, $arg_param, $uri_param, 'long', $pwcallback, $stringfilemap);
 	    print STDERR "$str\n\n";
 	    return;
 	} elsif ($args->[0] eq 'bashcomplete') {
diff --git a/src/PVE/RESTHandler.pm b/src/PVE/RESTHandler.pm
index 9e0980e..ad76b01 100644
--- a/src/PVE/RESTHandler.pm
+++ b/src/PVE/RESTHandler.pm
@@ -410,7 +410,7 @@ sub handle {
 # $format: 'asciidoc', 'pod' or 'text'
 # $style: 'config', 'arg' or 'fixed'
 my $get_property_description = sub {
-    my ($name, $style, $phash, $format, $hidepw) = @_;
+    my ($name, $style, $phash, $format, $hidepw, $fileparams) = @_;
 
     my $res = '';
 
@@ -426,6 +426,15 @@ my $get_property_description = sub {
 	$type = '';
     }
 
+    if ($fileparams && $type eq 'string') {
+	foreach my $elem (@$fileparams) {
+	    if ($name eq $elem) {
+		$type = 'filepath';
+		last;
+	    }
+	}
+    }
+
     if ($format eq 'asciidoc') {
 
 	if ($style eq 'config') {
@@ -508,8 +517,9 @@ my $get_property_description = sub {
 #   'full'     ... also include description
 #   'asciidoc' ... generate asciidoc for man pages (like 'full')
 # $hidepw      ... hide password option (use this if you provide a read passwork callback)
+# $stringfilemap ... mapping for string parameters to file path parameters
 sub usage_str {
-    my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw) = @_;
+    my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw, $stringfilemap) = @_;
 
     $format = 'long' if !$format;
 
@@ -564,7 +574,8 @@ sub usage_str {
 	    $base = "${name}[n]";
 	}
 
-	$opts .= &$get_property_description($base, 'arg', $prop->{$k}, 'text', $hidepw);
+	$opts .= &$get_property_description($base, 'arg', $prop->{$k}, 'text',
+					    $hidepw, &$stringfilemap($name));
 
 	if (!$prop->{$k}->{optional}) {
 	    $args .= " " if $args;
@@ -661,7 +672,7 @@ sub cli_handler {
 
 	die $err if !$ec || $ec ne "PVE::Exception" || !$err->is_param_exc();
 	
-	$err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $pwcallback);
+	$err->{usage} = $self->usage_str($name, $prefix, $arg_param, $fixed_param, 'short', $pwcallback, $stringfilemap);
 
 	die $err;
     }
-- 
2.1.4





More information about the pve-devel mailing list