Skip to content

Commit

Permalink
Fix invalid files being listed in "log" command
Browse files Browse the repository at this point in the history
Ignores output from other processes during the SSH connection

Copies a method from the RemoteEnvVars class
  • Loading branch information
pjcdawkins committed Dec 11, 2023
1 parent 08168fb commit ad61903
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Command/Environment/EnvironmentLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
/** @var \Doctrine\Common\Cache\CacheProvider $cache */
$cache = $this->getService('cache');
if (!$result = $cache->fetch($cacheKey)) {
$result = $host->runCommand('ls -1 ' . $logDir . '/*.log');
$result = $host->runCommand('echo -n _BEGIN_FILE_LIST_; ls -1 ' . $logDir . '/*.log; echo -n _END_FILE_LIST_');
if (is_string($result)) {
$result = $this->extractResult($result, '_BEGIN_FILE_LIST_', '_END_FILE_LIST_');
}

// Cache the list for 1 day.
$cache->save($cacheKey, $result, 86400);
Expand All @@ -84,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$logDir . '/access.log',
$logDir . '/error.log',
];
$files = $result ? explode("\n", $result) : $defaultFiles;
$files = $result && is_string($result) ? explode("\n", trim($result)) : $defaultFiles;

// Ask the user to choose a file.
$files = array_combine($files, array_map(function ($file) {
Expand Down Expand Up @@ -129,4 +132,25 @@ public function completeArgumentValues($argumentName, CompletionContext $context

return $values;
}

/**
* Extracts a result from 'echo' output between beginning and ending delimiters.
*
* @param string $output
* @param string $begin
* @param string $end
*
* @return string
*/
private function extractResult($output, $begin, $end)
{
$first = \strpos($output, $begin);
$last = \strrpos($output, $end, $first);
if ($first === false || $last === false) {
return $output;
}
$offset = $first + \strlen($begin);
$length = $last - $first - \strlen($begin);
return \substr($output, $offset, $length);
}
}

0 comments on commit ad61903

Please sign in to comment.