]> git.proxmox.com Git - pve-common.git/commitdiff
add run() method to CLIHandler
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 3 Sep 2015 16:58:27 +0000 (18:58 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 3 Sep 2015 16:58:27 +0000 (18:58 +0200)
we can reuse that code for several binaries.

src/PVE/CLIHandler.pm

index 6ccfcd2c041e6a4039bbfb372d8fa012059d4597..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;
@@ -294,6 +295,35 @@ sub generate_pod_manpage {
     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) = @_;