1717use 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 */
2424class 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
0 commit comments