Skip to content

Commit

Permalink
up: enhance the json get command logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 28, 2022
1 parent 6bb2e0b commit 9c8e759
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 19 additions & 7 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Json;
use function array_filter;
use function array_keys;
use function array_merge;
use function is_file;
use function is_scalar;
Expand Down Expand Up @@ -145,12 +146,13 @@ public function loadCommand(FlagsParser $fs, Output $output): void
* get data by path in the loaded JSON data.
*
* @arguments
* path string;The key path for search get;required
* path string;The key path for search get, use `$` get all;required
*
* @options
* --type The search type. allow: keys, path
* -s, --source The json data source, default read stdin, allow: @load, @clipboard, @stdin
* -o, --output The output, default is stdout, allow: @load, @clipboard, @stdout
* --type The search type. allow: keys, path
* -s, --source The json data source, default read stdin, allow: @load, @clipboard, @stdin
* -o, --output The output, default is stdout, allow: @load, @clipboard, @stdout
* --tk, --top-keys bool;only output all top key names
*
* @throws Throwable
*/
Expand All @@ -161,17 +163,27 @@ public function getCommand(FlagsParser $fs, Output $output): void
$this->autoReadJSON($source);

$path = $fs->getArg('path');
$ret = Arr::getByPath($this->data, $path);
if ($path = trim($path, ' .$')) {
$ret = Arr::getByPath($this->data, $path);
} else {
$ret = $this->data;
}

// is json string
if (is_string($ret) && str_starts_with($ret, '{"')) {
$output->info("find '$path' value is JSON string, auto decode");
$ret = Json::decode($ret, true);
}

if (!$ret || is_scalar($ret)) {
$str = $ret;
if ($ret === null) {
$str = 'NULL';
} elseif (is_scalar($ret)) {
$str = (string)$ret;
} else {
if ($fs->getOpt('top-keys')) {
$ret = array_keys($ret);
}

$str = $this->jsonRender()->renderData($ret);
}

Expand Down
4 changes: 2 additions & 2 deletions app/Console/Controller/PhpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public function pkgNewCommand(FlagsParser $fs, Output $output): void

Cmd::new('git')
->add('clone')
->addf('%s/%s', GitHub::GITHUB_HOST, $repoPath)
->addf('%s/%s', GitHub::HOST, $repoPath)
->add($pkgName)
->runAndPrint();

Expand Down Expand Up @@ -549,7 +549,7 @@ public function ghPkgCommand(FlagsParser $fs, Output $output): void
$pkgName = $composerInfo['name'];
}

$homepage = GitHub::GITHUB_HOST . "/$pkgName";
$homepage = GitHub::HOST . "/$pkgName";
if (!empty($composerInfo['homepage'])) {
$homepage = $composerInfo['homepage'];
}
Expand Down

0 comments on commit 9c8e759

Please sign in to comment.