ObjectCommand: support JSON via STDIN

fixes #1570
This commit is contained in:
Thomas Gelf 2022-07-20 06:37:06 +02:00
parent 2821b0721d
commit 9775922975
3 changed files with 31 additions and 0 deletions

View File

@ -75,6 +75,12 @@ icingacli director host create localhost \
--json '{ "address": "127.0.0.1", "vars": { "test": [ "one", "two" ] } }'
```
Passing JSON via STDIN is also possible:
```shell
icingacli director host create localhost --json < my-host.json
```
### Delete a specific object

View File

@ -27,6 +27,7 @@ v1.10.0 (unreleased)
### CLI
* FIX: config deploy doesn't try to wait in case of no deployment (#2522)
* FEATURE: improved wording for deployment error messages (#2523)
* FEATURE: JSON can now be shipped via STDIN (#1570)
1.9.1
-----

View File

@ -441,12 +441,36 @@ class ObjectCommand extends Command
protected function remainingParams()
{
if ($json = $this->params->shift('json')) {
if ($json === true) {
$json = $this->readFromStdin();
if ($json === null) {
$this->fail('Please pass JSON either via STDIN or via --json');
}
}
return (array) $this->parseJson($json);
} else {
return $this->params->getParams();
}
}
protected function readFromStdin()
{
if (!defined('STDIN')) {
define('STDIN', fopen("php://stdin","r"));
}
$inputIsTty = function_exists('posix_isatty') && posix_isatty(STDIN);
if ($inputIsTty) {
return null;
}
$stdin = file_get_contents('php://stdin');
if (strlen($stdin) === 0) {
return null;
}
return $stdin;
}
protected function exists($name)
{
return IcingaObject::existsByType(