refactor: Improve parsing of commands for sudo in parseCommandsByLineForSudo

This commit is contained in:
Andras Bacsai
2024-10-02 18:26:40 +02:00
parent a1a8f1336a
commit 7bb1bf0ae3

View File

@@ -1184,14 +1184,16 @@ function check_domain_usage(ServiceApplication|Application|null $resource = null
function parseCommandsByLineForSudo(Collection $commands, Server $server): array function parseCommandsByLineForSudo(Collection $commands, Server $server): array
{ {
$commands = $commands->map(function ($line) { $commands = $commands->map(function ($line) {
if (! str(trim($line))->startsWith([ if (
'cd', ! str(trim($line))->startsWith([
'command', 'cd',
'echo', 'command',
'true', 'echo',
'if', 'true',
'fi', 'if',
])) { 'fi',
])
) {
return "sudo $line"; return "sudo $line";
} }
@@ -3863,14 +3865,19 @@ function convertComposeEnvironmentToArray($environment)
{ {
$convertedServiceVariables = collect([]); $convertedServiceVariables = collect([]);
if (isAssociativeArray($environment)) { if (isAssociativeArray($environment)) {
// Example: $environment = ['FOO' => 'bar', 'BAZ' => 'qux'];
if ($environment instanceof Collection) { if ($environment instanceof Collection) {
$changedEnvironment = collect([]); $changedEnvironment = collect([]);
$environment->each(function ($value, $key) use ($changedEnvironment) { $environment->each(function ($value, $key) use ($changedEnvironment) {
$parts = explode('=', $value, 2); if (is_numeric($key)) {
if (count($parts) === 2) { $parts = explode('=', $value, 2);
$key = $parts[0]; if (count($parts) === 2) {
$realValue = $parts[1] ?? ''; $key = $parts[0];
$changedEnvironment->put($key, $realValue); $realValue = $parts[1] ?? '';
$changedEnvironment->put($key, $realValue);
} else {
$changedEnvironment->put($key, $value);
}
} else { } else {
$changedEnvironment->put($key, $value); $changedEnvironment->put($key, $value);
} }
@@ -3880,6 +3887,7 @@ function convertComposeEnvironmentToArray($environment)
} }
$convertedServiceVariables = $environment; $convertedServiceVariables = $environment;
} else { } else {
// Example: $environment = ['FOO=bar', 'BAZ=qux'];
foreach ($environment as $value) { foreach ($environment as $value) {
$parts = explode('=', $value, 2); $parts = explode('=', $value, 2);
$key = $parts[0]; $key = $parts[0];