Task: #53687 Replace --site/--directory-name with --siteDir and add JSON batch input

- Changed site targeting from --site, --directory-name, --directoryName to --siteDir
- Added write -i <json> for multi-setting input
- Added support for inline JSON string values in write
- Updated help/examples to generic placeholders
- Extended tests for new arguments and validations (all passing)
This commit is contained in:
Alejandro Sosa 2026-03-26 11:28:58 +01:00
commit 227de9ac07
3 changed files with 324 additions and 268 deletions

222
bin/cfg
View file

@ -22,51 +22,50 @@ foreach ($autoloadFiles as $autoloadFile) {
}
}
// Pre-parse site options so they can be passed after positional args.
function readJsonInputFile(string $path): array
{
if (!is_readable($path)) {
throw new \RuntimeException("Input file is not readable: $path");
}
$content = file_get_contents($path);
if ($content === false) {
throw new \RuntimeException("Can not read input file: $path");
}
try {
$data = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $ex) {
throw new \RuntimeException(sprintf(
'Input JSON is invalid (%s): %s',
(string)$ex->getCode(),
$ex->getMessage()
));
}
if (!is_array($data)) {
throw new \RuntimeException('Input JSON must decode to an object/array');
}
return $data;
}
// Pre-parse siteDir so it can be passed after positional args.
// The input helper library does not handle this case reliably.
$preparsedSiteOptions = [];
$preparsedSiteDir = null;
$rawArgv = $_SERVER['argv'] ?? $GLOBALS['argv'] ?? null;
if (is_array($rawArgv) && !empty($rawArgv)) {
$filteredArgv = [$rawArgv[0]];
for ($idx = 1; $idx < count($rawArgv); $idx++) {
$arg = $rawArgv[$idx];
if ($arg === '-s' || $arg === '--site') {
$preparsedSiteOptions[] = [
'name' => 'site',
'value' => $rawArgv[$idx + 1] ?? '',
];
if ($arg === '--siteDir') {
$preparsedSiteDir = $rawArgv[$idx + 1] ?? '';
if (isset($rawArgv[$idx + 1])) $idx++;
continue;
}
if (str_starts_with($arg, '--site=')) {
$preparsedSiteOptions[] = [
'name' => 'site',
'value' => substr($arg, strlen('--site=')),
];
continue;
}
if ($arg === '--directory-name' || $arg === '--directoryName') {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => $rawArgv[$idx + 1] ?? '',
];
if (isset($rawArgv[$idx + 1])) $idx++;
continue;
}
if (str_starts_with($arg, '--directory-name=')) {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => substr($arg, strlen('--directory-name=')),
];
continue;
}
if (str_starts_with($arg, '--directoryName=')) {
$preparsedSiteOptions[] = [
'name' => 'directoryName',
'value' => substr($arg, strlen('--directoryName=')),
];
if (str_starts_with($arg, '--siteDir=')) {
$preparsedSiteDir = substr($arg, strlen('--siteDir='));
continue;
}
@ -92,12 +91,7 @@ $collection = (new Input\InputCollection())
->add( Input\InputTypeFactory::build('LongOption')->name('in')->short('i') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Path to a json data file to to read')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('out')->short('o') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Path to a json file to to write to')
->description('Path to a JSON data file to read (for write)')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('mode')->short('m') // {{{
@ -115,14 +109,9 @@ $collection = (new Input\InputCollection())
->description('Path where the config/ directory of the package conf files is located, defaults to the working dir')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('site')->short('s') // {{{
->add( Input\InputTypeFactory::build('LongOption')->name('siteDir') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Site/instance directory below config/ (target: config/<site>/<prefix>.conf.php), e.g. owner_xyz')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('directoryName') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Alias for --site. Accepts owner_xyz or config/owner_xyz (also supports --directory-name)')
->description('Site/instance directory below config/. Accepts owner_xyz or config/owner_xyz')
) // }}}
->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{
@ -172,13 +161,17 @@ $collection = (new Input\InputCollection())
{
$setting = $context->find('setting');
$action = $context->find('action');
if ($setting === null || $setting === '') {
return ['key' => '', 'value' => null];
}
$setting = explode('=', $setting);
$settings['key'] = $setting[0];
if ($action === 'write')
{
$value = $setting[1];
if (! (isset($value) || $context->find('data'))) {
$value = $setting[1] ?? null;
if (! (isset($value) || $context->find('in'))) {
throw new \Exception('You need a value to write');
}
$specialValues = [ 'true', 'false', 'null' ];
@ -204,21 +197,29 @@ $collection = (new Input\InputCollection())
$usage = Cli\manpage( basename(__FILE__), $version,
'read write settings',
$collection, Colour::FG_GREEN, Colour::FG_WHITE,
[
'Examples' =>
'cfg write myCEESV \'auth:projectId="218523"\''
[
'Examples' =>
'cfg write extension_name \'module:enabled=true\''
.PHP_EOL
.'# writes local config: config/myCEESV.conf.php'
.'# writes local config: config/extension_name.conf.php'
.PHP_EOL
.'cfg write myCEESV \'auth:projectId="218523"\' --directory-name=owner_xyz'
.'cfg write extension_name -i /tmp/extension.json --siteDir=owner_xyz'
.PHP_EOL
.'# writes instance config: config/owner_xyz/myCEESV.conf.php'
.'# writes multiple settings from JSON to config/owner_xyz/extension_name.conf.php'
.PHP_EOL
.'cfg show myCEESV auth:projectId --site=owner_xyz'
.'cfg write extension_name \'feature_example:enabled=true\' --siteDir=owner_xyz'
.PHP_EOL
.'# reads merged config including config/owner_xyz/myCEESV.conf.php'
.'# writes instance config: config/owner_xyz/extension_name.conf.php'
.PHP_EOL
]
.'cfg write extension_name \'feature_example={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz'
.PHP_EOL
.'# writes multiple keys from one JSON string'
.PHP_EOL
.'cfg show extension_name module:enabled --siteDir=owner_xyz'
.PHP_EOL
.'# reads merged config including config/owner_xyz/extension_name.conf.php'
.PHP_EOL
]
).PHP_EOL;
// Get the supplied input. Passing the collection will make the handler bind values
@ -275,56 +276,38 @@ if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/');
$site = null;
$siteFlag = 0x01;
// Only one site selector may be provided to avoid ambiguous targets.
$providedSiteOptions = [];
foreach ($preparsedSiteOptions as $option) {
$providedSiteOptions[$option['name']][] = $option['value'] ?? '';
}
foreach (['site', 'directoryName'] as $optionName) {
$parsedValue = $argv->find($optionName);
if ($parsedValue !== null && $parsedValue !== '' && $parsedValue !== false) {
$providedSiteOptions[$optionName][] = $parsedValue;
$siteInput = ($preparsedSiteDir !== null) ? $preparsedSiteDir : $argv->find('siteDir');
if ($siteInput !== null && $siteInput !== false) {
$siteInput = trim((string)$siteInput);
// Accept both "owner_xyz" and "config/owner_xyz" and normalize to site key.
if (str_starts_with($siteInput, $appPath.'config/')) {
$siteInput = substr($siteInput, strlen($appPath.'config/'));
} elseif (str_starts_with($siteInput, 'config/')) {
$siteInput = substr($siteInput, strlen('config/'));
}
}
if (count($providedSiteOptions) > 1) {
fwrite(STDERR, 'Please use only one of --site or --directoryName (alias: --directory-name).'.PHP_EOL);
exit(1);
}
if (!empty($providedSiteOptions)) {
$optionName = array_key_first($providedSiteOptions);
$siteInput = trim((string)end($providedSiteOptions[$optionName]));
$siteInput = trim($siteInput, '/');
if ($siteInput === '') {
fwrite(STDERR, 'Option --siteDir is empty.'.PHP_EOL);
exit(1);
}
// Block directory traversal and hidden-dot segments.
if (str_contains($siteInput, '..') || preg_match('~(^|/)\.(?:/|$)~', $siteInput)) {
fwrite(STDERR, "Invalid directory in --siteDir: '$siteInput'.".PHP_EOL);
exit(1);
}
// Allow only predictable path segments for instance directories.
foreach (explode('/', $siteInput) as $part) {
if ($part === '' || !preg_match('/^[A-Za-z0-9._-]+$/', $part)) {
fwrite(STDERR, "Invalid directory name segment '$part' in --siteDir.".PHP_EOL);
exit(1);
}
}
if ($optionName === 'directoryName') {
// Accept both "owner_xyz" and "config/owner_xyz" and normalize to site key.
if (str_starts_with($siteInput, $appPath.'config/')) {
$siteInput = substr($siteInput, strlen($appPath.'config/'));
} elseif (str_starts_with($siteInput, 'config/')) {
$siteInput = substr($siteInput, strlen('config/'));
}
}
$siteInput = trim($siteInput, '/');
if ($siteInput === '') {
fwrite(STDERR, "Option --$optionName is empty.".PHP_EOL);
exit(1);
}
// Block directory traversal and hidden-dot segments.
if (str_contains($siteInput, '..') || preg_match('~(^|/)\.(?:/|$)~', $siteInput)) {
fwrite(STDERR, "Invalid directory in --$optionName: '$siteInput'.".PHP_EOL);
exit(1);
}
// Allow only predictable path segments for instance directories.
foreach (explode('/', $siteInput) as $part) {
if ($part === '' || !preg_match('/^[A-Za-z0-9._-]+$/', $part)) {
fwrite(STDERR, "Invalid directory name segment '$part' in --$optionName.".PHP_EOL);
exit(1);
}
}
$site = $siteInput;
// Reuse util-settings site resolution: config/<site>/<prefix>.conf.php
$cfg->site($site);
$site = $siteInput;
// Reuse util-settings site resolution: config/<site>/<prefix>.conf.php
$cfg->site($site);
}
try {
@ -368,9 +351,30 @@ case 'show':
break;
case 'write':
$path = ($settings['key'] !== '') ? explode(':', $settings['key']) : [];
$inputFile = $argv->find('in');
// Write needs exactly one payload source:
// either SETTING (key=value) or --in (JSON file).
// This avoids ambiguous input precedence and empty writes.
if ($inputFile && $settings['key'] !== '') {
fwrite(STDERR, 'Please use either SETTING or --in, not both.'.PHP_EOL);
exit(1);
}
if (!$inputFile && $settings['key'] === '') {
fwrite(STDERR, 'Nothing to write: provide SETTING or --in.'.PHP_EOL);
exit(1);
}
$setting2write = $settings['value'];
$path = ($settings['key'] !== '') ? explode(':', $settings['key']) : [];
if ($inputFile) {
try {
$setting2write = readJsonInputFile($inputFile);
} catch (\Throwable $e) {
fwrite(STDERR, $e->getMessage().PHP_EOL);
exit(1);
}
} else {
$setting2write = $settings['value'];
}
while ( ! empty($path))
{