2022-01-18 19:14:24 +01:00
|
|
|
#!/usr/bin/keyhelp-php81
|
|
|
|
<?php
|
2022-01-19 21:08:44 +01:00
|
|
|
if (php_sapi_name() !== 'cli') {
|
|
|
|
exit;
|
2022-01-18 19:14:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 13:48:15 +01:00
|
|
|
// version, store that somewhere else
|
|
|
|
$version = '0.0.1';
|
|
|
|
|
|
|
|
|
2022-01-22 17:08:05 +01:00
|
|
|
require dirname(path: __DIR__) . '/vendor/autoload.php';
|
2022-01-18 19:14:24 +01:00
|
|
|
|
2022-01-19 21:08:44 +01:00
|
|
|
use App\Controller\BindAPI;
|
|
|
|
|
2022-01-22 17:08:05 +01:00
|
|
|
$configFile = dirname(path: __DIR__) ."/config.json";
|
2022-01-23 15:21:41 +01:00
|
|
|
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);
|
|
|
|
}
|
2022-01-22 16:40:03 +01:00
|
|
|
$configJSON = file_get_contents($configFile);
|
2022-01-23 15:21:41 +01:00
|
|
|
|
|
|
|
if (!$config = json_decode($configJSON, associative: true)) {
|
|
|
|
echo 'Error parsing the config file.' . PHP_EOL;
|
|
|
|
echo $configJSON;
|
|
|
|
}
|
2022-01-19 21:08:44 +01:00
|
|
|
|
2022-01-23 13:48:15 +01:00
|
|
|
$shortOpts = 'v::'; // version
|
|
|
|
$shortOpts .= "V::"; // verbose
|
|
|
|
$shortOpts .= "h::"; // help
|
|
|
|
|
|
|
|
$longOpts = [
|
|
|
|
'version::',
|
|
|
|
'verbose::',
|
|
|
|
'help::'
|
|
|
|
];
|
|
|
|
|
|
|
|
$options = getopt($shortOpts, $longOpts, rest_index: $restIndex);
|
|
|
|
|
|
|
|
if (array_key_exists('v', $options) || array_key_exists('version', $options) ) {
|
|
|
|
print("bindAPI version: $version" . PHP_EOL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('h', $options) || array_key_exists('help', $options) ) {
|
|
|
|
print("Help …" . PHP_EOL);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('V', $options) || array_key_exists('verbose', $options) ) {
|
|
|
|
$config['verbose'] = true;
|
|
|
|
} else {
|
|
|
|
$config['verbose'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arguments = array_slice($argv, $restIndex);
|
|
|
|
|
|
|
|
$app = new BindAPI(config: $config, argumentsCount: count($arguments), arguments: $arguments);
|
2022-01-19 21:08:44 +01:00
|
|
|
$app->runCommand();
|
2022-01-18 19:14:24 +01:00
|
|
|
|
|
|
|
|
2022-01-23 15:21:41 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|