implement beta version of cli

relates to #2268
This commit is contained in:
Norbert Wagner 2021-05-04 17:55:29 +02:00
parent fc7579b5e6
commit ab736148c0
3 changed files with 730 additions and 3 deletions

239
bin/cfg Executable file
View file

@ -0,0 +1,239 @@
#!/usr/bin/env php
<?php
use rabe\Util\Settings;
use rabe\Util\SettingsWriter;
use pointybeard\Helpers\Cli\Input;
use pointybeard\Helpers\Cli\Input\AbstractInputType;
use pointybeard\Helpers\Cli\Input\AbstractInputHandler;
use pointybeard\Helpers\Cli\Colour\Colour;
use pointybeard\Helpers\Functions\Cli;
$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php'
];
foreach ($autoloadFiles as $autoloadFile) {
if (file_exists($autoloadFile)) {
require_once $autoloadFile;
break;
}
}
$version = '0.1 beta';
$actions = [ 'show', 'write' ];
$settings = ['key' => '', 'value' => ''];
/* Flags and Argument configuration {{{*/
$collection = (new Input\InputCollection())
->add( Input\InputTypeFactory::build('LongOption')->name('help')->short('h') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL)
->description('Display help text')
) // }}}
->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')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('appPath')->short('a') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Path where the config/ directory for the conf files is located, defaults to the working dir')
) // }}}
->add( Input\InputTypeFactory::build('LongOption')->name('pkgPath')->short('p') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL | Input\AbstractInputType::FLAG_VALUE_REQUIRED)
->description('Path where the config/ directory of the package conf files is located, defaults to the working dir')
) // }}}
->add( Input\InputTypeFactory::build('Argument')->name('action') // {{{
->flags(AbstractInputType::FLAG_REQUIRED)
->description(
'one of:'
."\n\t".'show reads configurations'
."\n\t".'write writes configurations'
)
->validator(new Input\Validator(
function (AbstractInputType $input, AbstractInputHandler $context) use ($actions)
{
$action = $context->find('action');
if ( !in_array($action, $actions) ) {
throw new \Exception("'$action' not found");
}
return $action;
}
))
) // }}}
->add( Input\InputTypeFactory::build('Argument')->name('prefix') // {{{
->flags(AbstractInputType::FLAG_REQUIRED)
->description(
'the settings prefix'
)
/* ->validator(new Input\Validator(
function (AbstractInputType $input, AbstractInputHandler $context) use ($actions)
{
$action = $context->find('action');
if ( !in_array($action, $actions) {
throw new \Exception("'$action' not found");
}
return $action;
}
))
*/
) // }}}
->add( Input\InputTypeFactory::build('Argument')->name('setting') // {{{
->flags(AbstractInputType::FLAG_OPTIONAL)
->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');
$setting = explode('=', $setting);
$settings['key'] = $setting[0];
if ($action === 'write')
{
$value = $setting[1];
if (! (isset($value) || $context->find('data'))) {
throw new \Exception('You need a value to write');
}
$specialValues = [ 'true', 'false', 'null' ];
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) {
throw new \Exception( sprintf(
'The value does not appear to be a valid JSON string. Returned: %s: %s',
$ex->getCode(), $ex->getMessage()
));
}
}
return $settings;
}
))
) // }}}
; // }}}
/* Parse Input, usage, unkown flags, Help {{{*/
$usage = Cli\manpage( basename(__FILE__), $version,
'read write settings',
$collection, Colour::FG_GREEN, Colour::FG_WHITE,
[
'Examples' =>
'cfg show VeruA db:host'.PHP_EOL.
'cfg write VeruA \'db:host="newHost"\''.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
try {
$argv = Input\InputHandlerFactory::build('Argv', $collection);
} catch (\Exception $ex) {
echo $usage;
echo $ex->getMessage().PHP_EOL;
exit(1);
}
if ($argv->find( 'help' ) || $argc <= 2)
{
echo $usage;
exit(0);
}
//}}}
$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);
$appPath = $argv->find('appPath');
if (!$appPath) $appPath = getcwd().'/';
/* $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";
}
}
*/
$cfg = (new Settings())->appPath($appPath)->prefix($prefix);
if ($pkgPath = $argv->find('pkgPath')) $cfg->pkgPath($pkgPath);
$cfg->load();
//var_dump($cfg);
$result = $cfg;
$settings = $argv->find('setting') ?? $settings;
if ($settings['key'])
{
foreach (explode(':', $settings['key']) as $setting)
{
try {
$result = $result->{$setting};
}
catch (\OutOfRangeException $e) {
echo $e->getMessage().PHP_EOL;
exit(1);
}
}
}
else $result = $cfg;
if ($result instanceof Settings) $result = $result->toArray();
switch ($argv->find('action'))
{
case 'show':
echo json_encode($result, JSON_PRETTY_PRINT);
break;
case 'write':
$path = explode(':', $settings['key']);
$setting2write = $settings['value'];
while ( ! empty($path))
{
$setting2write = [array_pop($path) => $setting2write];
}
if (is_readable($file = $cfg->buildFileName()))
{
$setting2write = array_replace_recursive(require($file), $setting2write);
}
$writeCfg = $cfg->create($setting2write);
// var_dump($writeCfg->toArray());
copy($file, "$file.bak");
(new SettingsWriter($writeCfg))->write();
break;
}
/* jEdit buffer local properties {{{
* :folding=explicit:collapseFolds=1:
}}}*/

View file

@ -13,7 +13,10 @@
"rabe\\Util\\": "src/" "rabe\\Util\\": "src/"
} }
}, },
"require": {}, "require": {
"pointybeard/helpers-cli-input": "1.2.2.2",
"pointybeard/helpers-functions-cli": "1.1.9.1"
},
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.5" "phpunit/phpunit": "^9.5"
} }

489
composer.lock generated
View file

@ -4,8 +4,493 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "e0910c4bfa347b6a6f7cd72a8a9ac489", "content-hash": "b2c9a2ed87a993de5b521845c4a000a1",
"packages": [], "packages": [
{
"name": "pointybeard/helpers-cli-colour",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-cli-colour.git",
"reference": "04f7070d6a6c70d64208b15ff1db128f1114b7e6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-cli-colour/zipball/04f7070d6a6c70d64208b15ff1db128f1114b7e6",
"reference": "04f7070d6a6c70d64208b15ff1db128f1114b7e6",
"shasum": ""
},
"require": {
"php": ">=5.6.6"
},
"require-dev": {
"block8/php-docblock-checker": "~1.10",
"phpunit/phpunit": "^5"
},
"type": "library",
"autoload": {
"psr-4": {
"pointybeard\\Helpers\\Cli\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "Provides colour constants and a simple way to colourise strings for use on the command-line.",
"homepage": "https://github.com/pointybeard/helpers-cli-colour",
"support": {
"issues": "https://github.com/pointybeard/helpers-cli-colour/issues",
"source": "https://github.com/pointybeard/helpers-cli-colour/tree/master",
"wiki": "https://github.com/pointybeard/helpers-cli-colour/wiki"
},
"time": "2019-05-05T10:11:59+00:00"
},
{
"name": "pointybeard/helpers-cli-input",
"version": "1.2.2.2",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-cli-input.git",
"reference": "9622c9825868cf4ad429ed7cd531175c02bdff94"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-cli-input/zipball/9622c9825868cf4ad429ed7cd531175c02bdff94",
"reference": "9622c9825868cf4ad429ed7cd531175c02bdff94",
"shasum": ""
},
"require": {
"php": ">=7.2",
"pointybeard/helpers-foundation-factory": "~1.0",
"pointybeard/helpers-functions-flags": "~1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"pointybeard\\Helpers\\Cli\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "Collection of classes for handling argv (and other) input when calling command-line scripts. Helps with parsing, collecting and validating arguments, options, and flags.",
"homepage": "https://github.com/pointybeard/helpers-cli-input",
"support": {
"issues": "https://github.com/pointybeard/helpers-cli-input/issues",
"source": "https://github.com/pointybeard/helpers-cli-input/tree/1.2.2.2",
"wiki": "https://github.com/pointybeard/helpers-cli-input/wiki"
},
"time": "2019-11-28T03:33:52+00:00"
},
{
"name": "pointybeard/helpers-exceptions-readabletrace",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-exceptions-readabletrace.git",
"reference": "d41d5bf8885a0f59e4953db7f72b5199733c1304"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-exceptions-readabletrace/zipball/d41d5bf8885a0f59e4953db7f72b5199733c1304",
"reference": "d41d5bf8885a0f59e4953db7f72b5199733c1304",
"shasum": ""
},
"require": {
"php": ">=7.2",
"pointybeard/helpers-functions-debug": "~1",
"pointybeard/helpers-functions-paths": "~1",
"pointybeard/helpers-functions-strings": "~1"
},
"require-dev": {
"phpunit/phpunit": "^8"
},
"type": "library",
"autoload": {
"psr-4": {
"pointybeard\\Helpers\\Exceptions\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "Provides an exception base class that includes method getReadableTrace(), giving simple access to a plain-text, readable, backtrace string.",
"homepage": "https://github.com/pointybeard/helpers-exceptions-readabletrace",
"support": {
"issues": "https://github.com/pointybeard/helpers-exceptions-readabletrace/issues",
"source": "https://github.com/pointybeard/helpers-exceptions-readabletrace/tree/1.0.2",
"wiki": "https://github.com/pointybeard/helpers-exceptions-readabletrace/wiki"
},
"time": "2019-05-26T10:02:32+00:00"
},
{
"name": "pointybeard/helpers-foundation-factory",
"version": "1.0.4",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-foundation-factory.git",
"reference": "852b887002f81b1f95aadcff0df14ccdbc586466"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-foundation-factory/zipball/852b887002f81b1f95aadcff0df14ccdbc586466",
"reference": "852b887002f81b1f95aadcff0df14ccdbc586466",
"shasum": ""
},
"require": {
"php": ">=7.2",
"pointybeard/helpers-functions-strings": "~1.1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"pointybeard\\Helpers\\Foundation\\": "src/"
},
"files": [
"src/Factory/Factory.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "Provides foundation factory classes and factory design pattern functionality",
"homepage": "https://github.com/pointybeard/helpers-foundation-factory",
"support": {
"issues": "https://github.com/pointybeard/helpers-foundation-factory/issues",
"source": "https://github.com/pointybeard/helpers-foundation-factory/tree/1.0.4",
"wiki": "https://github.com/pointybeard/helpers-foundation-factory/wiki"
},
"time": "2019-06-05T12:27:19+00:00"
},
{
"name": "pointybeard/helpers-functions-arrays",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-functions-arrays.git",
"reference": "f17675bd4ed9a27d47a5f085ced0bdd3cc295990"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-functions-arrays/zipball/f17675bd4ed9a27d47a5f085ced0bdd3cc295990",
"reference": "f17675bd4ed9a27d47a5f085ced0bdd3cc295990",
"shasum": ""
},
"require": {
"php": ">=5.6.6"
},
"require-dev": {
"block8/php-docblock-checker": "~1.10",
"phpunit/phpunit": "^5"
},
"type": "library",
"autoload": {
"psr-4": {
"pointybeard\\Helpers\\Functions\\": "src/"
},
"files": [
"src/Arrays/Arrays.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "A collection of helpful functions related to arrays and array manipulation",
"homepage": "https://github.com/pointybeard/helpers-functions-arrays",
"support": {
"issues": "https://github.com/pointybeard/helpers-functions-arrays/issues",
"source": "https://github.com/pointybeard/helpers-functions-arrays/tree/1.0.1",
"wiki": "https://github.com/pointybeard/helpers-functions-arrays/wiki"
},
"time": "2019-05-12T04:20:41+00:00"
},
{
"name": "pointybeard/helpers-functions-cli",
"version": "1.1.9.1",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-functions-cli.git",
"reference": "630efc99d6b7cb6df8333cf0b569c6d4c2bf16d6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-functions-cli/zipball/630efc99d6b7cb6df8333cf0b569c6d4c2bf16d6",
"reference": "630efc99d6b7cb6df8333cf0b569c6d4c2bf16d6",
"shasum": ""
},
"require": {
"php": ">=7.2",
"pointybeard/helpers-cli-colour": "~1.0",
"pointybeard/helpers-cli-input": "~1.2.0",
"pointybeard/helpers-exceptions-readabletrace": "~1.0.0",
"pointybeard/helpers-functions-arrays": "~1.0",
"pointybeard/helpers-functions-debug": "~1.0",
"pointybeard/helpers-functions-flags": "~1.0",
"pointybeard/helpers-functions-strings": "~1.1"
},
"type": "library",
"autoload": {
"psr-4": {
"pointybeard\\Helpers\\Functions\\": "src/"
},
"files": [
"src/Cli/Cli.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "A collection of functions relating to the command-line",
"homepage": "https://github.com/pointybeard/helpers-functions-cli",
"support": {
"issues": "https://github.com/pointybeard/helpers-functions-cli/issues",
"source": "https://github.com/pointybeard/helpers-functions-cli/tree/1.1.9.1",
"wiki": "https://github.com/pointybeard/helpers-functions-cli/wiki"
},
"time": "2020-04-11T08:51:09+00:00"
},
{
"name": "pointybeard/helpers-functions-debug",
"version": "1.0.1.2",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-functions-debug.git",
"reference": "3562931ffebcb8a6306b3636dc76048445cb5a53"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-functions-debug/zipball/3562931ffebcb8a6306b3636dc76048445cb5a53",
"reference": "3562931ffebcb8a6306b3636dc76048445cb5a53",
"shasum": ""
},
"require": {
"php": ">=7.2",
"pointybeard/helpers-functions-paths": "~1",
"pointybeard/helpers-functions-strings": "~1"
},
"require-dev": {
"phpunit/phpunit": "^8"
},
"type": "library",
"autoload": {
"files": [
"src/Debug/Debug.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "A collection of helpful functions to assist with debugging",
"homepage": "https://github.com/pointybeard/helpers-functions-debug",
"support": {
"issues": "https://github.com/pointybeard/helpers-functions-debug/issues",
"source": "https://github.com/pointybeard/helpers-functions-debug/tree/1.0.1.2",
"wiki": "https://github.com/pointybeard/helpers-functions-debug/wiki"
},
"time": "2020-11-03T10:57:00+00:00"
},
{
"name": "pointybeard/helpers-functions-flags",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-functions-flags.git",
"reference": "f336e542e97bb544ef686b94f41496aab76d09ed"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-functions-flags/zipball/f336e542e97bb544ef686b94f41496aab76d09ed",
"reference": "f336e542e97bb544ef686b94f41496aab76d09ed",
"shasum": ""
},
"require": {
"php": ">=5.6.6"
},
"require-dev": {
"block8/php-docblock-checker": "~1.10",
"phpunit/phpunit": "^5"
},
"type": "library",
"autoload": {
"files": [
"src/Flags/Flags.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "A collection of functions for handling bitwise flags",
"homepage": "https://github.com/pointybeard/helpers-functions-flags",
"support": {
"issues": "https://github.com/pointybeard/helpers-functions-flags/issues",
"source": "https://github.com/pointybeard/helpers-functions-flags/tree/1.0.0",
"wiki": "https://github.com/pointybeard/helpers-functions-flags/wiki"
},
"time": "2019-05-07T22:31:38+00:00"
},
{
"name": "pointybeard/helpers-functions-paths",
"version": "1.1.0.1",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-functions-paths.git",
"reference": "262dc45d3b531ff2f1e529f56e0ab1715a22159c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-functions-paths/zipball/262dc45d3b531ff2f1e529f56e0ab1715a22159c",
"reference": "262dc45d3b531ff2f1e529f56e0ab1715a22159c",
"shasum": ""
},
"require": {
"php": ">=7.4"
},
"type": "library",
"autoload": {
"psr-4": {
"pointybeard\\Helpers\\Functions\\": "src/"
},
"files": [
"src/Paths/Paths.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "A collection of helpful functions related to paths, directories, and files names",
"homepage": "https://github.com/pointybeard/helpers-functions-paths",
"support": {
"issues": "https://github.com/pointybeard/helpers-functions-paths/issues",
"source": "https://github.com/pointybeard/helpers-functions-paths/tree/1.1.0.1",
"wiki": "https://github.com/pointybeard/helpers-functions-paths/wiki"
},
"time": "2020-11-10T13:13:10+00:00"
},
{
"name": "pointybeard/helpers-functions-strings",
"version": "1.1.3",
"source": {
"type": "git",
"url": "https://github.com/pointybeard/helpers-functions-strings.git",
"reference": "ded9468690fb023a1f057562dc5223f365f035f9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/pointybeard/helpers-functions-strings/zipball/ded9468690fb023a1f057562dc5223f365f035f9",
"reference": "ded9468690fb023a1f057562dc5223f365f035f9",
"shasum": ""
},
"require": {
"php": ">=7.2"
},
"type": "library",
"autoload": {
"files": [
"src/Strings/Strings.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Alannah Kearney",
"email": "hi@alannahkearney.com",
"homepage": "http://alannahkearney.com",
"role": "Developer"
}
],
"description": "A collection of functions for manipulating strings",
"homepage": "https://github.com/pointybeard/helpers-functions-strings",
"support": {
"issues": "https://github.com/pointybeard/helpers-functions-strings/issues",
"source": "https://github.com/pointybeard/helpers-functions-strings/tree/master"
},
"time": "2020-04-09T10:15:14+00:00"
}
],
"packages-dev": [ "packages-dev": [
{ {
"name": "doctrine/instantiator", "name": "doctrine/instantiator",