Skip to content

Commit

Permalink
up: update json,git,str and more tool commands
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 2, 2022
1 parent 27c0160 commit cf10ce9
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 517 deletions.
10 changes: 6 additions & 4 deletions app/Common/CmdRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,11 @@ public function add(string $command, string $key = ''): self
}

/**
* @param array $config
* - command STRING|ARRAY
* - workDir STRING
* - where callable
* @param array $config = [
* 'command' => 'STRING|ARRAY',
* 'workDir' => 'STRING',
* 'where' => 'callable',
* ]
* @param string $key
*
* @return $this
Expand Down Expand Up @@ -254,6 +255,7 @@ public function addByArray(array $config, string $key = ''): self
public function run(bool $printOutput = false): static
{
$this->printOutput = $printOutput;

if ($command = $this->cmdline) {
$this->innerExecute($command, $this->workDir);

Expand Down
4 changes: 2 additions & 2 deletions app/Concern/InitApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use Inhere\Kite\Common\GitAPI\GitHubV3API;
use Inhere\Kite\Common\GitAPI\GitLabV4API;
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Jenkins\JenkinsFactory;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use PhpPkg\Config\ConfigBox;
use PhpPkg\EasyTpl\EasyTemplate;
use PhpPkg\EasyTpl\TextTemplate;
use PhpPkg\JenkinsClient\MultiJenkins;
use Toolkit\Stdlib\Arr\ArrayHelper;
use Toolkit\Stdlib\Obj\ObjectBox;
use Toolkit\Stdlib\OS;
Expand Down Expand Up @@ -129,7 +129,7 @@ protected function registerComServices(ObjectBox $box): void

$box->set('jenkins', function () {
$config = $this->config()->getArray('jenkins');
return new JenkinsFactory($config);
return new MultiJenkins($config);
});
}

Expand Down
5 changes: 3 additions & 2 deletions app/Console/Component/Clipboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Inhere\Kite\Console\Component;

use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj\AbstractObj;
use Toolkit\Stdlib\OS;
use Toolkit\Sys\Exec;
use function addslashes;
use function file_put_contents;
use function tempnam;

/**
Expand Down Expand Up @@ -90,7 +90,8 @@ public function write(string $contents, bool $addSlashes = false): bool
if ($multiLine) {
$file = tempnam(OS::tempDir(), "tmp_");

File::write($contents, $file);
// File::write($contents, $file);
file_put_contents($file, $contents);
$command = "$program < $file";
} else {
$command = "echo $contents | $program";
Expand Down
26 changes: 24 additions & 2 deletions app/Console/Component/ContentsAutoReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ class ContentsAutoReader extends AbstractObj
protected string $srcType = self::TYPE_STRING;

/**
* try read contents
*
* - input empty or '@i' or '@stdin' - will read from STDIN
* - input '@c' or '@cb' or '@clipboard' - will read from Clipboard
* - input '@l' or '@load' - will read from loaded file
* - input '@FILEPATH' - will read from the filepath.
*
* @param string $source
* @param array{print: bool, loadedFile: string, throwOnEmpty: bool} $opts
* @param array $opts = [
* 'print' => true,
* 'throwOnEmpty' => false,
* 'loadedFile' => '',
* 'suffix' => '.json',
* ]
*
* @return string
*/
Expand All @@ -52,7 +64,12 @@ public static function readFrom(string $source, array $opts = []): string
* - input '@FILEPATH' or FILEPATH - will read from the filepath.
*
* @param string $source the input text
* @param array{print: bool, loadedFile: string, throwOnEmpty: bool} $opts
* @param array $opts = [
* 'print' => true,
* 'throwOnEmpty' => false,
* 'loadedFile' => '',
* 'suffix' => '.json',
* ]
*
* @return string
*/
Expand Down Expand Up @@ -88,9 +105,14 @@ public function read(string $source, array $opts = []): string
$print && Cli::info('try read contents from file: ' . $lFile);
$str = File::readAll($lFile);
} else {
$suffix = $opts['suffix'] ?? '';
$filepath = Kite::resolve($source);

// direct path to disk file
if ($filepath[0] === '@') {
$filepath = substr($filepath, 1);
} elseif ($suffix) {
$filepath = File::appendSuffix($filepath, $suffix);
}

if (is_file($filepath)) {
Expand Down
28 changes: 0 additions & 28 deletions app/Console/Component/FlagValCollector.php

This file was deleted.

46 changes: 1 addition & 45 deletions app/Console/Controller/Gitx/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Common\GitLocal\GitLab;
use Inhere\Kite\Console\Component\RedirectToGitGroup;
use Inhere\Kite\Console\SubCmd\Gitflow\BranchCreateCmd;
use Inhere\Kite\Console\SubCmd\Gitflow\UpdateNoPushCmd;
use Inhere\Kite\Console\SubCmd\Gitflow\UpdatePushCmd;
use Inhere\Kite\Console\SubCmd\GitlabCmd\BranchCmd;
use Inhere\Kite\Console\SubCmd\GitlabCmd\BranchDeleteCmd;
use Inhere\Kite\Console\SubCmd\GitlabCmd\MergeRequestCmd;
use Inhere\Kite\Console\SubCmd\GitlabCmd\ProjectCmd;
use Inhere\Kite\Console\SubCmd\GitlabCmd\ResolveConflictCmd;
Expand Down Expand Up @@ -71,8 +69,6 @@ public static function aliases(): array
protected static function commandAliases(): array
{
return [
'deleteBranch' => ['del-br', 'delbr', 'dbr', 'db'],
'newBranch' => ['new-br', 'newbr', 'nbr', 'nb'],
'li' => 'linkInfo',
'cf' => 'config',
'conf' => 'config',
Expand Down Expand Up @@ -402,47 +398,6 @@ public function configCommand(FlagsParser $fs, Output $output): void
$output->success('Complete');
}

/**
* checkout an new branch for development. alias for `br new`
*
* @options
* --nm, --not-main bool;Dont push new branch to the main remote
*
* @arguments
* branch The new branch name. eg: fea_6_12
*
* @param FlagsParser $fs
* @param Output $output
*
* @throws Throwable
*/
public function newBranchCommand(FlagsParser $fs, Output $output): void
{
$bcCmd = new BranchCreateCmd($this->input, $output);
$bcCmd->run($fs->getFlags());
}

/**
* delete branches from local, origin, main remote
*
* @options
* -f, --force bool;Force execute delete command, ignore error
* --nm, --not-main bool;Dont delete branch on the main remote
*
* @arguments
* branches... array;The want deleted branch name(s). eg: fea_6_12;required
*
* @param FlagsParser $fs
* @param Output $output
*
* @throws Throwable
*/
public function deleteBranchCommand(FlagsParser $fs, Output $output): void
{
$bcCmd = new BranchDeleteCmd($this->input, $output);
$bcCmd->run($fs->getFlags());
}

/**
* show gitlab project config information
*
Expand Down Expand Up @@ -663,6 +618,7 @@ public function createCommand(FlagsParser $fs, Output $output): void
}

$run->addf('git remote -v', $name);
$run->addf('git push -u origin master');
$run->addf('git push main master');

// $run->addf('git push -u origin master');
Expand Down
65 changes: 54 additions & 11 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Inhere\Kite\Console\Component\Clipboard;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Generate\JsonToCode;
use Inhere\Kite\Lib\Parser\Text\Json5ItemParser;
Expand Down Expand Up @@ -79,6 +78,16 @@ protected function init(): void
$this->dumpfile = Kite::getTmpPath('json-load.json');
}

/**
* @param int $index
*
* @return string
*/
private function getDumpfile(int $index = 0): string
{
return Kite::getTmpPath("json-load$index.json");
}

protected function jsonRender(): JSONPretty
{
return JSONPretty::new([
Expand All @@ -91,7 +100,7 @@ protected function jsonRender(): JSONPretty
*/
private function loadDumpfileJSON(): void
{
$dumpfile = $this->dumpfile;
$dumpfile = $this->getDumpfile();
if (!$dumpfile || !is_file($dumpfile)) {
throw new InvalidArgumentException("the json temp file '$dumpfile' is not exists");
}
Expand All @@ -108,7 +117,8 @@ private function loadDumpfileJSON(): void
private function autoReadJSON(string $source): void
{
$this->json = ContentsAutoReader::readFrom($source, [
'loadedFile' => $this->dumpfile,
'loadedFile' => $this->getDumpfile(),
'suffix' => '.json',
]);
if (!$this->json) {
throw new InvalidArgumentException('the source json data is empty');
Expand All @@ -120,24 +130,41 @@ private function autoReadJSON(string $source): void
/**
* load json string data from clipboard to an tmp file
*
* @options
* -i, --index int;set the loaded tmp file index number;false;0
*
* @arguments
* source The source. allow: @clipboard, @stdin
* output The output file, default is @tmp/json-load.json
*
* @param FlagsParser $fs
* @param Output $output
*
* @throws Throwable
* @example
* {binWithCmd} @c l1 # will save to @tmp/l1.json
* {binName} json load -s @tmp/l1 $
*/
public function loadCommand(FlagsParser $fs, Output $output): void
{
$json = AppHelper::tryReadContents($fs->getArg('source'));
$ext = '.json';
$json = ContentsAutoReader::readFrom($fs->getArg('source'), [
'suffix' => $ext,
]);
if (!$json) {
throw new InvalidArgumentException('the input data is empty');
}

$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
if ($file = $fs->getArg('output')) {
$file = Kite::getTmpPath($file);
$file = File::appendSuffix($file, $ext);
} else {
$file = $this->dumpfile;
}

File::write(JsonHelper::prettyJSON($data), $this->dumpfile);
$output->info("load temp JSON data to file: $file");
File::write(JsonHelper::prettyJSON($data), $file);

$output->success('Complete');
}
Expand All @@ -150,11 +177,20 @@ public function loadCommand(FlagsParser $fs, Output $output): void
*
* @options
* --type The search type. allow: keys, path
* --nc, --no-color bool;dont render color for output result.
* -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
* -f, --filter array;include filter by input keywords.
* -e, --exclude array;exclude filter by input keywords.
*
* @throws Throwable
* @example
* {binWithCmd} -s @l --nc -e ' 0,' -e '[]' $ # exclude contains ' 0,' '[]' lines.
*
* ## load to custom file and read
* {binName} json load @c l1 # will save to @tmp/l1.json
* {binWithCmd} -s @tmp/l1 $ # read and render from @tmp/l1.json
*/
public function getCommand(FlagsParser $fs, Output $output): void
{
Expand All @@ -164,7 +200,7 @@ public function getCommand(FlagsParser $fs, Output $output): void

$path = $fs->getArg('path');
if ($path = trim($path, ' .$')) {
$ret = Arr::getByPath($this->data, $path);
$ret = Arr::getByPath($this->data, $path);
} else {
$ret = $this->data;
}
Expand All @@ -184,7 +220,12 @@ public function getCommand(FlagsParser $fs, Output $output): void
$ret = array_keys($ret);
}

$str = $this->jsonRender()->renderData($ret);
$render = $this->jsonRender()
->setNoColor($fs->getOpt('no-color'))
->setIncludes($fs->getOpt('filter'))
->setExcludes($fs->getOpt('exclude'));

$str = $render->renderData($ret);
}

$outFile = $fs->getOpt('output');
Expand Down Expand Up @@ -228,14 +269,15 @@ public function searchCommand(FlagsParser $fs, Output $output): void
* --uq, --unquote bool;unquote input string before format.
*
* @arguments
* json The json text line. if empty will try read text from clipboard
* json The json text line. allow: @load, @clipboard, @stdin
* if empty will try read text from clipboard
*
* @throws Throwable
*/
public function prettyCommand(FlagsParser $fs, Output $output): void
{
$json = $fs->getArg('json');
$json = AppHelper::tryReadContents($json, [
$json = ContentsAutoReader::readFrom($json, [
'loadedFile' => $this->dumpfile,
]);

Expand All @@ -253,13 +295,14 @@ public function prettyCommand(FlagsParser $fs, Output $output): void
* collect field and comments from JSON5 contents
*
* @arguments
* json5 The json text line. if empty will try read text from clipboard
* json5 The json text line. allow: @load, @clipboard, @stdin
* if empty will try read text from clipboard
*
*/
public function fieldsCommand(FlagsParser $fs, Output $output): void
{
$json = $fs->getArg('json5');
$json = AppHelper::tryReadContents($json, [
$json = ContentsAutoReader::readFrom($json, [
'loadedFile' => $this->dumpfile,
]);

Expand Down
Loading

0 comments on commit cf10ce9

Please sign in to comment.