1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-04 16:10:08 +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

@@ -88,9 +88,9 @@ function installCaddyDaemon()
*/
function restart()
{
$this->cli->quietly('launchctl unload '.$this->daemonPath);
$this->cli->quietly('sudo launchctl unload '.$this->daemonPath);
$this->cli->quietly('launchctl load '.$this->daemonPath);
$this->cli->quietly('sudo launchctl load '.$this->daemonPath);
}
/**
@@ -100,7 +100,7 @@ function restart()
*/
function stop()
{
$this->cli->quietly('launchctl unload '.$this->daemonPath);
$this->cli->quietly('sudo launchctl unload '.$this->daemonPath);
}
/**

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 () {};

View File

@@ -128,7 +128,7 @@ function createDomainResolver($domain)
*/
function updateDomain($oldDomain, $newDomain)
{
$this->cli->quietly('rm '.$this->resolverPath.'/'.$oldDomain);
$this->files->unlink($this->resolverPath.'/'.$oldDomain);
$this->install($newDomain);
}

View File

@@ -223,7 +223,9 @@ function symlink($target, $link, $owner = null)
*/
function unlink($path)
{
@unlink($path);
if ($this->exists($path)) {
@unlink($path);
}
}
/**

View File

@@ -6,17 +6,19 @@
class Site
{
var $config, $files;
var $config, $cli, $files;
/**
* Create a new Site instance.
*
* @param Configuration $config
* @param CommandLine $cli
* @param Filesystem $files
* @return void
*/
function __construct(Configuration $config, Filesystem $files)
function __construct(Configuration $config, CommandLine $cli, Filesystem $files)
{
$this->cli = $cli;
$this->files = $files;
$this->config = $config;
}
@@ -36,7 +38,7 @@ function link($target, $link)
$this->config->prependPath($linkPath);
$this->files->symlink($target, $linkPath.'/'.$link);
$this->cli->runAsUser('ln -s '.$target.' '.$linkPath.'/'.$link);
return $linkPath.'/'.$link;
}
@@ -81,7 +83,7 @@ function logs($paths)
$logPath = $path.'/'.$directory.'/storage/logs/laravel.log';
if ($this->files->isDir(dirname($logPath))) {
return $this->files->touch($logPath);
return $this->files->touchAsUser($logPath);
}
})->filter());
}

View File

@@ -28,9 +28,9 @@ function __construct(CommandLine $cli, Filesystem $files)
*/
function symlinkToUsersBin()
{
$this->cli->quietly('rm '.$this->valetBin);
$this->cli->quietlyAsUser('rm '.$this->valetBin);
$this->cli->run('ln -s '.realpath(__DIR__.'/../../valet').' '.$this->valetBin);
$this->cli->runAsUser('ln -s '.realpath(__DIR__.'/../../valet').' '.$this->valetBin);
}
/**