Use named arguments to pass files to template

This commit is contained in:
2024-08-04 22:08:11 +10:00
parent 89a5224af2
commit 09ffd40bf7
3 changed files with 30 additions and 27 deletions

View File

@@ -21,7 +21,7 @@ function create_or_update_page() {
slug=$(echo "$council_block" | jq -r '.slug')
content=$(echo "$council_block" | jq -c | php php-template/main.php "php://stdin" "candidates/$slug.csv")
content=$(echo "$council_block" | jq -c | php php-template/main.php --council-file "php://stdin" --candidates-file "candidates/$slug.csv")
if [ $? -eq 0 ]; then

View File

@@ -6,7 +6,7 @@
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/main.php",
"args": ["${workspaceFolder}/example-config.json", "${workspaceFolder}/example-candidates.csv"],
"args": ["--council-file", "${workspaceFolder}/example-config.json", "--candidates-file", "${workspaceFolder}/example-candidates.csv"],
"cwd": "${workspaceFolder}",
"port": 9000
}

View File

@@ -1,46 +1,49 @@
<?php
require_once "page_renderer.php";
// TODO: Need to handle arguments better
// Assume that the first argument is a path to a file contain the config.
// If the argument isn't present, load the json from standard input
if (isset($argv[1])) {
$jsonData = file_get_contents($argv[1]);
$options = getopt("", ["council-file:", "candidates-file:"]);
if (isset($options['council-file'])) {
$councilFileContents = file_get_contents($options['council-file']);
} else {
$jsonData = file_get_contents("php://stdin");
error_log("Error: Missing required option '--council-file'.");
exit(1);
}
$config = json_decode($jsonData, true);
$councilData = json_decode($councilFileContents, true);
// Check for decoding errors
if (json_last_error() !== JSON_ERROR_NONE) {
error_log('Error decoding JSON: ' . json_last_error_msg());
error_log('Error decoding council file: ' . json_last_error_msg());
exit(1);
}
if (isset($options['candidates-file'])) {
$candidatesFile = $options['candidates-file'];
} else {
error_log("Error: Missing required option '--candidates-file'.");
exit(1);
}
// Convert CSV into an array of dictionaries. Use the header as the key in the dictionary.
$candidates = [];
if (isset($argv[2])) {
if (($handle = fopen($argv[2], "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = [];
foreach ($headers as $key => $value) {
$candidate[$value] = $data[$key];
}
$candidates[] = $candidate;
$candidateData = [];
if (($handle = fopen($candidatesFile, "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = [];
foreach ($headers as $key => $value) {
$candidate[$value] = $data[$key];
}
fclose($handle);
} else {
error_log('Error opening CSV');
exit(1);
$candidateData[] = $candidate;
}
fclose($handle);
} else {
error_log('Error opening candidates file');
exit(1);
}
// TODO: Check that candidates were read successfully??
$renderer = new SPLPageRenderer();
$pageContent = $renderer->renderCouncilPage($config, $candidates);
$pageContent = $renderer->renderCouncilPage($councilData, $candidateData);
if ($pageContent === null) {
exit(2);
}