]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/RESTHandler.pm
improve asciidoc markup
[pve-common.git] / src / PVE / RESTHandler.pm
index ae0a6953acb067a9bba4674c046a97ebc46ed473..9d9ea566ccff7dd18af2be9245347a15aa54ba34 100644 (file)
@@ -9,7 +9,7 @@ use PVE::JSONSchema;
 use PVE::PodParser;
 use HTTP::Status qw(:constants :is status_message);
 use Text::Wrap;
 use PVE::PodParser;
 use HTTP::Status qw(:constants :is status_message);
 use Text::Wrap;
-use Storable qw(dclone);
+use Clone qw(clone);
 
 my $method_registry = {};
 my $method_by_name = {};
 
 my $method_registry = {};
 my $method_by_name = {};
@@ -27,7 +27,7 @@ sub api_clone_schema {
     foreach my $k (keys %$schema) {
        my $d = $schema->{$k};
        if ($k ne 'properties') {
     foreach my $k (keys %$schema) {
        my $d = $schema->{$k};
        if ($k ne 'properties') {
-           $res->{$k} = ref($d) ? dclone($d) : $d;
+           $res->{$k} = ref($d) ? clone($d) : $d;
            next;
        }
        # convert indexed parameters like -net\d+ to -net[n]
            next;
        }
        # convert indexed parameters like -net\d+ to -net[n]
@@ -40,7 +40,7 @@ sub api_clone_schema {
                    next;
                }
            }
                    next;
                }
            }
-           $res->{$k}->{$p} = ref($pd) ? dclone($pd) : $pd;
+           $res->{$k}->{$p} = ref($pd) ? clone($pd) : $pd;
        }
     }
 
        }
     }
 
@@ -105,7 +105,7 @@ sub api_dump_full {
                        $data->{$k} = api_clone_schema($d);
                    } else {
 
                        $data->{$k} = api_clone_schema($d);
                    } else {
 
-                       $data->{$k} = ref($d) ? dclone($d) : $d;
+                       $data->{$k} = ref($d) ? clone($d) : $d;
                    }
                } 
                $res->{info}->{$info->{method}} = $data;
                    }
                } 
                $res->{info}->{$info->{method}} = $data;
@@ -386,9 +386,11 @@ sub handle {
        # warn "validate ". Dumper($param}) . "\n" . Dumper($schema);
        PVE::JSONSchema::validate($param, $schema);
        # untaint data (already validated)
        # warn "validate ". Dumper($param}) . "\n" . Dumper($schema);
        PVE::JSONSchema::validate($param, $schema);
        # untaint data (already validated)
+       my $extra = delete $param->{'extra-args'};
        while (my ($key, $val) = each %$param) {
            ($param->{$key}) = $val =~ /^(.*)$/s;
        }
        while (my ($key, $val) = each %$param) {
            ($param->{$key}) = $val =~ /^(.*)$/s;
        }
+       $param->{'extra-args'} = [map { /^(.*)$/ } @$extra] if $extra;
     }
 
     my $result = &$func($param); 
     }
 
     my $result = &$func($param); 
@@ -412,6 +414,7 @@ sub handle {
 #   'long'     ... default (list all options)
 #   'short'    ... command line only (one line)
 #   'full'     ... also include description
 #   'long'     ... default (list all options)
 #   'short'    ... command line only (one line)
 #   '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)
 sub usage_str {
     my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw) = @_;
 # $hidepw      ... hide password option (use this if you provide a read passwork callback)
 sub usage_str {
     my ($self, $name, $prefix, $arg_param, $fixed_param, $format, $hidepw) = @_;
@@ -458,22 +461,37 @@ sub usage_str {
        if ($hidepw && $k eq 'password') {
            $type = '';
        }
        if ($hidepw && $k eq 'password') {
            $type = '';
        }
-       
-       my $defaulttxt = '';
-       if (defined(my $dv = $phash->{default})) {
-           $defaulttxt = "   (default=$dv)";
-       }
-       my $tmp = sprintf "  %-10s %s$defaulttxt\n", $display_name, "$type";
-       my $indend = "             ";
 
 
-       $res .= Text::Wrap::wrap('', $indend, ($tmp));
-       $res .= "\n",
-       $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n";
+       if ($format eq 'asciidoc') {
+           $res .= "`$display_name` `$type` ";
+           if (defined(my $dv = $phash->{default})) {
+               $res .= "(default=`$dv`)";
+           }
+           $res .= "::\n\n";
+           $res .= Text::Wrap::wrap('', '', ($descr)) . "\n";
+
+           if (my $req = $phash->{requires}) {
+               my $tmp .= ref($req) ? join(', ', @$req) : $req;
+               $res .= "+\nNOTE: Requires option(s): `$tmp`\n";
+           }
+           $res .= "\n";
+       } else {
+           my $defaulttxt = '';
+           if (defined(my $dv = $phash->{default})) {
+               $defaulttxt = "   (default=$dv)";
+           }
+           my $tmp = sprintf "  %-10s %s$defaulttxt\n", $display_name, "$type";
+           my $indend = "             ";
+
+           $res .= Text::Wrap::wrap('', $indend, ($tmp));
+           $res .= "\n",
+           $res .= Text::Wrap::wrap($indend, $indend, ($descr)) . "\n\n";
 
 
-       if (my $req = $phash->{requires}) {
-           my $tmp = "Requires option(s): ";
-           $tmp .= ref($req) ? join(', ', @$req) : $req;
-           $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n";
+           if (my $req = $phash->{requires}) {
+               my $tmp = "Requires option(s): ";
+               $tmp .= ref($req) ? join(', ', @$req) : $req;
+               $res .= Text::Wrap::wrap($indend, $indend, ($tmp)). "\n\n";
+           }
        }
 
        return $res;
        }
 
        return $res;
@@ -513,17 +531,26 @@ sub usage_str {
        }
     } 
 
        }
     } 
 
-    $out .= "USAGE: " if $format ne 'short';
-
-    $out .= "$prefix $args";
-
-    $out .= $opts ? " [OPTIONS]\n" : "\n";
+    if ($format eq 'asciidoc') {
+       $out .= "*${prefix}*";
+       $out .= " `$args`" if $args;
+       $out .= $opts ? " `[OPTIONS]`\n" : "\n";
+    } else {
+       $out .= "USAGE: " if $format ne 'short';
+       $out .= "$prefix $args";
+       $out .= $opts ? " [OPTIONS]\n" : "\n";
+    }
 
     return $out if $format eq 'short';
 
 
     return $out if $format eq 'short';
 
-    if ($info->{description} && $format eq 'full') {
-       my $desc = Text::Wrap::wrap('  ', '  ', ($info->{description}));
-       $out .= "\n$desc\n\n";
+    if ($info->{description}) {
+       if ($format eq 'asciidoc') {
+           my $desc = Text::Wrap::wrap('', '', ($info->{description}));
+           $out .= "\n$desc\n\n";
+       } elsif ($format eq 'full') {
+           my $desc = Text::Wrap::wrap('  ', '  ', ($info->{description}));
+           $out .= "\n$desc\n\n";
+       }
     }
 
     $out .= $argdescr if $argdescr;
     }
 
     $out .= $argdescr if $argdescr;