From 2737ec1d65300b3e8aae94114b75261c5444e4bc Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Wed, 25 Mar 2026 16:39:28 +0100 Subject: [PATCH 01/11] Task: #53687 Support instance-specific cfg write targets and add coverage --- bin/cfg | 161 +++++++++++++++++++-- src/SettingsWriter.php | 15 +- tests/CfgTest.php | 307 +++++++++++++++++++++++++++++++++++++++++ tests/SettingsTest.php | 16 +++ 4 files changed, 484 insertions(+), 15 deletions(-) create mode 100644 tests/CfgTest.php diff --git a/bin/cfg b/bin/cfg index 78e84c5..6f138cf 100755 --- a/bin/cfg +++ b/bin/cfg @@ -22,7 +22,62 @@ foreach ($autoloadFiles as $autoloadFile) { } } -$version = '0.3'; +// Pre-parse site options so they can be passed after positional args. +// The input helper library does not handle this case reliably. +$preparsedSiteOptions = []; +$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 (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=')), + ]; + continue; + } + + $filteredArgv[] = $arg; + } + + $_SERVER['argv'] = $filteredArgv; + $GLOBALS['argv'] = $filteredArgv; +} + +$version = '0.4'; $actions = [ 'show', 'write', 'help' ]; $settings = ['key' => '', 'value' => '']; @@ -60,6 +115,16 @@ $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') // {{{ + ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) + ->description('Site/instance directory below config/ (target: config//.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)') + ) // }}} + ->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{ ->flags(AbstractInputType::FLAG_REQUIRED) ->description( @@ -141,8 +206,18 @@ $usage = Cli\manpage( basename(__FILE__), $version, $collection, Colour::FG_GREEN, Colour::FG_WHITE, [ 'Examples' => - 'cfg show VeruA db:host'.PHP_EOL. - 'cfg write VeruA \'db:host="newHost"\''.PHP_EOL + 'cfg write myCEESV \'auth:projectId="218523"\'' + .PHP_EOL + .'# writes local config: config/myCEESV.conf.php' + .PHP_EOL + .'cfg write myCEESV \'auth:projectId="218523"\' --directory-name=owner_xyz' + .PHP_EOL + .'# writes instance config: config/owner_xyz/myCEESV.conf.php' + .PHP_EOL + .'cfg show myCEESV auth:projectId --site=owner_xyz' + .PHP_EOL + .'# reads merged config including config/owner_xyz/myCEESV.conf.php' + .PHP_EOL ] ).PHP_EOL; @@ -181,6 +256,7 @@ echo $argv->find('setting')['value'].PHP_EOL; // var_dump($cfg); $appPath = $argv->find('appPath'); if (!$appPath) $appPath = getcwd().'/'; +$appPath = rtrim($appPath, '/').'/'; /* $it = new RecursiveDirectoryIterator($appPath); @@ -194,7 +270,62 @@ foreach(new RecursiveIteratorIterator($it) as $file) */ $mode = ($argv->find('mode') == '') ? null : $argv->find('mode'); $cfg = (new Settings([], $mode))->appPath($appPath)->prefix($prefix); -if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath($pkgPath); +// pkgPath points to package defaults (e.g. .default.conf.php) +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; + } +} + +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])); + + 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//.conf.php + $cfg->site($site); +} try { if (is_readable($cfg->buildFileName('default'))) { @@ -238,27 +369,37 @@ case 'show': case 'write': $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; - var_dump($path); - + $setting2write = $settings['value']; - + while ( ! empty($path)) { $setting2write = [array_pop($path) => $setting2write]; } - if (is_readable($file = $cfg->buildFileName())) + + $writeType = ($site !== null) ? $siteFlag : null; + $file = $cfg->buildFileName($writeType); + if (is_readable($file)) { $setting2write = array_replace_recursive(require($file), $setting2write); copy($file, "$file.bak"); } + + $targetDir = dirname($file); + if (!is_dir($targetDir) && !mkdir($targetDir, 0775, true) && !is_dir($targetDir)) { + fwrite(STDERR, "Can not create directory: $targetDir".PHP_EOL); + exit(1); + } + $writeCfg = $cfg->create($setting2write); // var_dump($writeCfg->toArray()); try { - (new SettingsWriter($writeCfg))->write(); + (new SettingsWriter($writeCfg, '', $writeType))->write(); echo "Written modified settings to: $file".PHP_EOL; } catch (\Exception $e) { - echo $e->getMessage().PHP_EOL; + fwrite(STDERR, $e->getMessage().PHP_EOL); + exit(1); } break; diff --git a/src/SettingsWriter.php b/src/SettingsWriter.php index d6fc41c..6246a9f 100644 --- a/src/SettingsWriter.php +++ b/src/SettingsWriter.php @@ -22,6 +22,8 @@ declare(strict_types=1); namespace rabe\Util; +use ErrorException; + /** * Write Settings into a File * @author Norbert.e.Wagner dev@norb.me @@ -45,13 +47,16 @@ class SettingsWriter * * @param Settings $settings an object of type settings * @param String $name The name midfix for the settings File - */ - public function __construct( Settings $settings, $name='' ) + * @param String|int|null $type Optional type for Settings::buildFileName(), e.g. site flag + */ + public function __construct( Settings $settings, $name='', $type=null ) { $this->settings = $settings; - - $file = $settings->buildFileName( $name ); - + + $file = ( $type === null ) + ? $settings->buildFileName( $name ) + : $settings->buildFileName( $type ); + if ( ! $this->handle = fopen( $file, 'w' ) ) { throw new ErrorException( "Can not open File: $file" ); diff --git a/tests/CfgTest.php b/tests/CfgTest.php new file mode 100644 index 0000000..508a212 --- /dev/null +++ b/tests/CfgTest.php @@ -0,0 +1,307 @@ +tmpDir = rtrim(sys_get_temp_dir(), '/').'/util-settings-cfg-'.bin2hex(random_bytes(8)); + mkdir($this->tmpDir, 0775, true); + mkdir($this->tmpDir.'/config', 0775, true); + + file_put_contents( + $this->tmpDir.'/config/myCEESV.default.conf.php', + " 'prod',\n\t'auth' => [\n\t\t'projectId' => '',\n\t\t'clientId' => '',\n\t],\n];\n" + ); + } + + protected function tearDown(): void + { + $this->removeDir($this->tmpDir); + } + + public function testWriteWithoutDirectoryNameUsesLocalConfigFile(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + + $localFile = $this->tmpDir.'/config/myCEESV.conf.php'; + $this->assertFileExists($localFile); + + $cfg = require $localFile; + $this->assertSame('218523', $cfg['auth']['projectId']); + } + + public function testWriteWithDirectoryNameCreatesAndWritesSiteConfig(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directory-name', + 'owner_xyz', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + + $siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'; + $this->assertFileExists($siteFile); + $this->assertFileDoesNotExist($this->tmpDir.'/config/myCEESV.conf.php'); + + $cfg = require $siteFile; + $this->assertSame('218523', $cfg['auth']['projectId']); + } + + public function testWriteWithDirectoryNameMergesIntoExistingSiteConfig(): void + { + $firstWrite = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directory-name', + 'owner_xyz', + ]); + $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); + + $secondWrite = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:clientId="service-9999-qual@myceesv.ch"', + '--directory-name', + 'owner_xyz', + ]); + $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); + + $siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'; + $cfg = require $siteFile; + + $this->assertSame('218523', $cfg['auth']['projectId']); + $this->assertSame('service-9999-qual@myceesv.ch', $cfg['auth']['clientId']); + } + + public function testWriteWithDirectoryNameUsingConfigPrefixIsNormalized(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directory-name=config/owner_xyz', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + $this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'); + $this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/myCEESV.conf.php'); + } + + public function testWriteWithSiteOptionWritesToSiteConfig(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--site=owner_xyz', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + $this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'); + } + + public function testWriteWithDirectoryNameCamelCaseOptionWritesToSiteConfig(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directoryName=owner_xyz', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + $this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'); + } + + public function testWriteToExistingSiteConfigCreatesBackup(): void + { + $firstWrite = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="first"', + '--directory-name=owner_xyz', + ]); + $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); + + $secondWrite = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="second"', + '--directory-name=owner_xyz', + ]); + $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); + + $siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'; + $backupFile = $siteFile.'.bak'; + $this->assertFileExists($backupFile); + + $current = require $siteFile; + $backup = require $backupFile; + $this->assertSame('second', $current['auth']['projectId']); + $this->assertSame('first', $backup['auth']['projectId']); + } + + public function testShowWithSiteReturnsSiteSpecificValue(): void + { + $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directory-name=owner_xyz', + ]); + + $show = $this->runCfg([ + '-a', + $this->tmpDir, + 'show', + 'myCEESV', + 'auth:projectId', + '--site=owner_xyz', + ]); + + $this->assertSame(0, $show['code'], $show['output']); + $this->assertStringContainsString('218523', $show['output']); + } + + public function testDirectoryNameRejectsTraversal(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directory-name=../owner_xyz', + ]); + + $this->assertSame(1, $result['code']); + $this->assertStringContainsString('Invalid directory in --directoryName', $result['output']); + } + + public function testDirectoryNameRejectsInvalidSegment(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directory-name=owner xyz', + ]); + + $this->assertSame(1, $result['code']); + $this->assertStringContainsString("Invalid directory name segment 'owner xyz' in --directoryName.", $result['output']); + } + + public function testDirectoryNameRejectsEmptyValue(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--directory-name=', + ]); + + $this->assertSame(1, $result['code']); + $this->assertStringContainsString('Option --directoryName is empty.', $result['output']); + } + + public function testUsingSiteAndDirectoryNameTogetherReturnsError(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + '--site', + 'owner_xyz', + '--directory-name', + 'owner_xyz', + ]); + + $this->assertSame(1, $result['code']); + $this->assertStringContainsString('Please use only one of --site or --directoryName (alias: --directory-name).', $result['output']); + } + + private function runCfg(array $args): array + { + $script = realpath(__DIR__.'/../bin/cfg'); + $this->assertNotFalse($script); + + $command = escapeshellarg(PHP_BINARY).' '.escapeshellarg($script); + foreach ($args as $arg) { + $command .= ' '.escapeshellarg($arg); + } + $command .= ' 2>&1'; + + $outputLines = []; + $exitCode = 0; + exec($command, $outputLines, $exitCode); + + return [ + 'code' => $exitCode, + 'output' => implode(PHP_EOL, $outputLines), + ]; + } + + private function removeDir(string $dir): void + { + if (!is_dir($dir)) { + return; + } + + foreach (scandir($dir) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + + $path = $dir.'/'.$entry; + if (is_dir($path)) { + $this->removeDir($path); + } else { + unlink($path); + } + } + + rmdir($dir); + } +} diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index 64ed411..c895bba 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -211,6 +211,22 @@ class SettingsTest extends TestCase $this->assertEquals($path.$expected, $cfg->buildFileName($type)); } + + public function testBuildFileNameWithPrefixAndSite(): void + { + $cfg = new Settings(); + $cfg->appPath('./')->prefix('myCEESV')->site('owner_xyz'); + + $this->assertEquals('./config/owner_xyz/myCEESV.conf.php', $cfg->buildFileName(0x01)); + } + + public function testBuildFileNameWithPrefixAndNestedSite(): void + { + $cfg = new Settings(); + $cfg->appPath('./')->prefix('myCEESV')->site('owner_xyz/sub_a'); + + $this->assertEquals('./config/owner_xyz/sub_a/myCEESV.conf.php', $cfg->buildFileName(0x01)); + } public function fileNameData() { -- 2.52.0 From adf5a98768df4f8548e374ce7ec8c4a3ea1ac81a Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Thu, 26 Mar 2026 09:31:07 +0100 Subject: [PATCH 02/11] Task: #53687 Handle missing mode fallback and add no-mode cfg tests --- bin/cfg | 2 +- src/Settings.php | 15 +++++++++++++-- tests/CfgTest.php | 19 +++++++++++++++++++ tests/SettingsTest.php | 9 +++++++++ tests/cfg/noMode/config/default.conf.php | 3 +++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/cfg/noMode/config/default.conf.php diff --git a/bin/cfg b/bin/cfg index 6f138cf..2ce253f 100755 --- a/bin/cfg +++ b/bin/cfg @@ -334,7 +334,7 @@ try { elseif (is_readable($cfgFile = $cfg->buildFileName())) { $cfg->load(require($cfgFile)); } -} catch (Exception $e) { +} catch (\Throwable $e) { fwrite(STDERR, "Error: ".$e->getMessage().PHP_EOL); exit(1); } diff --git a/src/Settings.php b/src/Settings.php index 56e1b0f..ebf7aeb 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -159,7 +159,18 @@ class Settings implements \Iterator, \Countable // if a mode was set in the constructor do not overwrite it if (! isset($this->mode)) { // if a localConf Mode is set use it, or take the default conf mode - $this->mode = (isset($localConf['mode'])) ? $localConf['mode'] : $this->settings['mode']; + // if local/default config has no mode, fall back to the first known mode (normally "prod") + if (isset($localConf['mode']) && is_string($localConf['mode']) && $localConf['mode'] !== '') { + $this->mode = $localConf['mode']; + } + elseif (isset($this->settings['mode']) && is_string($this->settings['mode']) && $this->settings['mode'] !== '') { + $this->mode = $this->settings['mode']; + } + else { + // Backward-compatible fallback for configs without explicit mode. + $this->mode = (string)array_key_first($this->modes); + $this->settings['mode'] = $this->mode; + } } else { $this->settings['mode'] = $this->mode; } @@ -358,4 +369,4 @@ class Settings implements \Iterator, \Countable /* jEdit buffer local properties {{{ * :folding=explicit:collapseFolds=1: -}}}*/ \ No newline at end of file +}}}*/ diff --git a/tests/CfgTest.php b/tests/CfgTest.php index 508a212..79167ea 100644 --- a/tests/CfgTest.php +++ b/tests/CfgTest.php @@ -44,6 +44,25 @@ class CfgTest extends TestCase $this->assertSame('218523', $cfg['auth']['projectId']); } + public function testWriteWithoutModeFallsBackToDefaultMode(): void + { + file_put_contents( + $this->tmpDir.'/config/myCEESV.default.conf.php', + " [\n\t\t'projectId' => '',\n\t],\n];\n" + ); + + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'myCEESV', + 'auth:projectId="218523"', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + $this->assertFileExists($this->tmpDir.'/config/myCEESV.conf.php'); + } + public function testWriteWithDirectoryNameCreatesAndWritesSiteConfig(): void { $result = $this->runCfg([ diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index c895bba..ae70ef7 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -192,6 +192,15 @@ class SettingsTest extends TestCase $cfg = $this->appPath($cfg, 'localOverride')->load(); $this->assertEquals(42, $cfg->answer); } + + public function testLoadWithoutModeFallsBackToFirstKnownMode(): void + { + $cfg = new Settings(); + $cfg = $this->appPath($cfg, 'noMode')->load(); + + $this->assertEquals('prod', $cfg->mode); + $this->assertEquals('default', $cfg->testFiles); + } /** * @dataProvider fileNameData diff --git a/tests/cfg/noMode/config/default.conf.php b/tests/cfg/noMode/config/default.conf.php new file mode 100644 index 0000000..c0a0d9b --- /dev/null +++ b/tests/cfg/noMode/config/default.conf.php @@ -0,0 +1,3 @@ + 'default', +]; -- 2.52.0 From 227de9ac07621ad69cf1fa20672e19dfdf96c616 Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Thu, 26 Mar 2026 11:28:58 +0100 Subject: [PATCH 03/11] 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 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) --- bin/cfg | 222 +++++++++++++------------ tests/CfgTest.php | 368 +++++++++++++++++++++++------------------ tests/SettingsTest.php | 8 +- 3 files changed, 327 insertions(+), 271 deletions(-) diff --git a/bin/cfg b/bin/cfg index 2ce253f..c44d1d5 100755 --- a/bin/cfg +++ b/bin/cfg @@ -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//.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//.conf.php - $cfg->site($site); + $site = $siteInput; + // Reuse util-settings site resolution: config//.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)) { diff --git a/tests/CfgTest.php b/tests/CfgTest.php index 79167ea..5aebc06 100644 --- a/tests/CfgTest.php +++ b/tests/CfgTest.php @@ -15,8 +15,8 @@ class CfgTest extends TestCase mkdir($this->tmpDir.'/config', 0775, true); file_put_contents( - $this->tmpDir.'/config/myCEESV.default.conf.php', - " 'prod',\n\t'auth' => [\n\t\t'projectId' => '',\n\t\t'clientId' => '',\n\t],\n];\n" + $this->tmpDir.'/config/Extension.default.conf.php', + " 'prod',\n\t'module' => [\n\t\t'code' => '',\n\t\t'label' => '',\n\t\t'flags' => [\n\t\t\t'enabled' => false,\n\t\t],\n\t],\n\t'feature' => [\n\t\t'endpoint' => '',\n\t],\n];\n" ); } @@ -28,257 +28,309 @@ class CfgTest extends TestCase public function testWriteWithoutDirectoryNameUsesLocalConfigFile(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + ]); $this->assertSame(0, $result['code'], $result['output']); - $localFile = $this->tmpDir.'/config/myCEESV.conf.php'; + $localFile = $this->tmpDir.'/config/Extension.conf.php'; $this->assertFileExists($localFile); $cfg = require $localFile; - $this->assertSame('218523', $cfg['auth']['projectId']); + $this->assertSame('X100', $cfg['module']['code']); } public function testWriteWithoutModeFallsBackToDefaultMode(): void { file_put_contents( - $this->tmpDir.'/config/myCEESV.default.conf.php', - " [\n\t\t'projectId' => '',\n\t],\n];\n" + $this->tmpDir.'/config/Extension.default.conf.php', + " [\n\t\t'code' => '',\n\t],\n];\n" + ); + + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + $this->assertFileExists($this->tmpDir.'/config/Extension.conf.php'); + } + + public function testWriteWithInputFileWritesMultipleSettings(): void + { + $inFile = $this->tmpDir.'/extension-in.json'; + file_put_contents( + $inFile, + json_encode([ + 'module' => [ + 'code' => 'X100', + 'label' => 'demo-module', + ], + 'feature' => [ + 'endpoint' => 'https://example.invalid/v1/resource', + ], + ], JSON_PRETTY_PRINT) ); $result = $this->runCfg([ '-a', $this->tmpDir, 'write', - 'myCEESV', - 'auth:projectId="218523"', + 'Extension', + '--siteDir=owner_xyz', + '-i', + $inFile, ]); $this->assertSame(0, $result['code'], $result['output']); - $this->assertFileExists($this->tmpDir.'/config/myCEESV.conf.php'); + $cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; + $this->assertSame('X100', $cfg['module']['code']); + $this->assertSame('demo-module', $cfg['module']['label']); + $this->assertSame('https://example.invalid/v1/resource', $cfg['feature']['endpoint']); } - public function testWriteWithDirectoryNameCreatesAndWritesSiteConfig(): void + public function testWriteAcceptsJsonStringValueInSetting(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module={"code":"X100","label":"demo-module"}', + '--siteDir=owner_xyz', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + $cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; + $this->assertSame('X100', $cfg['module']['code']); + $this->assertSame('demo-module', $cfg['module']['label']); + } + + public function testWriteSupportsNestedPathWithColonNotation(): void { $result = $this->runCfg([ '-a', $this->tmpDir, 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directory-name', - 'owner_xyz', + 'Extension', + 'module:flags:enabled=true', + '--siteDir=owner_xyz', ]); $this->assertSame(0, $result['code'], $result['output']); - - $siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'; - $this->assertFileExists($siteFile); - $this->assertFileDoesNotExist($this->tmpDir.'/config/myCEESV.conf.php'); - - $cfg = require $siteFile; - $this->assertSame('218523', $cfg['auth']['projectId']); + $cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; + $this->assertTrue($cfg['module']['flags']['enabled']); } - public function testWriteWithDirectoryNameMergesIntoExistingSiteConfig(): void + public function testWriteWithInputFileAndSettingReturnsError(): void + { + $inFile = $this->tmpDir.'/extension-in.json'; + file_put_contents($inFile, json_encode(['module' => ['code' => 'X100']])); + + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="x"', + '-i', + $inFile, + ]); + + $this->assertSame(1, $result['code']); + $this->assertStringContainsString('Please use either SETTING or --in, not both.', $result['output']); + } + + public function testWriteWithSiteDirCreatesAndWritesSiteConfig(): void + { + $result = $this->runCfg([ + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir', + 'owner_xyz', + ]); + + $this->assertSame(0, $result['code'], $result['output']); + + $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; + $this->assertFileExists($siteFile); + $this->assertFileDoesNotExist($this->tmpDir.'/config/Extension.conf.php'); + + $cfg = require $siteFile; + $this->assertSame('X100', $cfg['module']['code']); + } + + public function testWriteWithSiteDirMergesIntoExistingSiteConfig(): void { $firstWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directory-name', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir', + 'owner_xyz', + ]); $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); $secondWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:clientId="service-9999-qual@myceesv.ch"', - '--directory-name', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:label="demo-module"', + '--siteDir', + 'owner_xyz', + ]); $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); - $siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'; + $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; $cfg = require $siteFile; - $this->assertSame('218523', $cfg['auth']['projectId']); - $this->assertSame('service-9999-qual@myceesv.ch', $cfg['auth']['clientId']); + $this->assertSame('X100', $cfg['module']['code']); + $this->assertSame('demo-module', $cfg['module']['label']); } - public function testWriteWithDirectoryNameUsingConfigPrefixIsNormalized(): void + public function testWriteWithSiteDirUsingConfigPrefixIsNormalized(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directory-name=config/owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=config/owner_xyz', + ]); $this->assertSame(0, $result['code'], $result['output']); - $this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'); - $this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/myCEESV.conf.php'); + $this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php'); + $this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/Extension.conf.php'); } - public function testWriteWithSiteOptionWritesToSiteConfig(): void + public function testWriteWithSiteDirWritesToSiteConfig(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--site=owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $result['code'], $result['output']); - $this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'); - } - - public function testWriteWithDirectoryNameCamelCaseOptionWritesToSiteConfig(): void - { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directoryName=owner_xyz', - ]); - - $this->assertSame(0, $result['code'], $result['output']); - $this->assertFileExists($this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'); + $this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php'); } public function testWriteToExistingSiteConfigCreatesBackup(): void { $firstWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="first"', - '--directory-name=owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="first"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); $secondWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="second"', - '--directory-name=owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="second"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); - $siteFile = $this->tmpDir.'/config/owner_xyz/myCEESV.conf.php'; + $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; $backupFile = $siteFile.'.bak'; $this->assertFileExists($backupFile); $current = require $siteFile; $backup = require $backupFile; - $this->assertSame('second', $current['auth']['projectId']); - $this->assertSame('first', $backup['auth']['projectId']); + $this->assertSame('second', $current['module']['code']); + $this->assertSame('first', $backup['module']['code']); } public function testShowWithSiteReturnsSiteSpecificValue(): void { $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directory-name=owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); $show = $this->runCfg([ - '-a', - $this->tmpDir, - 'show', - 'myCEESV', - 'auth:projectId', - '--site=owner_xyz', - ]); + '-a', + $this->tmpDir, + 'show', + 'Extension', + 'module:code', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $show['code'], $show['output']); - $this->assertStringContainsString('218523', $show['output']); + $this->assertStringContainsString('X100', $show['output']); } - public function testDirectoryNameRejectsTraversal(): void + public function testSiteDirRejectsTraversal(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directory-name=../owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=../owner_xyz', + ]); $this->assertSame(1, $result['code']); - $this->assertStringContainsString('Invalid directory in --directoryName', $result['output']); + $this->assertStringContainsString('Invalid directory in --siteDir', $result['output']); } - public function testDirectoryNameRejectsInvalidSegment(): void + public function testSiteDirRejectsInvalidSegment(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directory-name=owner xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=owner xyz', + ]); $this->assertSame(1, $result['code']); - $this->assertStringContainsString("Invalid directory name segment 'owner xyz' in --directoryName.", $result['output']); + $this->assertStringContainsString("Invalid directory name segment 'owner xyz' in --siteDir.", $result['output']); } - public function testDirectoryNameRejectsEmptyValue(): void + public function testSiteDirRejectsEmptyValue(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--directory-name=', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=', + ]); $this->assertSame(1, $result['code']); - $this->assertStringContainsString('Option --directoryName is empty.', $result['output']); - } - - public function testUsingSiteAndDirectoryNameTogetherReturnsError(): void - { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'myCEESV', - 'auth:projectId="218523"', - '--site', - 'owner_xyz', - '--directory-name', - 'owner_xyz', - ]); - - $this->assertSame(1, $result['code']); - $this->assertStringContainsString('Please use only one of --site or --directoryName (alias: --directory-name).', $result['output']); + $this->assertStringContainsString('Option --siteDir is empty.', $result['output']); } private function runCfg(array $args): array diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index ae70ef7..09f311c 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -224,17 +224,17 @@ class SettingsTest extends TestCase public function testBuildFileNameWithPrefixAndSite(): void { $cfg = new Settings(); - $cfg->appPath('./')->prefix('myCEESV')->site('owner_xyz'); + $cfg->appPath('./')->prefix('Extension')->site('owner_xyz'); - $this->assertEquals('./config/owner_xyz/myCEESV.conf.php', $cfg->buildFileName(0x01)); + $this->assertEquals('./config/owner_xyz/Extension.conf.php', $cfg->buildFileName(0x01)); } public function testBuildFileNameWithPrefixAndNestedSite(): void { $cfg = new Settings(); - $cfg->appPath('./')->prefix('myCEESV')->site('owner_xyz/sub_a'); + $cfg->appPath('./')->prefix('Extension')->site('owner_xyz/sub_a'); - $this->assertEquals('./config/owner_xyz/sub_a/myCEESV.conf.php', $cfg->buildFileName(0x01)); + $this->assertEquals('./config/owner_xyz/sub_a/Extension.conf.php', $cfg->buildFileName(0x01)); } public function fileNameData() -- 2.52.0 From 29d1da4ac582a81ac26e726c332986cf3482ee34 Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Thu, 26 Mar 2026 12:10:55 +0100 Subject: [PATCH 04/11] Task: #53687 Align cfg brace style to project conventions --- bin/cfg | 90 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/bin/cfg b/bin/cfg index c44d1d5..a8cbca5 100755 --- a/bin/cfg +++ b/bin/cfg @@ -24,18 +24,23 @@ foreach ($autoloadFiles as $autoloadFile) { function readJsonInputFile(string $path): array { - if (!is_readable($path)) { + if (!is_readable($path)) + { throw new \RuntimeException("Input file is not readable: $path"); } $content = file_get_contents($path); - if ($content === false) { + if ($content === false) + { throw new \RuntimeException("Can not read input file: $path"); } - try { + try + { $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); - } catch (\JsonException $ex) { + } + catch (\JsonException $ex) + { throw new \RuntimeException(sprintf( 'Input JSON is invalid (%s): %s', (string)$ex->getCode(), @@ -43,7 +48,8 @@ function readJsonInputFile(string $path): array )); } - if (!is_array($data)) { + if (!is_array($data)) + { throw new \RuntimeException('Input JSON must decode to an object/array'); } @@ -54,17 +60,21 @@ function readJsonInputFile(string $path): array // The input helper library does not handle this case reliably. $preparsedSiteDir = null; $rawArgv = $_SERVER['argv'] ?? $GLOBALS['argv'] ?? null; -if (is_array($rawArgv) && !empty($rawArgv)) { +if (is_array($rawArgv) && !empty($rawArgv)) +{ $filteredArgv = [$rawArgv[0]]; - for ($idx = 1; $idx < count($rawArgv); $idx++) { + for ($idx = 1; $idx < count($rawArgv); $idx++) + { $arg = $rawArgv[$idx]; - if ($arg === '--siteDir') { + if ($arg === '--siteDir') + { $preparsedSiteDir = $rawArgv[$idx + 1] ?? ''; if (isset($rawArgv[$idx + 1])) $idx++; continue; } - if (str_starts_with($arg, '--siteDir=')) { + if (str_starts_with($arg, '--siteDir=')) + { $preparsedSiteDir = substr($arg, strlen('--siteDir=')); continue; } @@ -224,9 +234,12 @@ $usage = Cli\manpage( basename(__FILE__), $version, // Get the supplied input. Passing the collection will make the handler bind values // and validate the input according to our collection -try { +try +{ $argv = Input\InputHandlerFactory::build('Argv', $collection); -} catch (\Exception $ex) { +} +catch (\Exception $ex) +{ echo $usage; if (isset($argv[1])) { @@ -277,29 +290,37 @@ if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/'); $site = null; $siteFlag = 0x01; $siteInput = ($preparsedSiteDir !== null) ? $preparsedSiteDir : $argv->find('siteDir'); -if ($siteInput !== null && $siteInput !== false) { +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/')) { + if (str_starts_with($siteInput, $appPath.'config/')) + { $siteInput = substr($siteInput, strlen($appPath.'config/')); - } elseif (str_starts_with($siteInput, 'config/')) { + } + elseif (str_starts_with($siteInput, 'config/')) + { $siteInput = substr($siteInput, strlen('config/')); } $siteInput = trim($siteInput, '/'); - if ($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)) { + 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)) { + 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); } @@ -310,14 +331,17 @@ if ($siteInput !== null && $siteInput !== false) { $cfg->site($site); } -try { +try +{ if (is_readable($cfg->buildFileName('default'))) { $cfg->load(); } elseif (is_readable($cfgFile = $cfg->buildFileName())) { $cfg->load(require($cfgFile)); } -} catch (\Throwable $e) { +} +catch (\Throwable $e) +{ fwrite(STDERR, "Error: ".$e->getMessage().PHP_EOL); exit(1); } @@ -355,24 +379,32 @@ case 'write': // 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'] !== '') { + if ($inputFile && $settings['key'] !== '') + { fwrite(STDERR, 'Please use either SETTING or --in, not both.'.PHP_EOL); exit(1); } - if (!$inputFile && $settings['key'] === '') { + if (!$inputFile && $settings['key'] === '') + { fwrite(STDERR, 'Nothing to write: provide SETTING or --in.'.PHP_EOL); exit(1); } $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; - if ($inputFile) { - try { + if ($inputFile) + { + try + { $setting2write = readJsonInputFile($inputFile); - } catch (\Throwable $e) { + } + catch (\Throwable $e) + { fwrite(STDERR, $e->getMessage().PHP_EOL); exit(1); } - } else { + } + else + { $setting2write = $settings['value']; } @@ -390,14 +422,16 @@ case 'write': } $targetDir = dirname($file); - if (!is_dir($targetDir) && !mkdir($targetDir, 0775, true) && !is_dir($targetDir)) { + if (!is_dir($targetDir) && !mkdir($targetDir, 0775, true) && !is_dir($targetDir)) + { fwrite(STDERR, "Can not create directory: $targetDir".PHP_EOL); exit(1); } $writeCfg = $cfg->create($setting2write); // var_dump($writeCfg->toArray()); - try { + try + { (new SettingsWriter($writeCfg, '', $writeType))->write(); echo "Written modified settings to: $file".PHP_EOL; } -- 2.52.0 From 2a391f4e0a4d65dabae3e3ae54e5b03d7e867cde Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Thu, 26 Mar 2026 12:27:12 +0100 Subject: [PATCH 05/11] Task: #53687 Simplify cfg argument handling and keep --siteDir with JSON write input (-i) --- bin/cfg | 32 +------------------------------ tests/CfgTest.php | 49 ++++++++++++++++++++++------------------------- 2 files changed, 24 insertions(+), 57 deletions(-) diff --git a/bin/cfg b/bin/cfg index a8cbca5..0fef170 100755 --- a/bin/cfg +++ b/bin/cfg @@ -56,36 +56,6 @@ function readJsonInputFile(string $path): array return $data; } -// Pre-parse siteDir so it can be passed after positional args. -// The input helper library does not handle this case reliably. -$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 === '--siteDir') - { - $preparsedSiteDir = $rawArgv[$idx + 1] ?? ''; - if (isset($rawArgv[$idx + 1])) $idx++; - continue; - } - if (str_starts_with($arg, '--siteDir=')) - { - $preparsedSiteDir = substr($arg, strlen('--siteDir=')); - continue; - } - - $filteredArgv[] = $arg; - } - - $_SERVER['argv'] = $filteredArgv; - $GLOBALS['argv'] = $filteredArgv; -} - $version = '0.4'; $actions = [ 'show', 'write', 'help' ]; @@ -289,7 +259,7 @@ if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/'); $site = null; $siteFlag = 0x01; -$siteInput = ($preparsedSiteDir !== null) ? $preparsedSiteDir : $argv->find('siteDir'); +$siteInput = $argv->find('siteDir'); if ($siteInput !== null && $siteInput !== false) { $siteInput = trim((string)$siteInput); diff --git a/tests/CfgTest.php b/tests/CfgTest.php index 5aebc06..be9b3c6 100644 --- a/tests/CfgTest.php +++ b/tests/CfgTest.php @@ -151,14 +151,13 @@ class CfgTest extends TestCase public function testWriteWithSiteDirCreatesAndWritesSiteConfig(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $result['code'], $result['output']); @@ -173,25 +172,23 @@ class CfgTest extends TestCase public function testWriteWithSiteDirMergesIntoExistingSiteConfig(): void { $firstWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); $secondWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:label="demo-module"', - '--siteDir', - 'owner_xyz', - ]); + '-a', + $this->tmpDir, + 'write', + 'Extension', + 'module:label="demo-module"', + '--siteDir=owner_xyz', + ]); $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; @@ -327,10 +324,10 @@ class CfgTest extends TestCase 'Extension', 'module:code="X100"', '--siteDir=', - ]); + ]); $this->assertSame(1, $result['code']); - $this->assertStringContainsString('Option --siteDir is empty.', $result['output']); + $this->assertStringContainsString('a value is required for --siteDir', $result['output']); } private function runCfg(array $args): array -- 2.52.0 From 480d97c804a58df4c5c94e579be44af2efdd9e59 Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Fri, 27 Mar 2026 08:55:53 +0100 Subject: [PATCH 06/11] Task: #53687 Add minimal --verbose mode and replace legacy var_dump debug points --- bin/cfg | 79 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/bin/cfg b/bin/cfg index 0fef170..4f9b6ca 100755 --- a/bin/cfg +++ b/bin/cfg @@ -56,6 +56,11 @@ function readJsonInputFile(string $path): array return $data; } +function verboseLog(bool $enabled, string $message): void +{ + if ($enabled) fwrite(STDERR, "[verbose] $message".PHP_EOL); +} + $version = '0.4'; $actions = [ 'show', 'write', 'help' ]; @@ -69,6 +74,11 @@ $collection = (new Input\InputCollection()) ->description('Display help text') ) // }}} + ->add( Input\InputTypeFactory::build('LongOption')->name('verbose')->short('v') // {{{ + ->flags(AbstractInputType::FLAG_OPTIONAL) + ->description('Print debug details to STDERR') + ) // }}} + ->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 read (for write)') @@ -179,26 +189,37 @@ $usage = Cli\manpage( basename(__FILE__), $version, $collection, Colour::FG_GREEN, Colour::FG_WHITE, [ 'Examples' => - 'cfg write extension_name \'module:enabled=true\'' - .PHP_EOL - .'# writes local config: config/extension_name.conf.php' - .PHP_EOL - .'cfg write extension_name -i /tmp/extension.json --siteDir=owner_xyz' - .PHP_EOL - .'# writes multiple settings from JSON to config/owner_xyz/extension_name.conf.php' - .PHP_EOL - .'cfg write extension_name \'feature_example:enabled=true\' --siteDir=owner_xyz' - .PHP_EOL - .'# 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 + 'Basic usage:' + .PHP_EOL + .'cfg show VeruA db:host' + .PHP_EOL + .'cfg write VeruA \'db:host="newHost"\'' + .PHP_EOL + .PHP_EOL + .'Advance usage:' + .PHP_EOL + .'cfg write extension_name \'module:enabled=true\'' + .PHP_EOL + .PHP_EOL + .'#Instance write:' + .PHP_EOL + .'cfg write extension_name \'module:enabled=true\' --siteDir=owner_xyz' + .PHP_EOL + .PHP_EOL + .'#Batch write from JSON file:' + .PHP_EOL + .'cfg write extension_name --siteDir=owner_xyz -i /tmp/extension.json' + .PHP_EOL + .PHP_EOL + .'#Batch write from JSON string:' + .PHP_EOL + .'cfg write extension_name \'module={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz' + .PHP_EOL + .PHP_EOL + .'#Read merged value for an instance:' + .PHP_EOL + .'cfg show extension_name module:enabled --siteDir=owner_xyz' + .PHP_EOL ] ).PHP_EOL; @@ -231,13 +252,7 @@ if ($argv->find( 'help' ) || $argv->find('action') == 'help') $prefix = $argv->find('prefix'); -/* -echo $argv->find('action').PHP_EOL; -echo ($prefix).PHP_EOL; -echo $argv->find('setting')['key'].PHP_EOL; -echo $argv->find('setting')['value'].PHP_EOL; - */ -// var_dump($cfg); +$verbose = (bool)$argv->find('verbose'); $appPath = $argv->find('appPath'); if (!$appPath) $appPath = getcwd().'/'; $appPath = rtrim($appPath, '/').'/'; @@ -299,6 +314,7 @@ if ($siteInput !== null && $siteInput !== false) $site = $siteInput; // Reuse util-settings site resolution: config//.conf.php $cfg->site($site); + verboseLog($verbose, "siteDir resolved to '$site'"); } try @@ -315,7 +331,7 @@ catch (\Throwable $e) fwrite(STDERR, "Error: ".$e->getMessage().PHP_EOL); exit(1); } -//var_dump($cfg); +verboseLog($verbose, 'config bootstrap loaded'); $result = $cfg; $settings = $argv->find('setting') ?? $settings; @@ -340,6 +356,7 @@ if ($result instanceof Settings) $result = $result->toArray(); switch ($argv->find('action')) { case 'show': + verboseLog($verbose, "show action for prefix '$prefix'"); $out = (is_string($result)) ? $result : json_encode($result, JSON_PRETTY_PRINT); echo $out.PHP_EOL; break; @@ -361,6 +378,7 @@ case 'write': } $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; + verboseLog($verbose, 'write source: '.($inputFile ? "--in ($inputFile)" : 'SETTING')); if ($inputFile) { try @@ -377,6 +395,7 @@ case 'write': { $setting2write = $settings['value']; } + verboseLog($verbose, 'write path: '.json_encode($path)); while ( ! empty($path)) { @@ -385,10 +404,12 @@ case 'write': $writeType = ($site !== null) ? $siteFlag : null; $file = $cfg->buildFileName($writeType); + verboseLog($verbose, "write target: $file"); if (is_readable($file)) { $setting2write = array_replace_recursive(require($file), $setting2write); copy($file, "$file.bak"); + verboseLog($verbose, "existing config merged from: $file"); } $targetDir = dirname($file); @@ -399,7 +420,7 @@ case 'write': } $writeCfg = $cfg->create($setting2write); -// var_dump($writeCfg->toArray()); + verboseLog($verbose, 'payload prepared for writer'); try { (new SettingsWriter($writeCfg, '', $writeType))->write(); -- 2.52.0 From 1c851632c3082a7a9462350064cbde7125379347 Mon Sep 17 00:00:00 2001 From: Alejandro Sosa Date: Mon, 30 Mar 2026 13:57:29 +0200 Subject: [PATCH 07/11] Task: #53687 Refactor CLI input validation into option validators, add validator coverage, and refactor cfg CLI tests --- bin/cfg | 247 ++++++++++++++------------- tests/CfgTest.php | 424 +++++++++++++++++++++++++++------------------- 2 files changed, 382 insertions(+), 289 deletions(-) diff --git a/bin/cfg b/bin/cfg index 4f9b6ca..5c2203c 100755 --- a/bin/cfg +++ b/bin/cfg @@ -22,40 +22,6 @@ foreach ($autoloadFiles as $autoloadFile) { } } -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; -} - function verboseLog(bool $enabled, string $message): void { if ($enabled) fwrite(STDERR, "[verbose] $message".PHP_EOL); @@ -80,9 +46,49 @@ $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 read (for write)') - ) // }}} + ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) + ->description('Path to a JSON data file to read (for write)') + ->validator(new Input\Validator( + function (AbstractInputType $input, AbstractInputHandler $context) + { + $path = $context->find('in'); + if ($path === null || $path === '') { + return null; + } + + if (!is_readable($path)) + { + throw new \Exception("Input file is not readable: $path"); + } + + $content = file_get_contents($path); + if ($content === false) + { + throw new \Exception("Can not read input file: $path"); + } + + try + { + $data = json_decode($content, true, 512, JSON_THROW_ON_ERROR); + } + catch (\JsonException $ex) + { + throw new \Exception(sprintf( + 'Input JSON is invalid (%s): %s', + (string)$ex->getCode(), + $ex->getMessage() + )); + } + + if (!is_array($data)) + { + throw new \Exception('Input JSON must decode to an object/array'); + } + + return $data; + } + )) + ) // }}} ->add( Input\InputTypeFactory::build('LongOption')->name('mode')->short('m') // {{{ ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) @@ -100,9 +106,58 @@ $collection = (new Input\InputCollection()) ) // }}} ->add( Input\InputTypeFactory::build('LongOption')->name('siteDir') // {{{ - ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) - ->description('Site/instance directory below config/. Accepts owner_xyz or config/owner_xyz') - ) // }}} + ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) + ->description('Site/instance directory below config/. Accepts owner_xyz or config/owner_xyz') + ->validator(new Input\Validator( + function (AbstractInputType $input, AbstractInputHandler $context) + { + $siteInput = $context->find('siteDir'); + if ($siteInput === null || $siteInput === false) { + return null; + } + + $siteInput = trim((string)$siteInput); + $appPath = $context->find('appPath'); + if (!$appPath) { + $appPath = getcwd().'/'; + } + $appPath = rtrim($appPath, '/').'/'; + + // 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 === '') + { + throw new \Exception('Option --siteDir is empty.'); + } + + // Block directory traversal and hidden-dot segments. + if (str_contains($siteInput, '..') || preg_match('~(^|/)\.(?:/|$)~', $siteInput)) + { + throw new \Exception("Invalid directory in --siteDir: '$siteInput'."); + } + + // Allow only predictable path segments for instance directories. + foreach (explode('/', $siteInput) as $part) + { + if ($part === '' || !preg_match('/^[A-Za-z0-9._-]+$/', $part)) + { + throw new \Exception("Invalid directory name segment '$part' in --siteDir."); + } + } + + return $siteInput; + } + )) + ) // }}} ->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{ ->flags(AbstractInputType::FLAG_REQUIRED) @@ -146,15 +201,23 @@ $collection = (new Input\InputCollection()) ->description( 'the settings you want to work on. With action "write" you can pass the value to set as JSON after an equal sign.' ) - ->validator(new Input\Validator( - function (AbstractInputType $input, AbstractInputHandler $context) - { - $setting = $context->find('setting'); - $action = $context->find('action'); + ->validator(new Input\Validator( + function (AbstractInputType $input, AbstractInputHandler $context) + { + $setting = $context->find('setting'); + $action = $context->find('action'); + $inputPayload = $context->find('in'); - if ($setting === null || $setting === '') { - return ['key' => '', 'value' => null]; - } + if ($action === 'write') + { + if (($setting !== null && $setting !== '') && $inputPayload) { + throw new \Exception('Please use either SETTING or --in, not both.'); + } + } + + if ($setting === null || $setting === '') { + return ['key' => '', 'value' => null]; + } $setting = explode('=', $setting); $settings['key'] = $setting[0]; @@ -274,47 +337,12 @@ if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath(rtrim($pkgPath, '/').'/'); $site = null; $siteFlag = 0x01; -$siteInput = $argv->find('siteDir'); -if ($siteInput !== null && $siteInput !== false) +$site = $argv->find('siteDir'); +if ($site !== null && $site !== 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/')); - } - - $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); - } - } - - $site = $siteInput; - // Reuse util-settings site resolution: config//.conf.php - $cfg->site($site); - verboseLog($verbose, "siteDir resolved to '$site'"); + // Reuse util-settings site resolution: config//.conf.php + $cfg->site($site); + verboseLog($verbose, "siteDir resolved to '$site'"); } try @@ -361,38 +389,21 @@ case 'show': echo $out.PHP_EOL; break; -case 'write': - $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); - } - - $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; - verboseLog($verbose, 'write source: '.($inputFile ? "--in ($inputFile)" : 'SETTING')); - if ($inputFile) - { - try + case 'write': + $inputPayload = $argv->find('in'); + if (!$inputPayload && $settings['key'] === '') + { + fwrite(STDERR, 'Nothing to write: provide SETTING or --in.'.PHP_EOL); + exit(1); + } + $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; + verboseLog($verbose, 'write source: '.($inputPayload ? '--in' : 'SETTING')); + if ($inputPayload) { - $setting2write = readJsonInputFile($inputFile); + $setting2write = $inputPayload; } - catch (\Throwable $e) + else { - fwrite(STDERR, $e->getMessage().PHP_EOL); - exit(1); - } - } - else - { $setting2write = $settings['value']; } verboseLog($verbose, 'write path: '.json_encode($path)); @@ -402,7 +413,7 @@ case 'write': $setting2write = [array_pop($path) => $setting2write]; } - $writeType = ($site !== null) ? $siteFlag : null; + $writeType = ($site !== null && $site !== false) ? $siteFlag : null; $file = $cfg->buildFileName($writeType); verboseLog($verbose, "write target: $file"); if (is_readable($file)) diff --git a/tests/CfgTest.php b/tests/CfgTest.php index be9b3c6..646bcb2 100644 --- a/tests/CfgTest.php +++ b/tests/CfgTest.php @@ -8,6 +8,7 @@ class CfgTest extends TestCase { private string $tmpDir; + /* Lifecycle {{{ */ protected function setUp(): void { $this->tmpDir = rtrim(sys_get_temp_dir(), '/').'/util-settings-cfg-'.bin2hex(random_bytes(8)); @@ -24,18 +25,16 @@ class CfgTest extends TestCase { $this->removeDir($this->tmpDir); } + /* }}} */ + /* Write Flow {{{ */ public function testWriteWithoutDirectoryNameUsesLocalConfigFile(): void { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - ]); + $result = $this->runWrite([ + 'module:code="X100"', + ]); - $this->assertSame(0, $result['code'], $result['output']); + $this->assertCfgSuccess($result); $localFile = $this->tmpDir.'/config/Extension.conf.php'; $this->assertFileExists($localFile); @@ -51,46 +50,34 @@ class CfgTest extends TestCase " [\n\t\t'code' => '',\n\t],\n];\n" ); - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - ]); + $result = $this->runWrite([ + 'module:code="X100"', + ]); - $this->assertSame(0, $result['code'], $result['output']); + $this->assertCfgSuccess($result); $this->assertFileExists($this->tmpDir.'/config/Extension.conf.php'); } public function testWriteWithInputFileWritesMultipleSettings(): void { - $inFile = $this->tmpDir.'/extension-in.json'; - file_put_contents( - $inFile, - json_encode([ - 'module' => [ - 'code' => 'X100', - 'label' => 'demo-module', - ], - 'feature' => [ - 'endpoint' => 'https://example.invalid/v1/resource', - ], - ], JSON_PRETTY_PRINT) - ); + $inFile = $this->createJsonInputFile([ + 'module' => [ + 'code' => 'X100', + 'label' => 'demo-module', + ], + 'feature' => [ + 'endpoint' => 'https://example.invalid/v1/resource', + ], + ]); - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', + $result = $this->runWrite([ '--siteDir=owner_xyz', '-i', $inFile, ]); - $this->assertSame(0, $result['code'], $result['output']); - $cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; + $this->assertCfgSuccess($result); + $cfg = $this->loadWrittenConfig('owner_xyz'); $this->assertSame('X100', $cfg['module']['code']); $this->assertSame('demo-module', $cfg['module']['label']); $this->assertSame('https://example.invalid/v1/resource', $cfg['feature']['endpoint']); @@ -98,68 +85,59 @@ class CfgTest extends TestCase public function testWriteAcceptsJsonStringValueInSetting(): void { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module={"code":"X100","label":"demo-module"}', - '--siteDir=owner_xyz', - ]); + $result = $this->runWrite([ + 'module={"code":"X100","label":"demo-module"}', + '--siteDir=owner_xyz', + ]); - $this->assertSame(0, $result['code'], $result['output']); - $cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; + $this->assertCfgSuccess($result); + $cfg = $this->loadWrittenConfig('owner_xyz'); $this->assertSame('X100', $cfg['module']['code']); $this->assertSame('demo-module', $cfg['module']['label']); } public function testWriteSupportsNestedPathWithColonNotation(): void { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', + $result = $this->runWrite([ 'module:flags:enabled=true', '--siteDir=owner_xyz', ]); - $this->assertSame(0, $result['code'], $result['output']); - $cfg = require $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; + $this->assertCfgSuccess($result); + $cfg = $this->loadWrittenConfig('owner_xyz'); $this->assertTrue($cfg['module']['flags']['enabled']); } - public function testWriteWithInputFileAndSettingReturnsError(): void + public function testWriteCoercesSimpleStringValueToJsonString(): void { - $inFile = $this->tmpDir.'/extension-in.json'; - file_put_contents($inFile, json_encode(['module' => ['code' => 'X100']])); + $result = $this->runWrite([ + 'module:code=X100', + '--siteDir=owner_xyz', + ]); - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="x"', - '-i', - $inFile, - ]); + $this->assertCfgSuccess($result); + $cfg = $this->loadWrittenConfig('owner_xyz'); + $this->assertSame('X100', $cfg['module']['code']); + } - $this->assertSame(1, $result['code']); - $this->assertStringContainsString('Please use either SETTING or --in, not both.', $result['output']); + public function testWriteRejectsInvalidJsonStringValue(): void + { + $result = $this->runWrite([ + 'module={"code":', + '--siteDir=owner_xyz', + ]); + + $this->assertCfgFailure($result, 'The value does not appear to be a valid JSON string.'); } public function testWriteWithSiteDirCreatesAndWritesSiteConfig(): void { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', + $result = $this->runWrite([ 'module:code="X100"', '--siteDir=owner_xyz', ]); - $this->assertSame(0, $result['code'], $result['output']); + $this->assertCfgSuccess($result); $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; $this->assertFileExists($siteFile); @@ -171,28 +149,19 @@ class CfgTest extends TestCase public function testWriteWithSiteDirMergesIntoExistingSiteConfig(): void { - $firstWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', + $firstWrite = $this->runWrite([ 'module:code="X100"', '--siteDir=owner_xyz', ]); - $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); + $this->assertCfgSuccess($firstWrite); - $secondWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', + $secondWrite = $this->runWrite([ 'module:label="demo-module"', '--siteDir=owner_xyz', ]); - $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); + $this->assertCfgSuccess($secondWrite); - $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; - $cfg = require $siteFile; + $cfg = $this->loadWrittenConfig('owner_xyz'); $this->assertSame('X100', $cfg['module']['code']); $this->assertSame('demo-module', $cfg['module']['label']); @@ -200,56 +169,62 @@ class CfgTest extends TestCase public function testWriteWithSiteDirUsingConfigPrefixIsNormalized(): void { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir=config/owner_xyz', - ]); + $result = $this->runWrite([ + 'module:code="X100"', + '--siteDir=config/owner_xyz', + ]); - $this->assertSame(0, $result['code'], $result['output']); + $this->assertCfgSuccess($result); $this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php'); $this->assertFileDoesNotExist($this->tmpDir.'/config/config/owner_xyz/Extension.conf.php'); } + public function testWriteWithSiteDirUsingAbsoluteConfigPrefixIsNormalized(): void + { + $result = $this->runWrite([ + 'module:code="X100"', + '--siteDir='.$this->tmpDir.'/config/owner_xyz', + ]); + + $this->assertCfgSuccess($result); + $this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php'); + } + + public function testWriteWithNestedSiteDirWritesToNestedSiteConfig(): void + { + $result = $this->runWrite([ + 'module:code="X100"', + '--siteDir=owner_xyz/sub_a', + ]); + + $this->assertCfgSuccess($result); + $this->assertFileExists($this->tmpDir.'/config/owner_xyz/sub_a/Extension.conf.php'); + } + public function testWriteWithSiteDirWritesToSiteConfig(): void { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir=owner_xyz', - ]); + $result = $this->runWrite([ + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); - $this->assertSame(0, $result['code'], $result['output']); + $this->assertCfgSuccess($result); $this->assertFileExists($this->tmpDir.'/config/owner_xyz/Extension.conf.php'); } public function testWriteToExistingSiteConfigCreatesBackup(): void { - $firstWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="first"', - '--siteDir=owner_xyz', - ]); - $this->assertSame(0, $firstWrite['code'], $firstWrite['output']); + $firstWrite = $this->runWrite([ + 'module:code="first"', + '--siteDir=owner_xyz', + ]); + $this->assertCfgSuccess($firstWrite); - $secondWrite = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="second"', - '--siteDir=owner_xyz', - ]); - $this->assertSame(0, $secondWrite['code'], $secondWrite['output']); + $secondWrite = $this->runWrite([ + 'module:code="second"', + '--siteDir=owner_xyz', + ]); + $this->assertCfgSuccess($secondWrite); $siteFile = $this->tmpDir.'/config/owner_xyz/Extension.conf.php'; $backupFile = $siteFile.'.bak'; @@ -260,74 +235,180 @@ class CfgTest extends TestCase $this->assertSame('second', $current['module']['code']); $this->assertSame('first', $backup['module']['code']); } + /* }}} */ + /* Show Flow {{{ */ public function testShowWithSiteReturnsSiteSpecificValue(): void { - $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir=owner_xyz', - ]); + $this->runWrite([ + 'module:code="X100"', + '--siteDir=owner_xyz', + ]); - $show = $this->runCfg([ - '-a', - $this->tmpDir, - 'show', - 'Extension', - 'module:code', - '--siteDir=owner_xyz', - ]); + $show = $this->runShow([ + 'module:code', + '--siteDir=owner_xyz', + ]); - $this->assertSame(0, $show['code'], $show['output']); + $this->assertCfgSuccess($show); $this->assertStringContainsString('X100', $show['output']); } + /* }}} */ - public function testSiteDirRejectsTraversal(): void + /* Validation {{{ */ + public function testInvalidActionReturnsError(): void { $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir=../owner_xyz', - ]); + '-a', + $this->tmpDir, + 'delete', + 'Extension', + ]); - $this->assertSame(1, $result['code']); - $this->assertStringContainsString('Invalid directory in --siteDir', $result['output']); - } - - public function testSiteDirRejectsInvalidSegment(): void - { - $result = $this->runCfg([ - '-a', - $this->tmpDir, - 'write', - 'Extension', - 'module:code="X100"', - '--siteDir=owner xyz', - ]); - - $this->assertSame(1, $result['code']); - $this->assertStringContainsString("Invalid directory name segment 'owner xyz' in --siteDir.", $result['output']); + $this->assertCfgFailure($result, "'delete' not found"); } public function testSiteDirRejectsEmptyValue(): void { - $result = $this->runCfg([ + $result = $this->runWrite([ + 'module:code="X100"', + '--siteDir=', + ]); + + $this->assertCfgFailure($result, 'a value is required for --siteDir'); + } + + /** + * @dataProvider siteDirValidationDataProvider + */ + public function testSiteDirValidation(string $siteDir, string $expectedMessage): void + { + $result = $this->runWrite([ + 'module:code="X100"', + '--siteDir='.$siteDir, + ]); + + $this->assertCfgFailure($result, $expectedMessage); + } + + /** + * @dataProvider inputFileValidationDataProvider + */ + public function testInputFileValidation(?string $fileContents, string $pathSuffix, string $expectedMessage): void + { + $inFile = $this->tmpDir.'/'.$pathSuffix; + if ($fileContents !== null) { + file_put_contents($inFile, $fileContents); + } + + $result = $this->runWrite([ + '-i', + $inFile, + ]); + + $this->assertCfgFailure($result, $expectedMessage); + } + + /** + * @dataProvider writePayloadSourceValidationDataProvider + */ + public function testWritePayloadSourceValidation(array $extraArgs, string $expectedMessage): void + { + $inFile = $this->createJsonInputFile(['module' => ['code' => 'X100']]); + + $args = array_merge( + [ '-a', $this->tmpDir, 'write', 'Extension', - 'module:code="X100"', - '--siteDir=', - ]); + ], + array_map( + fn (string $arg): string => $arg === '__INPUT_FILE__' ? $inFile : $arg, + $extraArgs + ) + ); - $this->assertSame(1, $result['code']); - $this->assertStringContainsString('a value is required for --siteDir', $result['output']); + $result = $this->runCfg($args); + + $this->assertCfgFailure($result, $expectedMessage); + } + + public function siteDirValidationDataProvider(): array + { + return [ + 'traversal' => ['../owner_xyz', 'Invalid directory in --siteDir'], + 'invalid segment' => ['owner xyz', "Invalid directory name segment 'owner xyz' in --siteDir."], + 'hidden dot segment' => ['owner_xyz/./secret', "Invalid directory in --siteDir: 'owner_xyz/./secret'."], + 'config root only' => ['config/', 'Option --siteDir is empty.'], + ]; + } + + public function inputFileValidationDataProvider(): array + { + return [ + 'missing file' => [null, 'missing.json', 'Input file is not readable'], + 'invalid json' => ['{"module":', 'invalid.json', 'Input JSON is invalid'], + 'scalar json' => ['true', 'scalar.json', 'Input JSON must decode to an object/array'], + ]; + } + + public function writePayloadSourceValidationDataProvider(): array + { + return [ + 'missing payload source' => [[], 'Nothing to write: provide SETTING or --in.'], + 'both payload sources' => [['module:code="x"', '-i', '__INPUT_FILE__'], 'Please use either SETTING or --in, not both.'], + ]; + } + /* }}} */ + + /* Helpers {{{ */ + private function runWrite(array $args): array + { + return $this->runCfg(array_merge([ + '-a', + $this->tmpDir, + 'write', + 'Extension', + ], $args)); + } + + private function runShow(array $args): array + { + return $this->runCfg(array_merge([ + '-a', + $this->tmpDir, + 'show', + 'Extension', + ], $args)); + } + + private function createJsonInputFile(array $contents, string $name = 'extension-in.json'): string + { + $path = $this->tmpDir.'/'.$name; + file_put_contents($path, json_encode($contents, JSON_PRETTY_PRINT)); + + return $path; + } + + private function loadWrittenConfig(?string $siteDir = null): array + { + $path = $siteDir === null + ? $this->tmpDir.'/config/Extension.conf.php' + : $this->tmpDir.'/config/'.$siteDir.'/Extension.conf.php'; + + return require $path; + } + + private function assertCfgSuccess(array $result): void + { + $this->assertSame(0, $result['code'], $result['output']); + } + + private function assertCfgFailure(array $result, string $message): void + { + $this->assertSame(1, $result['code'], $result['output']); + $this->assertStringContainsString($message, $result['output']); } private function runCfg(array $args): array @@ -372,4 +453,5 @@ class CfgTest extends TestCase rmdir($dir); } + /* }}} */ } -- 2.52.0 From 6f1f78fc33c34fa240b5d0254b4d3ee9184eb2f5 Mon Sep 17 00:00:00 2001 From: norb Date: Mon, 30 Mar 2026 16:56:15 +0200 Subject: [PATCH 08/11] feat: add shortOption to siteDir --- bin/cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cfg b/bin/cfg index 5c2203c..ac69102 100755 --- a/bin/cfg +++ b/bin/cfg @@ -105,7 +105,7 @@ $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('siteDir') // {{{ + ->add( Input\InputTypeFactory::build('LongOption')->name('siteDir')->short('s') // {{{ ->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED) ->description('Site/instance directory below config/. Accepts owner_xyz or config/owner_xyz') ->validator(new Input\Validator( -- 2.52.0 From f87ada6c15c16fafae08b337c4986aa1e61b1058 Mon Sep 17 00:00:00 2001 From: norb Date: Mon, 30 Mar 2026 16:56:40 +0200 Subject: [PATCH 09/11] fix: remove commented code --- bin/cfg | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/bin/cfg b/bin/cfg index ac69102..7a1e253 100755 --- a/bin/cfg +++ b/bin/cfg @@ -320,16 +320,6 @@ $appPath = $argv->find('appPath'); if (!$appPath) $appPath = getcwd().'/'; $appPath = rtrim($appPath, '/').'/'; -/* $it = new RecursiveDirectoryIterator($appPath); - -foreach(new RecursiveIteratorIterator($it) as $file) -{ - $configDir = $file->getPath(); - if ($file->isDir() && $file->getFilename() == '.' && basename($configDir) == 'config') { - echo "found config dir: $configDir\n"; - } -} - */ $mode = ($argv->find('mode') == '') ? null : $argv->find('mode'); $cfg = (new Settings([], $mode))->appPath($appPath)->prefix($prefix); // pkgPath points to package defaults (e.g. .default.conf.php) -- 2.52.0 From 56872a04c497efdd66a22d234cf0de760328cd80 Mon Sep 17 00:00:00 2001 From: norb Date: Mon, 30 Mar 2026 16:57:58 +0200 Subject: [PATCH 10/11] style: fix indentation --- bin/cfg | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/bin/cfg b/bin/cfg index 7a1e253..0f8f200 100755 --- a/bin/cfg +++ b/bin/cfg @@ -379,21 +379,22 @@ case 'show': echo $out.PHP_EOL; break; - case 'write': - $inputPayload = $argv->find('in'); - if (!$inputPayload && $settings['key'] === '') - { - fwrite(STDERR, 'Nothing to write: provide SETTING or --in.'.PHP_EOL); - exit(1); - } - $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; - verboseLog($verbose, 'write source: '.($inputPayload ? '--in' : 'SETTING')); - if ($inputPayload) - { - $setting2write = $inputPayload; - } - else - { +case 'write': + $inputPayload = $argv->find('in'); + if (!$inputPayload && $settings['key'] === '') + { + fwrite(STDERR, 'Nothing to write: provide SETTING or --in.'.PHP_EOL); + exit(1); + } + $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; + verboseLog($verbose, 'write source: '.($inputPayload ? '--in' : 'SETTING')); + + if ($inputPayload) + { + $setting2write = $inputPayload; + } + else + { $setting2write = $settings['value']; } verboseLog($verbose, 'write path: '.json_encode($path)); -- 2.52.0 From 5fdf1e2b7faff48246af433a144800049c189437 Mon Sep 17 00:00:00 2001 From: norb Date: Mon, 30 Mar 2026 17:04:33 +0200 Subject: [PATCH 11/11] fix: remove Verua specific names using Settings specific --- bin/cfg | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/cfg b/bin/cfg index 0f8f200..3c7c0ef 100755 --- a/bin/cfg +++ b/bin/cfg @@ -261,27 +261,27 @@ $usage = Cli\manpage( basename(__FILE__), $version, .PHP_EOL .'Advance usage:' .PHP_EOL - .'cfg write extension_name \'module:enabled=true\'' + .'cfg write prefix \'module:enabled=true\'' .PHP_EOL .PHP_EOL - .'#Instance write:' + .'#Site specific write:' .PHP_EOL - .'cfg write extension_name \'module:enabled=true\' --siteDir=owner_xyz' + .'cfg write prefix \'module:enabled=true\' --siteDir=owner_xyz' .PHP_EOL .PHP_EOL .'#Batch write from JSON file:' .PHP_EOL - .'cfg write extension_name --siteDir=owner_xyz -i /tmp/extension.json' + .'cfg write prefix --siteDir=owner_xyz -i /tmp/extension.json' .PHP_EOL .PHP_EOL .'#Batch write from JSON string:' .PHP_EOL - .'cfg write extension_name \'module={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz' + .'cfg write prefix \'module={"enabled":true,"timeout":30,"label":"example"}\' --siteDir=owner_xyz' .PHP_EOL .PHP_EOL - .'#Read merged value for an instance:' + .'#Read merged value for a site:' .PHP_EOL - .'cfg show extension_name module:enabled --siteDir=owner_xyz' + .'cfg show prefix module:enabled --siteDir=owner_xyz' .PHP_EOL ] ).PHP_EOL; -- 2.52.0