From 9c8e75910389e0bd3d1084fa4a4018f363061387 Mon Sep 17 00:00:00 2001 From: Inhere Date: Fri, 28 Oct 2022 16:32:21 +0800 Subject: [PATCH] up: enhance the json get command logic --- app/Console/Controller/JsonController.php | 26 +++++++++++++++++------ app/Console/Controller/PhpController.php | 4 ++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/Console/Controller/JsonController.php b/app/Console/Controller/JsonController.php index bb946bd..ea81b77 100644 --- a/app/Console/Controller/JsonController.php +++ b/app/Console/Controller/JsonController.php @@ -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; @@ -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 */ @@ -161,7 +163,11 @@ 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, '{"')) { @@ -169,9 +175,15 @@ public function getCommand(FlagsParser $fs, Output $output): void $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); } diff --git a/app/Console/Controller/PhpController.php b/app/Console/Controller/PhpController.php index 3fa4d5a..5c832bd 100644 --- a/app/Console/Controller/PhpController.php +++ b/app/Console/Controller/PhpController.php @@ -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(); @@ -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']; }