netplan_yaml

This commit is contained in:
devdatt 2025-12-23 01:20:10 +05:30
parent 4da9a23b53
commit b5cbb3f56d
1 changed files with 10 additions and 4 deletions

View File

@ -164,15 +164,22 @@ function build_interface(array $d, string $key): array
function netplan_yaml(array $data, int $indent = 0): string function netplan_yaml(array $data, int $indent = 0): string
{ {
$yaml = ''; $yaml = '';
$pad = str_repeat(' ', $indent); $pad = str_repeat(' ', $indent);
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
// List (numeric keys) if ($value instanceof stdClass) {
$yaml .= "{$pad}{$key}: {}\n";
continue;
}
if (is_array($value) && array_keys($value) === range(0, count($value) - 1)) { if (is_array($value) && array_keys($value) === range(0, count($value) - 1)) {
foreach ($value as $item) { foreach ($value as $item) {
if (is_array($item)) { if ($item instanceof stdClass) {
$yaml .= "{$pad}- {}\n";
} elseif (is_array($item)) {
$yaml .= "{$pad}-\n"; $yaml .= "{$pad}-\n";
$yaml .= netplan_yaml($item, $indent + 1); $yaml .= netplan_yaml($item, $indent + 1);
} else { } else {
@ -182,14 +189,12 @@ function netplan_yaml(array $data, int $indent = 0): string
continue; continue;
} }
// Mapping
if (is_array($value)) { if (is_array($value)) {
$yaml .= "{$pad}{$key}:\n"; $yaml .= "{$pad}{$key}:\n";
$yaml .= netplan_yaml($value, $indent + 1); $yaml .= netplan_yaml($value, $indent + 1);
continue; continue;
} }
// Scalar
if (is_bool($value)) { if (is_bool($value)) {
$value = $value ? 'true' : 'false'; $value = $value ? 'true' : 'false';
} }
@ -200,6 +205,7 @@ function netplan_yaml(array $data, int $indent = 0): string
return $yaml; return $yaml;
} }
function update_service($which_service) function update_service($which_service)
{ {