Use named arguments to pass files to template
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
2
php-template/.vscode/launch.json
vendored
2
php-template/.vscode/launch.json
vendored
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
$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];
|
||||
}
|
||||
$candidates[] = $candidate;
|
||||
$candidateData[] = $candidate;
|
||||
}
|
||||
fclose($handle);
|
||||
} else {
|
||||
error_log('Error opening CSV');
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user