]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
createSchema: include type property
[pve-common.git] / src / PVE / CLIHandler.pm
index 5372198fdc2c1d5d57d345438938f39470bac042..516d6320bf7d2f752991fc0aa49d4a877b71b474 100644 (file)
@@ -199,9 +199,8 @@ my $print_bash_completion = sub {
     my $cmdline = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT});
     print STDERR "\nCMDLINE: $ENV{COMP_LINE}\n" if $debug;
 
-    # fixme: shell quoting??
-    my @args = split(/\s+/, $cmdline);
-    my $pos = scalar(@args) - 2;
+    my $args = PVE::Tools::split_args($cmdline);
+    my $pos = scalar(@$args) - 2;
     $pos += 1 if $cmdline =~ m/\s+$/;
 
     print STDERR "CMDLINE:$pos:$cmdline\n" if $debug;
@@ -222,7 +221,7 @@ my $print_bash_completion = sub {
            &$print_result(keys %$cmddef);
            return;
        }
-       $cmd = $args[1];
+       $cmd = $args->[1];
     }
 
     my $def = $cmddef->{$cmd};
@@ -252,7 +251,7 @@ my $print_bash_completion = sub {
        if ($d->{completion}) {
            my $vt = ref($d->{completion});
            if ($vt eq 'CODE') {
-               my $res = $d->{completion}->($cmd, $pname, $cur);
+               my $res = $d->{completion}->($cmd, $pname, $cur, $args);
                &$print_result(@$res);
            }
        } elsif ($d->{type} eq 'boolean') {
@@ -297,13 +296,22 @@ sub verify_api {
     PVE::RESTHandler::validate_method_schemas();
 }
 
+my $get_exe_name = sub {
+    my ($class) = @_;
+    
+    my $name = $class;
+    $name =~ s/^.*:://;
+    $name =~ s/_/-/g;
+
+    return $name;
+};
+
 sub generate_bash_completions {
     my ($class) = @_;
 
     # generate bash completion config
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    $exename = &$get_exe_name($class);
 
     print <<__EOD__;
 # $exename bash completion
@@ -318,12 +326,14 @@ __EOD__
 }
 
 sub find_cli_class_source {
-    my ($exename) = @_;
+    my ($name) = @_;
 
     my $filename;
 
-    my $cpath = "PVE/CLI/${exename}.pm";
-    my $spath = "PVE/Service/${exename}.pm";
+    $name =~ s/-/_/g;
+
+    my $cpath = "PVE/CLI/${name}.pm";
+    my $spath = "PVE/Service/${name}.pm";
     foreach my $p (@INC) {
        foreach my $s (($cpath, $spath)) {
            my $testfn = "$p/$s";
@@ -341,8 +351,7 @@ sub find_cli_class_source {
 sub generate_pod_manpage {
     my ($class, $podfn) = @_;
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    $exename = &$get_exe_name($class);
 
     $podfn = find_cli_class_source($exename) if !defined($podfn);
 
@@ -367,19 +376,21 @@ sub run_cli {
 
     $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
 
-    $exename = $class;
-    $exename =~ s/^.*:://;
+    $exename = &$get_exe_name($class);
 
     initlog($exename);
 
-    die "please run as root\n" if $> != 0;
 
-    PVE::INotify::inotify_init() if $class !~ m/^PVE::Service::/;
+    if ($class !~ m/^PVE::Service::/) {
+       die "please run as root\n" if $> != 0;
 
-    my $rpcenv = PVE::RPCEnvironment->init('cli');
-    $rpcenv->init_request();
-    $rpcenv->set_language($ENV{LANG});
-    $rpcenv->set_user('root@pam');
+       PVE::INotify::inotify_init();
+
+       my $rpcenv = PVE::RPCEnvironment->init('cli');
+       $rpcenv->init_request();
+       $rpcenv->set_language($ENV{LANG});
+       $rpcenv->set_user('root@pam');
+    }
 
     no strict 'refs';
     my $def = ${"${class}::cmddef"};