Skip to content

Commit

Permalink
up: add some new commands for gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 25, 2022
1 parent 7db35e8 commit 789be67
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/Console/Attach/Gitlab/BranchCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function aliases(): array
protected function subCommands(): array
{
return [
BranchInitCmd::class,
BranchCreateCmd::class,
];
}
Expand Down
5 changes: 5 additions & 0 deletions app/Console/Attach/Gitlab/BranchCreateCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ class BranchCreateCmd extends Command
protected static string $name = 'create';
protected static string $desc = 'create a new branch for gitlab project';

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

protected function configFlags(FlagsParser $fs): void
{
// $this->flags->addOptByRule($name, $rule);
Expand Down
38 changes: 38 additions & 0 deletions app/Console/Attach/Gitlab/BranchInitCmd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\Attach\Gitlab;

use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Toolkit\PFlag\FlagsParser;

/**
* Class BranchInitCmd
*
* @package Inhere\Kite\Console\Controller\Gitlab
*/
class BranchInitCmd extends Command
{
protected static string $name = 'init';
protected static string $desc = 'quick init testing, qa, pre branches for gitlab project';

protected function configFlags(FlagsParser $fs): void
{
// $this->flags->addOptByRule($name, $rule);
}

/**
* Do execute command
*
* @param Input $input
* @param Output $output
*
* @return int|mixed
*/
protected function execute(Input $input, Output $output): mixed
{
$output->println(__METHOD__);
return 0;
}
}
45 changes: 40 additions & 5 deletions app/Console/Command/SearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Inhere\Console\Command;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Helper\AppHelper;
use InvalidArgumentException;
use Toolkit\Stdlib\Str\UrlHelper;
use function implode;
use function strpos;
use function substr;

Expand Down Expand Up @@ -36,14 +40,33 @@ class SearchCommand extends Command
'scholar' => 'https://scholar.google.com/scholar?q=',
];

/**
* @var array{string, string}
*/
private array $engAliases = [
'bi' => 'bing',
'b' => 'baidu',
'bd' => 'baidu',
'g' => 'google',
'gg' => 'google',
'gh' => 'github',
'ddg' => 'duckduckgo',
'sf' => 'stackoverflow',
];

public static function aliases(): array
{
return ['s'];
}

/**
* Search by web search engine, such as google, baidu, bing
*
* @arguments
* keywords The keywords for search
* keywords array;The keywords for search;true
*
* @options
* -e, --engine string;The web search engine name;;google
* -e, --engine string;The web search engine name;;baidu
* -l, --list bool;list all web search engine
*
* @param Input $input
Expand All @@ -54,12 +77,24 @@ class SearchCommand extends Command
protected function execute(Input $input, Output $output): int
{
if ($this->flags->getOpt('list')) {
$output->aList($this->engAliases, 'Engine Aliases');
$output->aList($this->getEngineList(), 'Engine List');
return 0;
}

$engine = $this->flags->getOpt('engine');
$engine = $this->engAliases[$engine] ?? $engine;
if (!isset($this->engines[$engine])) {
throw new InvalidArgumentException('invalid engine name: ' . $engine);
}

$engUrl = $this->engines[$engine];
$keywords = $this->flags->getArg('keywords');
$queryStr = UrlHelper::encode(implode(' ', $keywords));

// on MAC: open URL
// on MAC: open_command URL
AppHelper::openBrowser($engUrl . $queryStr);
return 0;
}

Expand All @@ -68,11 +103,11 @@ private function getEngineList(): array
$list = [];
foreach ($this->engines as $name => $url) {
if ($pos = strpos($url, '.com/')) {
$url = substr($url, 0, $pos+4);
$url = substr($url, 0, $pos + 4);
} elseif ($pos = strpos($url, '.org/')) {
$url = substr($url, 0, $pos+4);
$url = substr($url, 0, $pos + 4);
} elseif ($pos = strpos($url, '.ru/')) {
$url = substr($url, 0, $pos+4);
$url = substr($url, 0, $pos + 4);
}

$list[$name] = $url;
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Controller/GitLabController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Inhere\Kite\Helper\AppHelper;
use Inhere\Kite\Helper\GitUtil;
use Throwable;
use Toolkit\FsUtil\Path;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Str;
use function array_merge;
Expand Down Expand Up @@ -78,6 +77,7 @@ protected static function commandAliases(): array
'updatePush' => ['upp', 'up-push'],
'project' => ['pj', 'info'],
'checkout' => ['co'],
'branch' => BranchCmd::aliases(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Console/Controller/StringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function processCommand(FlagsParser $fs, Output $output): void
$cutChar = $cut;
if (strlen($cut) > 1) {
if ($cut[0] === 'L') {
$cutPos = 'L';
// $cutPos = 'L';
$cutChar= substr($cut, 1);
} elseif ($cut[0] === 'R') {
$cutPos = 'R';
Expand Down

0 comments on commit 789be67

Please sign in to comment.