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', netplan_yaml($netplan));
file_put_contents('/var/www/50-cloud-init.yaml', $yaml);
} }
} }

View File

@ -133,21 +133,47 @@ function build_interface(array $cfg, string $type): array
return $out; return $out;
} }
function yaml(array $data, int $indent = 0): string function netplan_yaml(array $data, int $indent = 0): string
{ {
$out = ''; $out = '';
$pad = str_repeat(' ', $indent); $pad = str_repeat(' ', $indent);
foreach ($data as $k => $v) { foreach ($data as $key => $value) {
if ($v instanceof stdClass) {
$out .= "{$pad}{$k}: {}\n"; /* Empty map */
} elseif (is_array($v)) { if ($value instanceof stdClass) {
$out .= "{$pad}{$k}:\n"; $out .= "{$pad}{$key}: {}\n";
$out .= yaml($v, $indent + 1); continue;
} else {
$out .= "{$pad}{$k}: {$v}\n";
} }
/* 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; return $out;
} }