]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
add run() method to CLIHandler
[pve-common.git] / src / PVE / CLIHandler.pm
index 6d3f69dd06317b26e5685cf437f115d158e7406a..aee6bb2a4f8de2581a763b2520709b32bd5525bc 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use Data::Dumper;
 
+use PVE::SafeSyslog;
 use PVE::Exception qw(raise raise_param_exc);
 use PVE::RESTHandler;
 use PVE::PodParser;
@@ -261,6 +262,68 @@ my $print_bash_completion = sub {
     &$print_result(@option_list);
 };
 
+sub verify_api {
+    my ($class) = @_;
+
+    # simply verify all registered methods
+    PVE::RESTHandler::validate_method_schemas();
+}
+
+sub generate_pod_manpage {
+    my ($class, $podfn) = @_;
+
+    no strict 'refs'; 
+    $cmddef = ${"${class}::cmddef"};
+
+    $exename = $class;
+    $exename =~ s/^.*:://;
+
+    if (!defined($podfn)) {
+       my $cpath = "$class.pm";
+       $cpath =~ s/::/\//g;
+       foreach my $p (@INC) {
+           my $testfn = "$p/$cpath";
+           if (-f $testfn) {
+               $podfn = $testfn;
+               last;
+           }
+       }
+    }
+
+    die "unable to find source for class '$class'" if !$podfn;
+
+    print_pod_manpage($podfn);
+}
+
+sub run {
+    my ($class, $pwcallback, $preparefunc) = @_;
+
+    $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
+
+    no strict 'refs';
+    $cmddef = ${"${class}::cmddef"};
+
+    $exename = $class;
+    $exename =~ s/^.*:://;
+
+    initlog($exename);
+
+    die "please run as root\n" if $> != 0;
+
+    PVE::INotify::inotify_init();
+
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user('root@pam');
+
+    my $cmd = shift @ARGV;
+
+    handle_cmd($cmddef, $exename, $cmd, \@ARGV, $pwcallback, $0, $preparefunc);
+
+    exit 0;
+}
+
 sub handle_cmd {
     my ($def, $cmdname, $cmd, $args, $pwcallback, $podfn, $preparefunc) = @_;