From 4e6525d584e705b8953c4026efc4f4537c84afbd Mon Sep 17 00:00:00 2001 From: Inhere Date: Sun, 30 Oct 2022 22:17:21 +0800 Subject: [PATCH] up: move ts2date to a alone command class, and attach to str and conv group --- app/Console/Controller/ConvertController.php | 81 +++-------------- app/Console/Controller/StringController.php | 21 +++-- app/Console/SubCmd/ConvCmd/Ts2dateCmd.php | 93 ++++++++++++++++++++ app/Console/SubCmd/Gitflow/UpdatePushCmd.php | 4 +- 4 files changed, 122 insertions(+), 77 deletions(-) create mode 100644 app/Console/SubCmd/ConvCmd/Ts2dateCmd.php diff --git a/app/Console/Controller/ConvertController.php b/app/Console/Controller/ConvertController.php index bc571a2..2f2f5a5 100644 --- a/app/Console/Controller/ConvertController.php +++ b/app/Console/Controller/ConvertController.php @@ -16,6 +16,7 @@ use Inhere\Kite\Console\Component\Clipboard; use Inhere\Kite\Console\Component\ContentsAutoReader; use Inhere\Kite\Console\Component\ContentsAutoWriter; +use Inhere\Kite\Console\SubCmd\ConvCmd\Ts2dateCmd; use Inhere\Kite\Helper\KiteUtil; use Inhere\Kite\Lib\Convert\JavaProperties; use Inhere\Kite\Lib\Parser\DBTable; @@ -26,9 +27,7 @@ use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Json; use function base_convert; -use function count; use function date; -use function preg_match_all; use function strlen; use function strtotime; use function time; @@ -58,16 +57,19 @@ public static function aliases(): array protected static function commandAliases(): array { return [ - 'ts2date' => [ - 'tc', - 'td', - ], - 'date2ts' => [ - 'dt', - ], - 'yaml2json' => ['yml2json', 'y2j'], - 'yaml2prop' => ['yml2prop', 'y2p'], - 'prop2yaml' => ['prop2yml', 'p2y'], + 'date2ts' => ['dt',], + 'yaml2json' => ['yml2json', 'y2j'], + 'yaml2prop' => ['yml2prop', 'y2p'], + 'prop2yaml' => ['prop2yml', 'p2y'], + ] + [ + Ts2dateCmd::getName() => Ts2dateCmd::aliases(), + ]; + } + + public function subCommands(): array + { + return [ + Ts2dateCmd::class, ]; } @@ -272,59 +274,4 @@ public function date2tsCommand(FlagsParser $fs, Output $output): void ], 'today'); } - /** - * convert timestamp to datetime - * - * @arguments - * times array;The want convert timestamps, allow @clipboard;true - * - * @param FlagsParser $fs - * @param Output $output - */ - public function ts2dateCommand(FlagsParser $fs, Output $output): void - { - $args = $fs->getArg('times'); - - if (count($args) === 1 && KiteUtil::isClipboardAlias($args[0])) { - $text = Clipboard::new()->read(); - $args = $text ? [$text] : []; - - if (!$args) { - throw new PromptException('no contents in clipboard'); - } - } - - $output->info('Input Data:'); - $output->writeRaw($args); - - $data = []; - foreach ($args as $time) { - if (strlen($time) > 10) { - preg_match_all('/1\d{9}/', $time, $matches); - if (empty($matches[0])) { - $output->warning("not found time in the: $time"); - continue; - } - - foreach ($matches[0] as $match) { - $data[] = [ - 'timestamp' => $match, - 'datetime' => date('Y-m-d H:i:s', (int)$match), - ]; - } - continue; - } - - $data[] = [ - 'timestamp' => $time, - 'datetime' => date('Y-m-d H:i:s', (int)$time), - ]; - } - - $output->info('Parsed Result:'); - $output->colored('- Current Time: ' . date('Y-m-d H:i:s')); - // opts - $output->table($data, 'Time to date', []); - } - } diff --git a/app/Console/Controller/StringController.php b/app/Console/Controller/StringController.php index a521268..a8b4141 100644 --- a/app/Console/Controller/StringController.php +++ b/app/Console/Controller/StringController.php @@ -15,6 +15,7 @@ use Inhere\Kite\Console\Component\Clipboard; use Inhere\Kite\Console\Component\ContentsAutoReader; use Inhere\Kite\Console\Component\ContentsAutoWriter; +use Inhere\Kite\Console\SubCmd\ConvCmd\Ts2dateCmd; use Inhere\Kite\Console\SubCmd\ParseUrlQueryCmd; use Inhere\Kite\Console\SubCmd\ToolCmd\HashCommand; use Inhere\Kite\Console\SubCmd\ToolCmd\RandomCommand; @@ -70,14 +71,17 @@ public static function aliases(): array protected static function commandAliases(): array { return [ - 'join' => ['implode', 'j'], - 'split' => ['s'], - 'process' => ['p', 'filter', 'f'], - 'replace' => ['r'], - 'parse' => ['fields'], - 'random' => RandomCommand::aliases(), - 'dequery' => ParseUrlQueryCmd::aliases(), - ]; + 'join' => ['implode', 'j'], + 'split' => ['s'], + 'process' => ['p', 'filter', 'f'], + 'replace' => ['r'], + 'parse' => ['fields'], + ] + [ + RandomCommand::getName() => RandomCommand::aliases(), + ParseUrlQueryCmd::getName() => ParseUrlQueryCmd::aliases(), + HashCommand::getName() => HashCommand::aliases(), + Ts2dateCmd::getName() => Ts2dateCmd::aliases(), + ]; } protected function init(): void @@ -90,6 +94,7 @@ protected function init(): void protected function subCommands(): array { return [ + Ts2dateCmd::class, ParseUrlQueryCmd::class, HashCommand::class, RandomCommand::class, diff --git a/app/Console/SubCmd/ConvCmd/Ts2dateCmd.php b/app/Console/SubCmd/ConvCmd/Ts2dateCmd.php new file mode 100644 index 0000000..87cb02d --- /dev/null +++ b/app/Console/SubCmd/ConvCmd/Ts2dateCmd.php @@ -0,0 +1,93 @@ +flags->addOptByRule($name, $rule); + } + + /** + * @arguments + * times array;The want convert timestamps, allow @clipboard;true + * + * @param Input $input + * @param Output $output + * + * @return mixed + */ + protected function execute(Input $input, Output $output): mixed + { + $fs = $this->flags; + + $args = $fs->getArg('times'); + if (count($args) === 1 && KiteUtil::isClipboardAlias($args[0])) { + $text = Clipboard::new()->read(); + $args = $text ? [$text] : []; + + if (!$args) { + throw new PromptException('no contents in clipboard'); + } + } + + $output->info('Input Data:'); + $output->writeRaw($args); + + $data = []; + foreach ($args as $time) { + if (strlen($time) > 10) { + preg_match_all('/1\d{9}/', $time, $matches); + if (empty($matches[0])) { + $output->warning("not found time in the: $time"); + continue; + } + + foreach ($matches[0] as $match) { + $data[] = [ + 'timestamp' => $match, + 'datetime' => date('Y-m-d H:i:s', (int)$match), + ]; + } + continue; + } + + $data[] = [ + 'timestamp' => $time, + 'datetime' => date('Y-m-d H:i:s', (int)$time), + ]; + } + + $output->info('Parsed Result:'); + // opts + $output->table($data, 'Time to date', []); + $output->colored('> Current Time: ' . date('Y-m-d H:i:s')); + return 0; + } +} diff --git a/app/Console/SubCmd/Gitflow/UpdatePushCmd.php b/app/Console/SubCmd/Gitflow/UpdatePushCmd.php index 14b03c7..272ef57 100644 --- a/app/Console/SubCmd/Gitflow/UpdatePushCmd.php +++ b/app/Console/SubCmd/Gitflow/UpdatePushCmd.php @@ -35,7 +35,7 @@ protected function configFlags(FlagsParser $fs): void * * @options * --dr, --dry-run bool;Dry-run the workflow, dont real execute - * --np, --not-push bool;Not push to remote repo after updated. + * --np, --no-push bool;Not push to remote repo after updated. * --rb, --remote-branch The remote branch name, default is current branch name. * * @param Input $input @@ -64,7 +64,7 @@ protected function execute(Input $input, Output $output): mixed $si = $gx->getStatusInfo(); - if (!$fs->getOpt('not-push')) { + if (!$fs->getOpt('no-push')) { $runner->addf('git push %s', $si->upRemote); }