1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-06 08:40:09 +01:00

run commands as sudo

This commit is contained in:
Taylor Otwell
2016-05-11 19:40:44 -05:00
parent e0003c6176
commit fd595b5c71
7 changed files with 55 additions and 28 deletions

View File

@@ -8,10 +8,24 @@ class CommandLine
{
/**
* Simple global function to run commands.
*
* @param string $command
* @return void
*/
public function quietly($command)
{
(new Process($command))->run();
$this->runCommand($command.' > /dev/null 2>&1');
}
/**
* Simple global function to run commands.
*
* @param string $command
* @return void
*/
public function quietlyAsUser($command)
{
$this->quietly('sudo -u '.user().' '.$command.' > /dev/null 2>&1');
}
/**
@@ -26,7 +40,7 @@ public function passthru($command)
}
/**
* Run the given command.
* Run the given command as the non-root user.
*
* @param string $command
* @param callable $onError
@@ -34,17 +48,29 @@ public function passthru($command)
*/
public function run($command, callable $onError = null)
{
return $this->runAsRoot('sudo -u '.$_SERVER['SUDO_USER'].' '.$command, $onError);
return $this->runCommand($command, $onError);
}
/**
* Run the given command as root.
* Run the given command.
*
* @param string $command
* @param callable $onError
* @return string
*/
public function runAsRoot($command, callable $onError = null)
public function runAsUser($command, callable $onError = null)
{
return $this->runCommand('sudo -u '.user().' '.$command, $onError);
}
/**
* Run the given command.
*
* @param string $command
* @param callable $onError
* @return string
*/
protected function runCommand($command, callable $onError = null)
{
$onError = $onError ?: function () {};