From de4d0b6e66ceda08b0daec18a218c1baa71ebf04 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sat, 26 Dec 2020 01:27:29 +0300 Subject: Add --version flag --- .github/workflows/release.yml | 4 +- bin/rr | 312 ------------------------------------------ cmd/cli/root.go | 1 + cmd/cli/version.go | 9 ++ cmd/main.go | 8 -- 5 files changed, 12 insertions(+), 322 deletions(-) delete mode 100755 bin/rr create mode 100644 cmd/cli/version.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 97a412fc..fbf9af0c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -55,8 +55,8 @@ jobs: CGO_ENABLED: 0 LDFLAGS: >- -s - -X github.com/spiral/roadrunner/cmd/main.Version=${{ steps.values.outputs.version }} - -X github.com/spiral/roadrunner/cmd/main.BuildTime=${{ steps.values.outputs.timestamp }} + -X github.com/spiral/roadrunner/cmd/cli.Version=${{ steps.values.outputs.version }} + -X github.com/spiral/roadrunner/cmd/cli.BuildTime=${{ steps.values.outputs.timestamp }} run: | go build -trimpath -ldflags "$LDFLAGS" -o "./${{ steps.values.outputs.binary-name }}" ./cmd/main.go stat "./${{ steps.values.outputs.binary-name }}" diff --git a/bin/rr b/bin/rr deleted file mode 100755 index d7d0b4f2..00000000 --- a/bin/rr +++ /dev/null @@ -1,312 +0,0 @@ -#!/usr/bin/env php -open($archive); - - $name = self::getSignature() . '/rr'; - if (self::getOSType() == 'windows') { - $name .= '.exe'; - } - - $stream = $zip->getStream($name); - if (!is_resource($stream)) { - return; - } - - $to = fopen($target, 'w'); - stream_copy_to_stream($stream, $to); - fclose($to); - - $zip->close(); - } - - /** - * @param string $archive - * @param string $target - * @throws Exception - */ - protected static function extractTAR(string $archive, string $target) - { - $arch = new PharData($archive); - $arch->extractTo('./', self::getSignature() . '/rr'); - - copy('./' . self::getSignature() . '/rr', $target); - unlink('./' . self::getSignature() . '/rr'); - rmdir('./' . self::getSignature()); - } -} - -(new Application('RoadRunner', RRHelper::getVersion())) - ->register('get-binary') - ->setDescription("Install or update RoadRunner binaries in specified folder (current folder by default)") - ->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.') - ->setCode(function (InputInterface $input, OutputInterface $output) { - $output->writeln('Updating binary file of RoadRunner'); - - $finalFile = $input->getOption('location') . DIRECTORY_SEPARATOR . 'rr'; - if (RRHelper::getOSType() == 'windows') { - $finalFile .= '.exe'; - } - - if (is_file($finalFile)) { - $version = RRHelper::getVersion(); - - $previousVersion = preg_match( - '#Version:.+(\d+\.\d+\.\d+)#', - (string)shell_exec($finalFile), - $matches - ) ? $matches[1] : ""; - - $output->writeln('RoadRunner binary file already exists!'); - $helper = $this->getHelper('question'); - - if (version_compare($previousVersion, $version) === 0) { - $output->writeln(sprintf('Current version: %s', $previousVersion)); - $question = new ConfirmationQuestion( - sprintf('Skip update to the same version: %s ? [Y/n]', $version) - ); - if ($helper->ask($input, $output, $question)) { - return; - } - } else { - $question = new ConfirmationQuestion('Do you want overwrite it? [Y/n]'); - if (!$helper->ask($input, $output, $question)) { - return; - } - } - } - - $output->writeln('Downloading RoadRunner archive for ' . ucfirst(RRHelper::getOSType()) . ''); - - $progressBar = new ProgressBar($output); - $progressBar->setFormat('verbose'); - - $zipFileName = 'rr_zip_'.random_int(0, 10000); - if (RRHelper::getOSType() == 'linux') { - $zipFileName .= '.tar.gz'; - } - - $zipFile = fopen($zipFileName, "w+"); - $curlResource = curl_init(); - - curl_setopt($curlResource, CURLOPT_URL, RRHelper::getBinaryDownloadUrl()); - curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curlResource, CURLOPT_BINARYTRANSFER, true); - curl_setopt($curlResource, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($curlResource, CURLOPT_FILE, $zipFile); - curl_setopt($curlResource, CURLOPT_PROGRESSFUNCTION, - function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use (&$progressBar, $output) { - if ($download_size == 0) { - return; - } - - if ($progressBar->getStartTime() === 0) { - $progressBar->start(); - } - - if ($progressBar->getMaxSteps() != $download_size) { - /** - * Workaround for symfony < 4.1.x, for example PHP 7.0 will use 3.x - * feature #26449 Make ProgressBar::setMaxSteps public (ostrolucky) - */ - $progressBar = new ProgressBar($output, $download_size); - } - - $progressBar->setFormat('[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% ' . intval($download_size / 1024) . 'KB'); - $progressBar->setProgress($downloaded); - }); - curl_setopt($curlResource, CURLOPT_NOPROGRESS, false); // needed to make progress function work - curl_setopt($curlResource, CURLOPT_HEADER, 0); - curl_exec($curlResource); - curl_close($curlResource); - fclose($zipFile); - - $progressBar->finish(); - $output->writeln(""); - - $output->writeln('Unpacking ' . basename(RRHelper::getBinaryDownloadUrl()) . ''); - - RRHelper::extractBinary($zipFileName, $finalFile); - unlink($zipFileName); - - if (!file_exists($finalFile) || filesize($finalFile) === 0) { - throw new Exception('Unable to extract the file.'); - } - - chmod($finalFile, 0755); - $output->writeln('Binary file updated!'); - }) - ->getApplication() - ->register("init-config") - ->setDescription("Inits default .rr.yaml config in specified folder (current folder by default)") - ->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.') - ->setCode(function (InputInterface $input, OutputInterface $output) { - if (is_file($input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml')) { - $output->writeln('Config file already exists!'); - $helper = $this->getHelper('question'); - $question = new ConfirmationQuestion('Do you want overwrite it? [Y/n] '); - - if (!$helper->ask($input, $output, $question)) { - return; - } - } - - copy( - __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.rr.yaml', - $input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml' - ); - $output->writeln('Config file created!'); - }) - ->getApplication() - ->run(); diff --git a/cmd/cli/root.go b/cmd/cli/root.go index eb15f565..7b18a22f 100644 --- a/cmd/cli/root.go +++ b/cmd/cli/root.go @@ -25,6 +25,7 @@ var ( Use: "rr", SilenceErrors: true, SilenceUsage: true, + Version: Version, } ) diff --git a/cmd/cli/version.go b/cmd/cli/version.go new file mode 100644 index 00000000..6a259668 --- /dev/null +++ b/cmd/cli/version.go @@ -0,0 +1,9 @@ +package cli + +var ( + // Version - defines build version. + Version string = "local" //nolint:deadcode + + // BuildTime - defined build time. + BuildTime string = "development" //nolint:deadcode +) diff --git a/cmd/main.go b/cmd/main.go index a18d2c0d..4e7fc099 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,14 +17,6 @@ import ( "github.com/spiral/roadrunner/v2/plugins/rpc" ) -var ( - // Version - defines build version. - Version string = "local" //nolint:deadcode - - // BuildTime - defined build time. - BuildTime string = "development" //nolint:deadcode -) - func main() { var err error cli.Container, err = endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel), endure.RetryOnFail(false)) -- cgit v1.2.3