]> git.proxmox.com Git - pve-access-control.git/commitdiff
PVE/RPCEnvironment.pm: new function setup_default_cli_env
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 11 Jan 2017 11:12:11 +0000 (12:12 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 12 Jan 2017 09:01:17 +0000 (10:01 +0100)
Convenience function for command line tools.

PVE/RPCEnvironment.pm

index 3ec2c912d220498ca50303e23db5a25e0b67b0c1..30415ebe4f3ca85d99652426a913f0ad59b831c0 100644 (file)
@@ -513,6 +513,23 @@ sub init {
     return $self;
 }; 
 
     return $self;
 }; 
 
+# convenience function for command line tools
+sub setup_default_cli_env {
+    my ($username) = @_;
+
+    $username //= 'root@pam';
+
+    PVE::INotify::inotify_init();
+
+    my $rpcenv = PVE::RPCEnvironment->init('cli');
+    $rpcenv->init_request();
+    $rpcenv->set_language($ENV{LANG});
+    $rpcenv->set_user($username);
+
+    die "please run as root\n"
+       if ($username eq 'root@pam') && ($> != 0);
+}
+
 # get the singleton 
 sub get {
 
 # get the singleton 
 sub get {