netplan_yaml

This commit is contained in:
devdatt 2025-12-23 12:09:34 +05:30
parent e1b72df58d
commit c94d2cfffa
2 changed files with 36 additions and 11 deletions

View File

@ -167,8 +167,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
}
$yaml = yaml($netplan);
file_put_contents('/var/www/50-cloud-init.yaml', $yaml);
file_put_contents('/var/www/50-cloud-init.yaml', netplan_yaml($netplan));
}
}

View File

@ -133,21 +133,47 @@ function build_interface(array $cfg, string $type): array
return $out;
}
function yaml(array $data, int $indent = 0): string
function netplan_yaml(array $data, int $indent = 0): string
{
$out = '';
$pad = str_repeat(' ', $indent);
foreach ($data as $k => $v) {
if ($v instanceof stdClass) {
$out .= "{$pad}{$k}: {}\n";
} elseif (is_array($v)) {
$out .= "{$pad}{$k}:\n";
$out .= yaml($v, $indent + 1);
} else {
$out .= "{$pad}{$k}: {$v}\n";
foreach ($data as $key => $value) {
/* Empty map */
if ($value instanceof stdClass) {
$out .= "{$pad}{$key}: {}\n";
continue;
}
/* Boolean */
if (is_bool($value)) {
$out .= "{$pad}{$key}: " . ($value ? 'true' : 'false') . "\n";
continue;
}
/* Scalar */
if (!is_array($value)) {
$out .= "{$pad}{$key}: {$value}\n";
continue;
}
/* Numeric array (list) */
if (array_keys($value) === range(0, count($value) - 1)) {
$out .= "{$pad}{$key}:\n";
foreach ($value as $item) {
$out .= is_bool($item)
? "{$pad} - " . ($item ? 'true' : 'false') . "\n"
: "{$pad} - {$item}\n";
}
continue;
}
/* Mapping */
$out .= "{$pad}{$key}:\n";
$out .= netplan_yaml($value, $indent + 1);
}
return $out;
}