Skip to content

Commit ab4fed6

Browse files
committed
add new command for update self pacakge
1 parent c1e289b commit ab4fed6

4 files changed

Lines changed: 135 additions & 2 deletions

File tree

app/Command/SelfUpdateCommand.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Swoft\Cli\Command;
4+
5+
use Swoft\Cli\SwoftCLI;
6+
use Swoft\Console\Helper\Show;
7+
use Swoft\Console\Annotation\Mapping\Command;
8+
use Swoft\Console\Annotation\Mapping\CommandArgument;
9+
use Swoft\Console\Annotation\Mapping\CommandMapping;
10+
use Swoft\Console\Annotation\Mapping\CommandOption;
11+
use Swoft\Console\Input\Input;
12+
use Swoft\Console\Output\Output;
13+
use Swoole\Coroutine\Http\Client;
14+
use Toolkit\Cli\Download;
15+
use function file_get_contents;
16+
use function json_decode;
17+
use function parse_url;
18+
19+
/**
20+
* update the swoft-cli to latest version from github releases
21+
*
22+
* @Command("self-update", alias="selfupdate, upself, update-self, updateself", coroutine=true)
23+
*/
24+
class SelfUpdateCommand
25+
{
26+
public const LATEST_RELEASE_URL = 'https://api.github.com/repos/swoft-cloud/swoft-cli/releases/latest';
27+
28+
/**
29+
* update the swoft-cli to latest version from github releases
30+
*
31+
* @CommandMapping(alias="selfupdate, upself, update-self, updateself")
32+
* @CommandOption(
33+
* "only-check", type="bool",
34+
* desc="only fetch latest release information, but dont download and update package",
35+
* )
36+
* @param Input $input
37+
* @param Output $output
38+
*/
39+
public function up(Input $input, Output $output): void
40+
{
41+
$output->colored('Current Version: ' . SwoftCLI::VERSION);
42+
43+
// @see https://developer.github.com/v3/repos/releases/
44+
// curl https://api.github.com/repos/swoft-cloud/swoft-cli/releases/latest
45+
46+
// $jsonText = file_get_contents(self::LATEST_RELEASE_URL);
47+
48+
$output->colored('Fetch latest release information for Github ...', 'cyan');
49+
50+
$info = $this->parseUrl(self::LATEST_RELEASE_URL);
51+
$port = (int)$info['port'];
52+
$path = $info['path'];
53+
54+
$client = new Client($info['host'], $port, $port === 443);
55+
$client->setHeaders([
56+
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
57+
]);
58+
$client->execute($path);
59+
60+
// $status = $client->statusCode;
61+
$result = $client->body;
62+
63+
// close connection
64+
$client->close();
65+
66+
$latest = json_decode($result, true);
67+
if (!$latest) {
68+
Show::error('Failed for update swoft-cli: fetch latest version info failed');
69+
return;
70+
}
71+
72+
$tagName = $latest['tag_name'];
73+
74+
$metaInfo = [
75+
'local version' => 'v' . SwoftCLI::VERSION,
76+
'latest version' => $tagName,
77+
'created at' => $latest['created_at'],
78+
'published at' => $latest['published_at'],
79+
];
80+
81+
Show::aList($metaInfo, 'latest release information');
82+
83+
if ($input->getOpt('only-check')) {
84+
return;
85+
}
86+
87+
// get phar download address.
88+
if (!isset($latest['assets'][0]['browser_download_url'])) {
89+
Show::error('Failed for update swoft-cli: not found latest phar package download url');
90+
return;
91+
}
92+
93+
$pharUrl = $latest['assets'][0]['browser_download_url'];
94+
95+
Download::file($pharUrl);
96+
}
97+
98+
/**
99+
* @param string $url
100+
*
101+
* @return array
102+
*/
103+
public function parseUrl(string $url): array
104+
{
105+
$info = parse_url($url);
106+
107+
if ($info['scheme'] === 'https') {
108+
$info['port'] = 443;
109+
}
110+
111+
return $info;
112+
}
113+
}

app/Command/ToolCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
namespace Swoft\Cli\Command;
44

55
use Swoft\Console\Helper\Show;
6+
use Swoft\Console\Annotation\Mapping\Command;
7+
use Swoft\Console\Annotation\Mapping\CommandArgument;
8+
use Swoft\Console\Annotation\Mapping\CommandMapping;
9+
use Swoft\Console\Annotation\Mapping\CommandOption;
610

11+
/**
12+
* some internal tool commands, like ab, update-self
13+
*
14+
* @Command(coroutine=false)
15+
*/
716
class ToolCommand
817
{
918
/**
10-
* like ab test tool
19+
* like ab test tool, but can use for test http, websocket, tcp server
20+
*
21+
* @CommandMapping()
1122
*/
1223
public function ab(): void
1324
{

app/SwoftCLI.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
class SwoftCLI extends SwoftApplication
1616
{
17+
public const VERSION = '0.1.2';
18+
1719
protected function afterInit(): void
1820
{
1921
parent::afterInit();

app/bean.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@
22

33
// use Swoft\Http\Server\Swoole\RequestListener;
44
// use Swoft\Server\Swoole\SwooleEvent;
5+
use Swoft\Cli\SwoftCLI;
56

67
return [
78
'cliApp' => [
89
'name' => 'Swoft-cli',
9-
'version' => '0.1.2',
10+
'version' => SwoftCLI::VERSION,
1011
'description' => 'CLI tool application for quick use swoft framework',
1112
],
1213
'cliRouter' => [
1314
'idAliases' => [
1415
// 'run' => 'serve:run'
16+
'ab' => 'tool:ab',
17+
'upself' => 'self-update:up',
18+
'update-self' => 'self-update:up',
19+
'updateself' => 'self-update:up',
20+
'selfupdate' => 'self-update:up',
21+
'self-update' => 'self-update:up',
1522
],
1623
'disabledGroups' => ['http', 'asset'],
1724
],

0 commit comments

Comments
 (0)