Skip to content

Commit

Permalink
up: add some new tool command
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 26, 2022
1 parent b3c7318 commit b652458
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 26 deletions.
6 changes: 6 additions & 0 deletions app/Concern/InitApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Inhere\Kite\Common\GitAPI\GitHubV3API;
use Inhere\Kite\Common\GitAPI\GitLabV4API;
use Inhere\Kite\Kite;
use Inhere\Kite\Lib\Jenkins\JenkinsClient;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use PhpPkg\EasyTpl\EasyTemplate;
Expand Down Expand Up @@ -122,5 +123,10 @@ protected function registerComServices(ObjectBox $box): void
$config = $this->getArrayParam('github');
return new GitHubV3API($config);
});

$box->set('jenkins', function () {
$config = $this->getArrayParam('jenkins');
return new JenkinsClient($config);
});
}
}
2 changes: 2 additions & 0 deletions app/Console/Command/ToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Console\SubCmd\OpenCmd;
use Inhere\Kite\Console\SubCmd\ToolCmd\HashHmacCommand;

/**
* Class ToolCommand
Expand All @@ -26,6 +27,7 @@ protected function subCommands(): array
{
return [
OpenCmd::class,
HashHmacCommand::class,
];
}

Expand Down
61 changes: 61 additions & 0 deletions app/Console/SubCmd/ToolCmd/HashHmacCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 function hash_hmac;
use function md5;
use function strtoupper;

/**
* class HashHmacCommand
*
* @author inhere
*/
class HashHmacCommand extends Command
{
protected static string $name = 'hmac';
protected static string $desc = 'Generate a keyed hash value using the HMAC method';

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, 'sha256');
$this->flags->addOpt(
'key', 'k',
'Shared secret key used for generating the HMAC variant of the message digest.',
'string', true);
}

protected function execute(Input $input, Output $output)
{
$fs = $this->flags;

$secKey = $fs->getOpt('key');
$rawStr = $fs->getArg('str');
$rawStr = ContentsAutoReader::readFrom($rawStr);

$algoName = $fs->getOpt('algo');
$output->aList([
'key' => $secKey,
'algo' => $algoName,
]);
$output->colored('RAW STRING:');
$output->println($rawStr);

if ($algoName === 'md5') {
$signStr = md5($rawStr);
} else {
$signStr = hash_hmac($algoName, $rawStr, $secKey);
}

$signStr = strtoupper($signStr);

$output->colored('SIGN:');
$output->println($signStr);
}
}
2 changes: 2 additions & 0 deletions app/Kite.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Inhere\Kite\Console\Component\AutoSetProxyEnv;
use Inhere\Kite\Console\Plugin\PluginManager;
use Inhere\Kite\Http\WebApplication;
use Inhere\Kite\Lib\Jenkins\JenkinsClient;
use Inhere\Kite\Lib\Jump\QuickJump;
use Inhere\Route\Dispatcher\Dispatcher;
use Inhere\Route\Router;
Expand All @@ -36,6 +37,7 @@
* @method static Router webRouter()
* @method static Dispatcher dispatcher()
* @method static ScriptRunner scriptRunner()
* @method static JenkinsClient jenkins()
*
* @see Kite::__callStatic() for quick get object
*/
Expand Down
108 changes: 97 additions & 11 deletions app/Lib/Jenkins/JenkinsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,126 @@

namespace Inhere\Kite\Lib\Jenkins;

use Psr\Http\Client\ClientInterface;
use Toolkit\Stdlib\Obj\Traits\QuickInitTrait;
use PhpPkg\Http\Client\AbstractClient;
use PhpPkg\Http\Client\Client;
use Toolkit\Stdlib\Obj\AbstractObj;
use function strtr;

/**
* class JenkinsClient
*
* @author inhere
*/
class JenkinsClient
class JenkinsClient extends AbstractObj
{
use QuickInitTrait;
/**
* @var AbstractClient|null
*/
private ?AbstractClient $httpClient;

/**
* @var string
*/
public string $hostUrl = '';

/**
* @var ClientInterface
* Jenkins username
*
* @var string
*/
public string $username = '';

/**
* @var string
*/
private ClientInterface $httpClient;
public string $apiToken = '';

/**
* eg: /some
*
* @var string
*/
public string $baseUrl = '';
public string $folderPath = '';

/**
* @var string
*/
public string $jobName = '';

/**
* @param string $jobName
*
* @return $this
*/
public function withJobName(string $jobName): self
{
$this->jobName = $jobName;
return $this;
}

/**
* @param array $params
*
* @return string
*/
public function buildWithParams(array $params): string
{
$tpl = '{folderPath}/job/{name}/buildWithParameters';
$url = $this->buildUrl($tpl);
$cli = $this->getHttpClient()->post($url, $params);

return $cli->getResponseBody();
}

public function buildNoParams(): self
{
$tpl = '{folderPath}/job/{name}/build';
$url = $this->buildUrl($tpl);

return $this;
}

/**
* @return ClientInterface
* @param string $pathTpl
*
* @return string
*/
public function getHttpClient(): ClientInterface
public function buildUrl(string $pathTpl): string
{
return $this->hostUrl . strtr($pathTpl, [
'{folderPath}' => $this->folderPath,
'{name}' => $this->jobName,
]);
}

/**
* @param string $jobName
*
* @return string
*/
public function jobPageUrl(string $jobName = ''): string
{
$jobName = $jobName ?: $this->jobName;

return $this->hostUrl . '/job/' . $jobName;
}

/**
* @return AbstractClient
*/
public function getHttpClient(): AbstractClient
{
if (!$this->httpClient) {
$this->httpClient = Client::factory([]);
}

return $this->httpClient;
}

/**
* @param ClientInterface $httpClient
* @param AbstractClient $httpClient
*/
public function setHttpClient(ClientInterface $httpClient): void
public function setHttpClient(AbstractClient $httpClient): void
{
$this->httpClient = $httpClient;
}
Expand Down
35 changes: 20 additions & 15 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
$basePath = Inhere\Kite\Kite::basePath();

return [
'app' => [
'app' => [
],
'logger' => [
'logger' => [
'name' => 'Kite',
'logfile' => OS::userCacheDir('kite.log'),
],
'git' => [
'git' => [
// remote
'mainRemote' => 'main',
'forkRemote' => 'origin',
// 'auto-sign' => true,
// 'sign-text' => 'inhere <in.798@qq.com>',
],
'gitflow' => [
'gitflow' => [
// remote
'mainRemote' => 'main',
'forkRemote' => 'origin',
],
'gitlab' => [
'gitlab' => [
// remote
'mainRemote' => 'main',
'forkRemote' => 'origin',
Expand All @@ -36,23 +36,23 @@
// gitlab api config
'baseUrl' => '',
],
'github' => [
'github' => [
// remote
'mainRemote' => 'main',
'forkRemote' => 'origin',
// group
'defaultGroup' => 'swoft',
'defaultForkGroup' => 'ulue',
'redirectGit' => [
'*'
'*'
],
// github api config
// 'baseUrl' => '',
],
'osEnv' => [
'osEnv' => [
// env settings
],
'osPathEnv' => [
'osPathEnv' => [
// os path env settings
// '/path/to/my-tool/bin',
],
Expand All @@ -61,8 +61,13 @@
// 'http_proxy' => 'http://127.0.0.1:1081',
// 'https_proxy' => 'http://127.0.0.1:1081',
],
'jenkins' => [
'hostUrl' => getenv('JK_HOST_URL') ?: '',
'username' => getenv('JK_UNAME') ?: '',
'apiToken' => getenv('JK_API_TOKEN') ?: '',
],
// tool command usage docs
'manDocs' => [
'manDocs' => [
// if 'lang' not setting, will read from ENV.
// 'lang' => 'en',
'fallbackLang' => 'en',
Expand All @@ -82,16 +87,16 @@
],
],
/** @see \Inhere\Kite\Component\ScriptRunner */
'scriptRunner' => [
'enable' => true,
'scriptRunner' => [
'enable' => true,
],
'scriptDirs' => [
'scriptDirs' => [
// BASE_PATH . '/script',
$basePath . '/script',
$basePath . '/custom/script',
],
// custom scripts for quick run an command
'scripts' => require 'scripts.php',
'scripts' => require 'scripts.php',
// command aliases. element is: alias command => real command
'aliases' => require 'aliases.php',
'aliases' => require 'aliases.php',
];

0 comments on commit b652458

Please sign in to comment.