Skip to content

Commit 3eff5ed

Browse files
committed
udpate some logic for self update command
1 parent ab4fed6 commit 3eff5ed

3 files changed

Lines changed: 39 additions & 34 deletions

File tree

app/Command/SelfUpdateCommand.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
use function parse_url;
1818

1919
/**
20-
* update the swoft-cli to latest version from github releases
20+
* update the swoft-cli to latest version from github
2121
*
22-
* @Command("self-update", alias="selfupdate, upself, update-self, updateself", coroutine=true)
22+
* @Command("self-update", alias="selfupdate, update-self, updateself", coroutine=true)
2323
*/
2424
class SelfUpdateCommand
2525
{
2626
public const LATEST_RELEASE_URL = 'https://api.github.com/repos/swoft-cloud/swoft-cli/releases/latest';
2727

2828
/**
29-
* update the swoft-cli to latest version from github releases
29+
* update the swoft-cli to latest version from github
3030
*
31-
* @CommandMapping(alias="selfupdate, upself, update-self, updateself")
31+
* @CommandMapping(alias="selfupdate, update-self, updateself")
3232
* @CommandOption(
3333
* "only-check", type="bool",
3434
* desc="only fetch latest release information, but dont download and update package",
@@ -47,30 +47,14 @@ public function up(Input $input, Output $output): void
4747

4848
$output->colored('Fetch latest release information for Github ...', 'cyan');
4949

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-
50+
$result = $this->fetchInfo();
6651
$latest = json_decode($result, true);
6752
if (!$latest) {
68-
Show::error('Failed for update swoft-cli: fetch latest version info failed');
53+
Show::error('Failed for update: fetch latest version info failed');
6954
return;
7055
}
7156

72-
$tagName = $latest['tag_name'];
73-
57+
$tagName = $latest['tag_name'];
7458
$metaInfo = [
7559
'local version' => 'v' . SwoftCLI::VERSION,
7660
'latest version' => $tagName,
@@ -79,14 +63,13 @@ public function up(Input $input, Output $output): void
7963
];
8064

8165
Show::aList($metaInfo, 'latest release information');
82-
8366
if ($input->getOpt('only-check')) {
8467
return;
8568
}
8669

8770
// get phar download address.
8871
if (!isset($latest['assets'][0]['browser_download_url'])) {
89-
Show::error('Failed for update swoft-cli: not found latest phar package download url');
72+
Show::error('Failed for update: not found latest phar package download url');
9073
return;
9174
}
9275

@@ -95,12 +78,33 @@ public function up(Input $input, Output $output): void
9578
Download::file($pharUrl);
9679
}
9780

81+
private function fetchInfo(): string
82+
{
83+
$info = $this->parseUrl(self::LATEST_RELEASE_URL);
84+
$port = (int)$info['port'];
85+
$path = $info['path'];
86+
87+
$client = new Client($info['host'], $port, $port === 443);
88+
$client->setHeaders([
89+
'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',
90+
]);
91+
$client->execute($path);
92+
93+
// $status = $client->statusCode;
94+
$result = $client->body;
95+
96+
// close connection
97+
$client->close();
98+
99+
return $result;
100+
}
101+
98102
/**
99103
* @param string $url
100104
*
101105
* @return array
102106
*/
103-
public function parseUrl(string $url): array
107+
private function parseUrl(string $url): array
104108
{
105109
$info = parse_url($url);
106110

app/bean.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66

77
return [
88
'cliApp' => [
9-
'name' => 'Swoft-cli',
9+
'name' => config('name'),
1010
'version' => SwoftCLI::VERSION,
11-
'description' => 'CLI tool application for quick use swoft framework',
11+
'description' => config('description'),
1212
],
1313
'cliRouter' => [
1414
'idAliases' => [
1515
// 'run' => 'serve:run'
1616
'ab' => 'tool:ab',
17-
'upself' => 'self-update:up',
1817
'update-self' => 'self-update:up',
1918
'updateself' => 'self-update:up',
20-
'selfupdate' => 'self-update:up',
19+
'selfupdate' => 'self-update:up',
2120
'self-update' => 'self-update:up',
2221
],
2322
'disabledGroups' => ['http', 'asset'],

config/base.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php declare(strict_types=1);
22

3+
use Swoft\Cli\SwoftCLI;
4+
35
return [
4-
'name' => 'swoft cli',
5-
'debug' => env('SWOFT_DEBUG', 1),
6-
'version' => '1.0.0',
7-
'description' => 'CLI tool application for quick use swoft',
6+
'name' => 'Swoft-cli',
7+
'debug' => env('SWOFT_DEBUG', 0),
8+
'version' => SwoftCLI::VERSION,
9+
'description' => '🛠️ Command line tool application for quick use swoft',
810
];

0 commit comments

Comments
 (0)