mirror of
https://github.com/tiennm99/coolify.git
synced 2026-08-05 20:22:27 +00:00
feat: add function to extract inline comments from docker-compose YAML environment variables
This commit is contained in:
@@ -512,6 +512,215 @@ function parseEnvFormatToArray($env_file_contents)
|
||||
return $env_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract inline comments from environment variables in raw docker-compose YAML.
|
||||
*
|
||||
* Parses raw docker-compose YAML to extract inline comments from environment sections.
|
||||
* Standard YAML parsers discard comments, so this pre-processes the raw text.
|
||||
*
|
||||
* Handles both formats:
|
||||
* - Map format: `KEY: "value" # comment` or `KEY: value # comment`
|
||||
* - Array format: `- KEY=value # comment`
|
||||
*
|
||||
* @param string $rawYaml The raw docker-compose.yml content
|
||||
* @return array Map of environment variable keys to their inline comments
|
||||
*/
|
||||
function extractYamlEnvironmentComments(string $rawYaml): array
|
||||
{
|
||||
$comments = [];
|
||||
$lines = explode("\n", $rawYaml);
|
||||
$inEnvironmentBlock = false;
|
||||
$environmentIndent = 0;
|
||||
|
||||
foreach ($lines as $line) {
|
||||
// Skip empty lines
|
||||
if (trim($line) === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Calculate current line's indentation (number of leading spaces)
|
||||
$currentIndent = strlen($line) - strlen(ltrim($line));
|
||||
|
||||
// Check if this line starts an environment block
|
||||
if (preg_match('/^(\s*)environment\s*:\s*$/', $line, $matches)) {
|
||||
$inEnvironmentBlock = true;
|
||||
$environmentIndent = strlen($matches[1]);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if this line starts an environment block with inline content (rare but possible)
|
||||
if (preg_match('/^(\s*)environment\s*:\s*\{/', $line)) {
|
||||
// Inline object format - not supported for comment extraction
|
||||
continue;
|
||||
}
|
||||
|
||||
// If we're in an environment block, check if we've exited it
|
||||
if ($inEnvironmentBlock) {
|
||||
// If we hit a line with same or less indentation that's not empty, we've left the block
|
||||
// Unless it's a continuation of the environment block
|
||||
$trimmedLine = ltrim($line);
|
||||
|
||||
// Check if this is a new top-level key (same indent as 'environment:' or less)
|
||||
if ($currentIndent <= $environmentIndent && ! str_starts_with($trimmedLine, '-') && ! str_starts_with($trimmedLine, '#')) {
|
||||
// Check if it looks like a YAML key (contains : not inside quotes)
|
||||
if (preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*\s*:/', $trimmedLine)) {
|
||||
$inEnvironmentBlock = false;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Skip comment-only lines
|
||||
if (str_starts_with($trimmedLine, '#')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Try to extract environment variable and comment from this line
|
||||
$extracted = extractEnvVarCommentFromYamlLine($trimmedLine);
|
||||
if ($extracted !== null && $extracted['comment'] !== null) {
|
||||
$comments[$extracted['key']] = $extracted['comment'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract environment variable key and inline comment from a single YAML line.
|
||||
*
|
||||
* @param string $line A trimmed line from the environment section
|
||||
* @return array|null Array with 'key' and 'comment', or null if not an env var line
|
||||
*/
|
||||
function extractEnvVarCommentFromYamlLine(string $line): ?array
|
||||
{
|
||||
$key = null;
|
||||
$comment = null;
|
||||
|
||||
// Handle array format: `- KEY=value # comment` or `- KEY # comment`
|
||||
if (str_starts_with($line, '-')) {
|
||||
$content = ltrim(substr($line, 1));
|
||||
|
||||
// Check for KEY=value format
|
||||
if (preg_match('/^([A-Za-z_][A-Za-z0-9_]*)/', $content, $keyMatch)) {
|
||||
$key = $keyMatch[1];
|
||||
// Find comment - need to handle quoted values
|
||||
$comment = extractCommentAfterValue($content);
|
||||
}
|
||||
}
|
||||
// Handle map format: `KEY: "value" # comment` or `KEY: value # comment`
|
||||
elseif (preg_match('/^([A-Za-z_][A-Za-z0-9_]*)\s*:/', $line, $keyMatch)) {
|
||||
$key = $keyMatch[1];
|
||||
// Get everything after the key and colon
|
||||
$afterKey = substr($line, strlen($keyMatch[0]));
|
||||
$comment = extractCommentAfterValue($afterKey);
|
||||
}
|
||||
|
||||
if ($key === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'comment' => $comment,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract inline comment from a value portion of a YAML line.
|
||||
*
|
||||
* Handles quoted values (where # inside quotes is not a comment).
|
||||
*
|
||||
* @param string $valueAndComment The value portion (may include comment)
|
||||
* @return string|null The comment text, or null if no comment
|
||||
*/
|
||||
function extractCommentAfterValue(string $valueAndComment): ?string
|
||||
{
|
||||
$valueAndComment = ltrim($valueAndComment);
|
||||
|
||||
if ($valueAndComment === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$firstChar = $valueAndComment[0] ?? '';
|
||||
|
||||
// Handle case where value is empty and line starts directly with comment
|
||||
// e.g., `KEY: # comment` becomes `# comment` after ltrim
|
||||
if ($firstChar === '#') {
|
||||
$comment = trim(substr($valueAndComment, 1));
|
||||
|
||||
return $comment !== '' ? $comment : null;
|
||||
}
|
||||
|
||||
// Handle double-quoted value
|
||||
if ($firstChar === '"') {
|
||||
// Find closing quote (handle escaped quotes)
|
||||
$pos = 1;
|
||||
$len = strlen($valueAndComment);
|
||||
while ($pos < $len) {
|
||||
if ($valueAndComment[$pos] === '\\' && $pos + 1 < $len) {
|
||||
$pos += 2; // Skip escaped character
|
||||
|
||||
continue;
|
||||
}
|
||||
if ($valueAndComment[$pos] === '"') {
|
||||
// Found closing quote
|
||||
$remainder = substr($valueAndComment, $pos + 1);
|
||||
|
||||
return extractCommentFromRemainder($remainder);
|
||||
}
|
||||
$pos++;
|
||||
}
|
||||
|
||||
// No closing quote found
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle single-quoted value
|
||||
if ($firstChar === "'") {
|
||||
// Find closing quote (single quotes don't have escapes in YAML)
|
||||
$closingPos = strpos($valueAndComment, "'", 1);
|
||||
if ($closingPos !== false) {
|
||||
$remainder = substr($valueAndComment, $closingPos + 1);
|
||||
|
||||
return extractCommentFromRemainder($remainder);
|
||||
}
|
||||
|
||||
// No closing quote found
|
||||
return null;
|
||||
}
|
||||
|
||||
// Unquoted value - find # that's preceded by whitespace
|
||||
// Be careful not to match # at the start of a value like color codes
|
||||
if (preg_match('/\s+#\s*(.*)$/', $valueAndComment, $matches)) {
|
||||
$comment = trim($matches[1]);
|
||||
|
||||
return $comment !== '' ? $comment : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract comment from the remainder of a line after a quoted value.
|
||||
*
|
||||
* @param string $remainder Text after the closing quote
|
||||
* @return string|null The comment text, or null if no comment
|
||||
*/
|
||||
function extractCommentFromRemainder(string $remainder): ?string
|
||||
{
|
||||
// Look for # in remainder
|
||||
$hashPos = strpos($remainder, '#');
|
||||
if ($hashPos !== false) {
|
||||
$comment = trim(substr($remainder, $hashPos + 1));
|
||||
|
||||
return $comment !== '' ? $comment : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function data_get_str($data, $key, $default = null): Stringable
|
||||
{
|
||||
$str = data_get($data, $key, $default) ?? $default;
|
||||
@@ -1345,6 +1554,9 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
{
|
||||
if ($resource->getMorphClass() === \App\Models\Service::class) {
|
||||
if ($resource->docker_compose_raw) {
|
||||
// Extract inline comments from raw YAML before Symfony parser discards them
|
||||
$envComments = extractYamlEnvironmentComments($resource->docker_compose_raw);
|
||||
|
||||
try {
|
||||
$yaml = Yaml::parse($resource->docker_compose_raw);
|
||||
} catch (\Exception $e) {
|
||||
@@ -1376,7 +1588,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
}
|
||||
$topLevelVolumes = collect($tempTopLevelVolumes);
|
||||
}
|
||||
$services = collect($services)->map(function ($service, $serviceName) use ($topLevelVolumes, $topLevelNetworks, $definedNetwork, $isNew, $generatedServiceFQDNS, $resource, $allServices) {
|
||||
$services = collect($services)->map(function ($service, $serviceName) use ($topLevelVolumes, $topLevelNetworks, $definedNetwork, $isNew, $generatedServiceFQDNS, $resource, $allServices, $envComments) {
|
||||
// Workarounds for beta users.
|
||||
if ($serviceName === 'registry') {
|
||||
$tempServiceName = 'docker-registry';
|
||||
@@ -1722,6 +1934,8 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
$key = str($variableName);
|
||||
$value = str($variable);
|
||||
}
|
||||
// Preserve original key for comment lookup before $key might be reassigned
|
||||
$originalKey = $key->value();
|
||||
if ($key->startsWith('SERVICE_FQDN')) {
|
||||
if ($isNew || $savedService->fqdn === null) {
|
||||
$name = $key->after('SERVICE_FQDN_')->beforeLast('_')->lower();
|
||||
@@ -1775,6 +1989,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
'resourceable_type' => get_class($resource),
|
||||
'resourceable_id' => $resource->id,
|
||||
'is_preview' => false,
|
||||
'comment' => $envComments[$originalKey] ?? null,
|
||||
]);
|
||||
}
|
||||
// Caddy needs exact port in some cases.
|
||||
@@ -1854,6 +2069,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
'resourceable_type' => get_class($resource),
|
||||
'resourceable_id' => $resource->id,
|
||||
'is_preview' => false,
|
||||
'comment' => $envComments[$originalKey] ?? null,
|
||||
]);
|
||||
}
|
||||
if (! $isDatabase) {
|
||||
@@ -1892,6 +2108,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
'resourceable_type' => get_class($resource),
|
||||
'resourceable_id' => $resource->id,
|
||||
'is_preview' => false,
|
||||
'comment' => $envComments[$originalKey] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1930,6 +2147,7 @@ function parseDockerComposeFile(Service|Application $resource, bool $isNew = fal
|
||||
'resourceable_type' => get_class($resource),
|
||||
'resourceable_id' => $resource->id,
|
||||
'is_preview' => false,
|
||||
'comment' => $envComments[$originalKey] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user