diff options
| author | 2024-02-28 12:23:28 +0000 | |
|---|---|---|
| committer | 2024-02-28 13:23:28 +0100 | |
| commit | 4b29e666b06762b4b36438c9370c38bc43121f78 (patch) | |
| tree | 2b92dcbc5879aa7de8eeec81ccde208f572b3cf0 /cli/reconfigure.php | |
| parent | 5de794ee0fbbce2fdf0af3787b9b89299be8698e (diff) | |
Command Line Parser Concept (#6099)
* Adds logic for validation
* Adds validation to do-install
* Adds help to do-install
* Adds validation & help to reconfigure
* Adds validation to check.translation
* Adds validation to manipulate.translation
* Small fixes to help texts
* Refactors language option validation
* Adds default options to validation
* Fixes validation with regex
* Refactors readAs functions
* Updates to new regex validation format
* Fixes typing around default values
* Adds file extension validation
* Restandardises validation & parsing typing around array of strings
* Adds NotOneOf validation
* Adds ArrayOfString read as
* Refactors existing validation
* Adds validation throughout cli
* Removes unused file
* Adds new CL parser with goal of wrapping CLI behaviour
* Hides parsing and validation
* Rewites CL parser to make better use of classes
* Rolls out new parser across CL
* Fixes error during unknown option check
* Fixes misnamed property calls
* Seperates validations into more appropriate locations
* Adds common boolean forms to validation
* Moves CommandLineParser and Option classes into their own files
* Fixes error when validating Int type
* Rewrites appendTypedValues -> appendTypedValidValues now filters invalid values from output
* Renames -> for clarity
* Adds some docs clarifying option defaults and value taking behaviour
* Refactors getUsageMessage for readability
* Minor formatting changes
* Adds tests for CommandLineParser
* Adds more tests
* Adds minor fixs
* Reconfigure now correctly updates config
* More fixes to reconfigure
* Fixes required files for CommandLineParserTest
* Use .php extension for PHP file
* PHPStan ignore instead of wrong typing
* Refactors to support php 7.4
* Moves away from dynamic properties by adding 'Definintions' to all commands
* Renames target to definition for clarity
* Stops null from being returned as a valid value in a certain edge case
* Adds PHPStan ignore instead of incorrect typing
* Refactors tests to take account of new typing solution
* Marks file as executable
* Draft CLI rework
* Finish rewrite as object-oriented
* Fix PHPStan ignore and make more strongly typed
* Rename class Option to CliOption
* Light renaming + anonymous classes
---------
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'cli/reconfigure.php')
| -rwxr-xr-x | cli/reconfigure.php | 207 |
1 files changed, 97 insertions, 110 deletions
diff --git a/cli/reconfigure.php b/cli/reconfigure.php index fde16c921..853e05297 100755 --- a/cli/reconfigure.php +++ b/cli/reconfigure.php @@ -3,133 +3,120 @@ declare(strict_types=1); require(__DIR__ . '/_cli.php'); -$parameters = [ - 'long' => [ - 'environment' => ':', - 'base-url' => ':', - 'language' => ':', - 'title' => ':', - 'default-user' => ':', - 'allow-anonymous' => '', - 'allow-anonymous-refresh' => '', - 'auth-type' => ':', - 'api-enabled' => '', - 'allow-robots' => '', - 'disable-update' => '', - 'db-type' => ':', - 'db-host' => ':', - 'db-user' => ':', - 'db-password' => ':', - 'db-base' => ':', - 'db-prefix' => '::', - ], - 'short' => [], - 'deprecated' => [ - 'base-url' => 'base_url', - 'default-user' => 'default_user', - 'allow-anonymous' => 'allow_anonymous', - 'allow-anonymous-refresh' => 'allow_anonymous_refresh', - 'auth-type' => 'auth_type', - 'api-enabled' => 'api_enabled', - 'allow-robots' => 'allow_robots', - 'disable-update' => 'disable_update', - ], -]; - -$configParams = [ - 'environment', - 'base-url', - 'language', - 'title', - 'default-user', - 'allow-anonymous', - 'allow-anonymous-refresh', - 'auth-type', - 'api-enabled', - 'allow-robots', - 'disable-update', -]; +$cliOptions = new class extends CliOptionsParser { + public string $defaultUser; + public string $environment; + public string $baseUrl; + public string $language; + public string $title; + public bool $allowAnonymous; + public bool $allowAnonymousRefresh; + public string $authType; + public bool $apiEnabled; + public bool $allowRobots; + public bool $disableUpdate; + public string $dbType; + public string $dbHost; + public string $dbUser; + public string $dbPassword; + public string $dbBase; + public string $dbPrefix; -$dBconfigParams = [ - 'db-type' => 'type', - 'db-host' => 'host', - 'db-user' => 'user', - 'db-password' => 'password', - 'db-base' => 'base', - 'db-prefix' => 'prefix', -]; - -$options = parseCliParams($parameters); + public function __construct() { + $this->addOption('defaultUser', (new CliOption('default-user'))->deprecatedAs('default_user')); + $this->addOption('environment', (new CliOption('environment'))); + $this->addOption('baseUrl', (new CliOption('base-url'))->deprecatedAs('base_url')); + $this->addOption('language', (new CliOption('language'))); + $this->addOption('title', (new CliOption('title'))); + $this->addOption( + 'allowAnonymous', + (new CliOption('allow-anonymous'))->withValueOptional('true')->deprecatedAs('allow_anonymous')->typeOfBool() + ); + $this->addOption( + 'allowAnonymousRefresh', + (new CliOption('allow-anonymous-refresh'))->withValueOptional('true')->deprecatedAs('allow_anonymous_refresh')->typeOfBool() + ); + $this->addOption('authType', (new CliOption('auth-type'))->deprecatedAs('auth_type')); + $this->addOption( + 'apiEnabled', + (new CliOption('api-enabled'))->withValueOptional('true')->deprecatedAs('api_enabled')->typeOfBool() + ); + $this->addOption( + 'allowRobots', + (new CliOption('allow-robots'))->withValueOptional('true')->deprecatedAs('allow_robots')->typeOfBool() + ); + $this->addOption( + 'disableUpdate', + (new CliOption('disable-update'))->withValueOptional('true')->deprecatedAs('disable_update')->typeOfBool() + ); + $this->addOption('dbType', (new CliOption('db-type'))); + $this->addOption('dbHost', (new CliOption('db-host'))); + $this->addOption('dbUser', (new CliOption('db-user'))); + $this->addOption('dbPassword', (new CliOption('db-password'))); + $this->addOption('dbBase', (new CliOption('db-base'))); + $this->addOption('dbPrefix', (new CliOption('db-prefix'))->withValueOptional()); + parent::__construct(); + } +}; -if (!empty($options['invalid'])) { - fail('Usage: ' . basename(__FILE__) . " --default-user admin ( --auth-type form" . - " --environment production --base-url https://rss.example.net --allow-robots" . - " --language en --title FreshRSS --allow-anonymous --allow-anonymous-refresh --api-enabled" . - " --db-type mysql --db-host localhost:3306 --db-user freshrss --db-password dbPassword123" . - " --db-base freshrss --db-prefix freshrss_ --disable-update )"); +if (!empty($cliOptions->errors)) { + fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage); } fwrite(STDERR, 'Reconfiguring FreshRSS…' . "\n"); -foreach ($configParams as $param) { - if (isset($options['valid'][$param])) { - switch ($param) { - case 'allow-anonymous-refresh': - FreshRSS_Context::systemConf()->allow_anonymous_refresh = true; - break; - case 'allow-anonymous': - FreshRSS_Context::systemConf()->allow_anonymous = true; - break; - case 'allow-robots': - FreshRSS_Context::systemConf()->allow_robots = true; - break; - case 'api-enabled': - FreshRSS_Context::systemConf()->api_enabled = true; - break; - case 'auth-type': - if (in_array($options['valid'][$param], ['form', 'http_auth', 'none'], true)) { - FreshRSS_Context::systemConf()->auth_type = $options['valid'][$param]; - } else { - fail('FreshRSS invalid authentication method! auth_type must be one of { form, http_auth, none }'); - } - break; - case 'base-url': - FreshRSS_Context::systemConf()->base_url = (string) $options['valid'][$param]; - break; - case 'default-user': - if (FreshRSS_user_Controller::checkUsername((string) $options['valid'][$param])) { - FreshRSS_Context::systemConf()->default_user = (string) $options['valid'][$param]; - } else { +$values = [ + 'default_user' => $cliOptions->defaultUser ?? null, + 'environment' => $cliOptions->environment ?? null, + 'base_url' => $cliOptions->baseUrl ?? null, + 'language' => $cliOptions->language ?? null, + 'title' => $cliOptions->title ?? null, + 'allow_anonymous' => $cliOptions->allowAnonymous ?? null, + 'allow_anonymous_refresh' => $cliOptions->allowAnonymousRefresh ?? null, + 'auth_type' => $cliOptions->authType ?? null, + 'api_enabled' => $cliOptions->apiEnabled ?? null, + 'allow_robots' => $cliOptions->allowRobots ?? null, + 'disable_update' => $cliOptions->disableUpdate ?? null, +]; + +$dbValues = [ + 'type' => $cliOptions->dbType ?? null, + 'host' => $cliOptions->dbHost ?? null, + 'user' => $cliOptions->dbUser ?? null, + 'password' => $cliOptions->dbPassword ?? null, + 'base' => $cliOptions->dbBase ?? null, + 'prefix' => $cliOptions->dbPrefix ?? null, +]; + +$systemConf = FreshRSS_Context::systemConf(); +foreach ($values as $name => $value) { + if ($value !== null) { + switch ($name) { + case 'default_user': + if (!FreshRSS_user_Controller::checkUsername($value)) { fail('FreshRSS invalid default username! default_user must be ASCII alphanumeric'); } break; - case 'disable-update': - FreshRSS_Context::systemConf()->disable_update = true; - break; case 'environment': - if (in_array($options['valid'][$param], ['development', 'production', 'silent'], true)) { - FreshRSS_Context::systemConf()->environment = $options['valid'][$param]; - } else { + if (!in_array($value, ['development', 'production', 'silent'], true)) { fail('FreshRSS invalid environment! environment must be one of { development, production, silent }'); } break; - case 'language': - FreshRSS_Context::systemConf()->language = (string) $options['valid'][$param]; - break; - case 'title': - FreshRSS_Context::systemConf()->title = (string) $options['valid'][$param]; + case 'auth_type': + if (!in_array($value, ['form', 'http_auth', 'none'], true)) { + fail('FreshRSS invalid authentication method! auth_type must be one of { form, http_auth, none }'); + } break; } + // @phpstan-ignore-next-line (change to `@phpstan-ignore property.dynamicName` when upgrading to PHPStan 1.11+) + $systemConf->$name = $value; } } -$db = FreshRSS_Context::systemConf()->db; -foreach ($dBconfigParams as $dBparam => $configDbParam) { - if (isset($options['valid'][$dBparam])) { - $db[$configDbParam] = $options['valid'][$dBparam]; - } -} -/** @var array{'type':string,'host':string,'user':string,'password':string,'base':string,'prefix':string, - * 'connection_uri_params':string,'pdo_options':array<int,int|string|bool>} $db */ + +$db = array_merge(FreshRSS_Context::systemConf()->db, array_filter($dbValues)); + +performRequirementCheck($db['type']); + FreshRSS_Context::systemConf()->db = $db; FreshRSS_Context::systemConf()->save(); |
