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

Skip printed warnings when parsing JSON from Homebrew

This commit is contained in:
Matt Stauffer
2023-02-22 23:39:56 -05:00
parent 79723194d7
commit 866bc7e259

View File

@@ -163,7 +163,7 @@ public function isBrewServiceRunning(string $name, bool $exactMatch = true): boo
public function isBrewServiceRunningAsRoot(string $name, bool $exactMatch = true): bool public function isBrewServiceRunningAsRoot(string $name, bool $exactMatch = true): bool
{ {
if (! $this->brewServicesRootOutput) { if (! $this->brewServicesRootOutput) {
$this->brewServicesRootOutput = json_decode($this->cli->run('brew services info --all --json'), false); $this->brewServicesRootOutput = $this->jsonFromCli('brew services info --all --json', true);
} }
return $this->isBrewServiceRunningGivenServiceList($this->brewServicesRootOutput, $name, $exactMatch); return $this->isBrewServiceRunningGivenServiceList($this->brewServicesRootOutput, $name, $exactMatch);
@@ -172,12 +172,26 @@ public function isBrewServiceRunningAsRoot(string $name, bool $exactMatch = true
public function isBrewServiceRunningAsUser(string $name, bool $exactMatch = true): bool public function isBrewServiceRunningAsUser(string $name, bool $exactMatch = true): bool
{ {
if (! $this->brewServicesUserOutput) { if (! $this->brewServicesUserOutput) {
$this->brewServicesUserOutput = json_decode($this->cli->runAsUser('brew services info --all --json'), false); $this->brewServicesUserOutput = $this->jsonFromCli('brew services info --all --json', false);
} }
return $this->isBrewServiceRunningGivenServiceList($this->brewServicesUserOutput, $name, $exactMatch); return $this->isBrewServiceRunningGivenServiceList($this->brewServicesUserOutput, $name, $exactMatch);
} }
public function jsonFromCli(string $input, bool $sudo = false): array
{
$contents = $sudo ? $this->cli->run($input) : $this->cli->runAsUser($input);
// Skip to the JSON, to avoid warnings; we're only getting arrays so start with [
$contents = substr($contents, strpos($contents, '['));
try {
return json_decode($contents, false, 512, JSON_THROW_ON_ERROR);
} catch (\Throwable $e) {
$command = $sudo ? 'sudo '.$input : $input;
throw new \Exception('Invalid JSON returned from command: '.$command);
}
}
protected function isBrewServiceRunningGivenServiceList(array $serviceList, string $name, bool $exactMatch = true): bool protected function isBrewServiceRunningGivenServiceList(array $serviceList, string $name, bool $exactMatch = true): bool
{ {
foreach ($serviceList as $service) { foreach ($serviceList as $service) {