added check for config file

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-23 15:21:41 +01:00
parent 1dfc68b974
commit 115a42baa3
1 changed files with 60 additions and 1 deletions

View File

@ -13,8 +13,23 @@ require dirname(path: __DIR__) . '/vendor/autoload.php';
use App\Controller\BindAPI;
$configFile = dirname(path: __DIR__) ."/config.json";
if (!file_exists($configFile)) {
echo 'Missing config file' . PHP_EOL;
if (confirm(message: 'Should I create a new config based on config.json.sample?')) {
copy(from: 'config.json.sample', to: 'config.json');
echo 'Config file has been generated. Adjust it to your needs, then proceed to database setup.' . PHP_EOL;
} else {
echo 'You first have to setup the bindAPI. Bye.' . PHP_EOL;
exit(0);
}
exit(1);
}
$configJSON = file_get_contents($configFile);
$config = json_decode($configJSON, associative: true);
if (!$config = json_decode($configJSON, associative: true)) {
echo 'Error parsing the config file.' . PHP_EOL;
echo $configJSON;
}
$shortOpts = 'v::'; // version
$shortOpts .= "V::"; // verbose
@ -50,3 +65,47 @@ $app = new BindAPI(config: $config, argumentsCount: count($arguments), arguments
$app->runCommand();
/**
* @param String $message
* @param array $options
* @param string $default
*
* @return bool
*/
function confirm(String $message = 'Are you sure? ', array $options = ['y', 'n'], string $default ='n'): bool
{
// first $options means true, any other false
echo $message, ' (';
$first = true;
foreach ($options as $option) {
// mark default
if ($option == $default) {
$option = strtoupper($option);
}
if ($first) {
echo $option;
$first = false;
} else {
echo '/', $option;
}
}
echo '): ';
$handle = fopen("php://stdin", 'r');
$line = trim(fgetc($handle));
fclose($handle);
if ($line == '') {
// enter
$line = $default;
}
if ($line == $options[0]) {
$result = true;
} else {
$result = false;
}
return $result;
}