Skip to content

Commit

Permalink
ObjectCommand: support JSON via STDIN
Browse files Browse the repository at this point in the history
fixes #1570
  • Loading branch information
Thomas-Gelf committed Jul 20, 2022
1 parent 2821b07 commit 9775922
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/60-CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions doc/82-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
24 changes: 24 additions & 0 deletions library/Director/Cli/ObjectCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,36 @@ protected function shiftOneOrMoreNames()
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(
Expand Down

0 comments on commit 9775922

Please sign in to comment.