Skip to content

Commit

Permalink
added self update/rollback/check commands to manage application easier
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed Jan 5, 2020
1 parent e1d8172 commit 55c3766
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 9 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A PHP application to access your Kimai 2 installation via its API (http).
- iconv extension
- zlib extension

## Installation and updates
## Installation

To install and update the Kimai console tools, execute the following commands:

Expand Down Expand Up @@ -64,6 +64,12 @@ You get a list of all available commands with `kimai`.
- `kimai version` - show the full version string of the remote installation
- `kimai configuration` - creates the initial configuration file or displays it

The following commands will help you with updating the command:

- `kimai self:check` - check if there is a new version available
- `kimai self:update` - update your local version to the latest release
- `kimai self:rollback` - rollback to a previous release

To get help for a dedicated command use the `--help` switch, eg: `kimai project:list --help`

### Start a timesheet
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"ext-iconv": "*",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.5",
"padraic/phar-updater": "^1.0",
"symfony/console": "5.0.*"
},
"config": {
Expand Down
179 changes: 178 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ protected function getDefaultCommands()
new Command\VersionCommand(),
]);

// TODO commented, until it is working
/*
if ('phar:' === substr(__FILE__, 0, 5)) {
$commands[] = new Command\SelfCheckCommand();
$commands[] = new Command\SelfUpdateCommand();
$commands[] = new Command\SelfRollbackCommand();
}
*/

return $commands;
}
Expand Down
29 changes: 29 additions & 0 deletions src/Command/AbstractSelfCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Kimai 2 - Remote Console.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiConsole\Command;

use Humbug\SelfUpdate\Updater;
use KimaiConsole\Constants;

abstract class AbstractSelfCommand extends BaseCommand
{
/**
* {@inheritdoc}
*/
protected function getUpdater(): Updater
{
$updater = new Updater(null, false, Updater::STRATEGY_GITHUB);
$updater->getStrategy()->setPackageName('kevinpapst/kimai2-console');
$updater->getStrategy()->setPharName('kimai.phar');
$updater->getStrategy()->setCurrentLocalVersion(Constants::VERSION);

return $updater;
}
}
60 changes: 60 additions & 0 deletions src/Command/SelfCheckCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Kimai 2 - Remote Console.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiConsole\Command;

use KimaiConsole\Constants;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class SelfCheckCommand extends AbstractSelfCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('self:check')
->setDescription('Checks for available updates')
->setHelp('This command checks if there is a new version available')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

$updater = $this->getUpdater();

try {
$result = $updater->hasUpdate();

if ($result) {
// this is also triggered, if the current local version is higher then the remote version
// it does not trigger a version_compare, but only compare if the two versions are equal
$io->success(sprintf('There is a new release available in version: %s', $updater->getNewVersion()));
} elseif (false === $updater->getNewVersion()) {
$io->success('There are no stable builds available.');
} else {
$io->success(sprintf('You are running the latest version: %s', Constants::VERSION));
}
} catch (\Exception $ex) {
$io->error('Failed to check for updates: ' . $ex->getMessage());

return 1;
}

return 0;
}
}
54 changes: 54 additions & 0 deletions src/Command/SelfRollbackCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Kimai 2 - Remote Console.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiConsole\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class SelfRollbackCommand extends AbstractSelfCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('self:rollback')
->setDescription('Rollback to the latest available version')
->setHelp('This command lets you rollback an updated application to the previous version')
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

$updater = $this->getUpdater();

try {
$result = $updater->rollback();
if ($result) {
$io->success(sprintf('Rollback from version %s to %s', $updater->getOldVersion(), $updater->getNewVersion()));
} else {
$io->success('Cannot rollback');
}
} catch (\Exception $ex) {
$io->error('Failed to rollback! ' . $ex->getMessage());

return 1;
}

return 0;
}
}
Loading

0 comments on commit 55c3766

Please sign in to comment.