Skip to content

Commit

Permalink
Skip printed warnings when parsing JSON from Homebrew
Browse files Browse the repository at this point in the history
  • Loading branch information
mattstauffer committed Feb 23, 2023
1 parent 7972319 commit 866bc7e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions cli/Valet/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function isBrewServiceRunning(string $name, bool $exactMatch = true): boo
public function isBrewServiceRunningAsRoot(string $name, bool $exactMatch = true): bool
{
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);
Expand All @@ -172,12 +172,26 @@ public function isBrewServiceRunningAsRoot(string $name, bool $exactMatch = true
public function isBrewServiceRunningAsUser(string $name, bool $exactMatch = true): bool
{
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);
}

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
{
foreach ($serviceList as $service) {
Expand Down

0 comments on commit 866bc7e

Please sign in to comment.