Skip to content

Commit

Permalink
up: update some git commands and tool commands
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 26, 2022
1 parent 7c4b74d commit e327ea5
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ alias kg="kite git"
alias kgit="kite git"
alias kgl="kite gitlab"
alias kgh="kite github"
alias kjson="kite json"
```

## 使用说明
Expand Down
4 changes: 3 additions & 1 deletion app/Console/Controller/GenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Inhere\Console\Exception\PromptException;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\SubCmd\ToolCmd\HashCommand;
use Inhere\Kite\Console\SubCmd\ToolCmd\RandomCommand;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Kite;
use PhpPkg\EasyTpl\TextTemplate;
Expand Down Expand Up @@ -43,14 +44,15 @@ protected static function commandAliases(): array
return [
'rpt' => 'repeat',
'tpl' => 'template',
'random' => ['rdm', 'rand'],
'random' => RandomCommand::aliases(),
];
}

protected function subCommands(): array
{
return [
HashCommand::class,
RandomCommand::class,
];
}
/**
Expand Down
18 changes: 16 additions & 2 deletions app/Console/Controller/JsonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Arr;
use Toolkit\Stdlib\Helper\JsonHelper;
use Toolkit\Stdlib\Json;
use function array_filter;
use function array_merge;
use function is_file;
use function is_scalar;
use function is_string;
use function json_decode;
use function str_contains;
use function str_replace;
use function str_starts_with;
use function stripslashes;
use function trim;
use const JSON_THROW_ON_ERROR;

Expand Down Expand Up @@ -153,11 +157,18 @@ public function loadCommand(FlagsParser $fs, Output $output): void
public function getCommand(FlagsParser $fs, Output $output): void
{
$source = $fs->getOpt('source');
// read and decode JSON
$this->autoReadJSON($source);

$path = $fs->getArg('path');
$ret = Arr::getByPath($this->data, $path);

// 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;
} else {
Expand Down Expand Up @@ -201,6 +212,9 @@ public function searchCommand(FlagsParser $fs, Output $output): void
/**
* pretty and format JSON text.
*
* @options
* --uq, --unquote bool;unquote input string before format.
*
* @arguments
* json The json text line. if empty will try read text from clipboard
*
Expand All @@ -213,8 +227,8 @@ public function prettyCommand(FlagsParser $fs, Output $output): void
'loadedFile' => $this->dumpfile,
]);

if (!$json) {
throw new InvalidArgumentException('please input json text for pretty');
if ($fs->getOpt('unquote')) {
$json = stripslashes($json);
}

// $data = json_decode($json, true);
Expand Down
5 changes: 4 additions & 1 deletion app/Console/Controller/SqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected static function commandAliases(): array
* @options
* -s,--source string;The source code for convert. allow: FILEPATH, @clipboard;true
* -o,--output The output target. default is stdout.
* -l,--lang The output language. allow:zh-CN,en;false;zh-CN
*
* @param FlagsParser $fs
* @param Output $output
Expand All @@ -62,7 +63,9 @@ public function mdCommand(FlagsParser $fs, Output $output): void
throw new InvalidArgumentException('empty source code for convert');
}

$md = DBTable::fromSchemeSQL($source)->toMDTable();
$md = DBTable::fromSchemeSQL($source)
->setLang($fs->getOpt('lang'))
->toMDTable();
$output->writeRaw($md);
// $cm = new CliMarkdown();
// $output->println($cm->parse($md));
Expand Down
16 changes: 14 additions & 2 deletions app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Inhere\Kite\Console\Component\ContentsAutoWriter;
use Inhere\Kite\Console\SubCmd\ParseUrlQueryCmd;
use Inhere\Kite\Console\SubCmd\ToolCmd\HashCommand;
use Inhere\Kite\Console\SubCmd\ToolCmd\RandomCommand;
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\KiteUtil;
use Inhere\Kite\Kite;
Expand Down Expand Up @@ -74,6 +75,7 @@ protected static function commandAliases(): array
'process' => ['p', 'filter', 'f'],
'replace' => ['r'],
'parse' => ['fields'],
'random' => RandomCommand::aliases(),
'dequery' => ParseUrlQueryCmd::aliases(),
];
}
Expand All @@ -90,6 +92,7 @@ protected function subCommands(): array
return [
ParseUrlQueryCmd::class,
HashCommand::class,
RandomCommand::class,
];
}

Expand Down Expand Up @@ -187,6 +190,7 @@ public function joinCommand(FlagsParser $fs): void
*
* @example
*
* {binWithCmd} -s '\n' @c
* {binWithCmd} --sep ',' --join-sep ',' -f "wrap:' " 'tom,john' # Output: 'tom', 'john'
*/
public function splitCommand(FlagsParser $fs): void
Expand Down Expand Up @@ -385,8 +389,9 @@ public function processCommand(FlagsParser $fs, Output $output): void
* input '@FILEPATH' - will read from the filepath
*
* @options
* -f, --from The replace from char
* -t, --to The replace to chars
* -f, -s, --from The replace from chars
* -t, --to The replace to chars
* --rm, --remove array;Want remove some chars, allow multi
*
* @param FlagsParser $fs
* @param Output $output
Expand All @@ -396,13 +401,20 @@ public function replaceCommand(FlagsParser $fs, Output $output): void
$text = trim($fs->getArg('text'));
$text = ContentsAutoReader::readFrom($text);

if ($rmList = $fs->getOpt('remove')) {
$output->writeRaw(str_replace($rmList, '', $text));
return;
}

$from = $fs->getOpt('from');
$to = $fs->getOpt('to');
if (!$from && !$to) {
$output->warning('the from and to cannot all empty');
return;
}

$from = TextParser::resolveSep($from);
$to = TextParser::resolveSep($to);
$output->writeRaw(str_replace($from, $to, $text));
}

Expand Down
2 changes: 2 additions & 0 deletions app/Console/SubCmd/GitlabCmd/ResolveConflictCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ protected function execute(Input $input, Output $output)
$runner->setDryRun($dryRun);
$runner->add('git fetch');
$runner->addf('git checkout %s', $branch);
$runner->addf('git push -u %s', $gl->getForkRemote());

// git checkout --track origin/BRANCH
// $runner->addf('git checkout --track %s/%s', $orgRemote, $branch);
$runner->addf('git pull');
Expand Down
79 changes: 79 additions & 0 deletions app/Console/SubCmd/ToolCmd/RandomCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\SubCmd\ToolCmd;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\Component\ContentsAutoReader;
use Inhere\Kite\Helper\AppHelper;
use function hash;
use function md5;
use function strtolower;
use function strtoupper;

/**
* class RandomCommand
*
* @author inhere
*/
class RandomCommand extends Command
{
protected static string $name = 'random';
protected static string $desc = 'generate an random string';

public static function aliases(): array
{
return ['rdm', 'rand'];
}

protected function configure(): void
{
// $this->flags->addOptByRule($name, $rule);
// $this->flags->addArg('str', 'want to signed string. allow: @c', 'string', true);
// $this->flags->addOpt('algo', 'a', 'Name of selected hashing algorithm. eg: md5, sha256', 'string', false, 'md5');
// $this->flags->addOpt(
// 'key', 'k',
// 'Shared secret key used for generating the HMAC variant of the message digest.',
// 'string', true);
}

/**
*
* @options
* -l, --length int;The generate string length;;15
* -n, --number int;The number of generated strings. default: 1
* -t, --template The sample template name.
* allow: alpha/a, alpha_num/an, alpha_num_up/anu, alpha_num_c/anc
*
* @param Input $input
* @param Output $output
* @return void
*/
protected function execute(Input $input, Output $output)
{
$fs = $this->flags;

$length = $fs->getOpt('length', 15);
$number = $fs->getOpt('number', 1);

if ($number < 1 || $number > 20) {
$number = 1;
}

$sname = $fs->getOpt('template', 'alpha_num');

if ($number === 1) {
$str = AppHelper::genRandomStr($sname, $length);
$output->info('Generated: ' . $str);
return;
}

$list = ['Generated:'];
for ($i = 0; $i < $number; $i++) {
$list[] = AppHelper::genRandomStr($sname, $length);
}

$output->info($list);
}
}
8 changes: 8 additions & 0 deletions app/Helper/AppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,21 @@ public static function openBrowser(string $pageUrl): void
*/
public static function genRandomStr(string $sname, int $length): string
{
$aliases = [
'a' => 'alpha',
'an' => 'alpha_num',
'anu' => 'alpha_num_up',
'anc' => 'alpha_num_c',
];

$samples = [
'alpha' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
'alpha_num' => '0123456789abcdefghijklmnopqrstuvwxyz',
'alpha_num_up' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
'alpha_num_c' => '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-+!@#$%&*',
];

$sname = $aliases[$sname] ?? $sname;
$chars = $samples[$sname] ?? $samples['alpha_num'];

$str = '';
Expand Down

0 comments on commit e327ea5

Please sign in to comment.