]> git.proxmox.com Git - pve-access-control.git/blobdiff - PVE/RPCEnvironment.pm
PVE/RPCEnvironment.pm: new function setup_default_cli_env
[pve-access-control.git] / PVE / RPCEnvironment.pm
index 3ec2c912d220498ca50303e23db5a25e0b67b0c1..30415ebe4f3ca85d99652426a913f0ad59b831c0 100644 (file)
@@ -513,6 +513,23 @@ sub init {
     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 {