diff --git a/bin/cfg b/bin/cfg index 14380b0..0ec463c 100755 --- a/bin/cfg +++ b/bin/cfg @@ -24,7 +24,7 @@ foreach ($autoloadFiles as $autoloadFile) { $version = '0.1 beta'; -$actions = [ 'show', 'write' ]; +$actions = [ 'show', 'write', 'help' ]; $settings = ['key' => '', 'value' => '']; /* Flags and Argument configuration {{{*/ @@ -115,7 +115,6 @@ $collection = (new Input\InputCollection()) if (! (in_array($value, $specialValues) || is_numeric($value) || strpbrk($value, '[{":') !== false )) { $value = "\"$value\""; } - var_dump($value); try { $settings['value'] = json_decode($value, true, 512, JSON_THROW_ON_ERROR); } catch (\JsonException $ex) { @@ -140,7 +139,7 @@ $usage = Cli\manpage( basename(__FILE__), $version, 'cfg show VeruA db:host'.PHP_EOL. 'cfg write VeruA \'db:host="newHost"\''.PHP_EOL ] -).PHP_EOL.PHP_EOL; +).PHP_EOL; // Get the supplied input. Passing the collection will make the handler bind values // and validate the input according to our collection @@ -148,11 +147,16 @@ try { $argv = Input\InputHandlerFactory::build('Argv', $collection); } catch (\Exception $ex) { echo $usage; + + if ($argv[1] == '-h' || $argv[1] == '--help' || $argv[1] == 'help') { + exit(0); + } echo $ex->getMessage().PHP_EOL; exit(1); } -if ($argv->find( 'help' ) || $argc <= 2) +// show help +if ($argv->find( 'help' ) || $argv->find('action') == 'help') { echo $usage; exit(0); @@ -183,7 +187,13 @@ foreach(new RecursiveIteratorIterator($it) as $file) */ $cfg = (new Settings())->appPath($appPath)->prefix($prefix); if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath($pkgPath); -$cfg->load(); + +if (is_readable($cfg->buildFileName('default'))) { + $cfg->load(); +} +elseif (is_readable($cfgFile = $cfg->buildFileName())) { + $cfg->load(require($cfgFile)); +} //var_dump($cfg); $result = $cfg; @@ -209,11 +219,12 @@ if ($result instanceof Settings) $result = $result->toArray(); switch ($argv->find('action')) { case 'show': - echo json_encode($result, JSON_PRETTY_PRINT); + echo json_encode($result, JSON_PRETTY_PRINT).PHP_EOL; break; case 'write': - $path = explode(':', $settings['key']); + $path = ($settings['key'] !== '') ? explode(':', $settings['key']) : []; + var_dump($path); $setting2write = $settings['value']; @@ -224,11 +235,17 @@ case 'write': if (is_readable($file = $cfg->buildFileName())) { $setting2write = array_replace_recursive(require($file), $setting2write); + copy($file, "$file.bak"); } $writeCfg = $cfg->create($setting2write); // var_dump($writeCfg->toArray()); - copy($file, "$file.bak"); - (new SettingsWriter($writeCfg))->write(); + try { + (new SettingsWriter($writeCfg))->write(); + echo "Written modified settings to: $file".PHP_EOL; + } + catch (\Exception $e) { + echo $e->getMessage().PHP_EOL; + } break; }