Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Commit

Permalink
add key:get
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Jul 9, 2018
1 parent db4e661 commit cbae096
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 28 deletions.
2 changes: 2 additions & 0 deletions bin/teamcity-keykeeper
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @link https://github.com/JBZoo/TeamcityKeyKeeper
*/

use JBZoo\TeamcityKeyKeeper\KeyGetCommand;
use JBZoo\TeamcityKeyKeeper\KeyRestoreCommand;
use JBZoo\TeamcityKeyKeeper\KeySaveCommand;
use Symfony\Component\Console\Application;
Expand Down Expand Up @@ -43,4 +44,5 @@ foreach ($vendorPaths as $vendorPath) {
$application = new Application();
$application->add(new KeySaveCommand());
$application->add(new KeyRestoreCommand());
$application->add(new KeyGetCommand());
$application->run();
49 changes: 49 additions & 0 deletions src/KeyGetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* JBZoo TeamcityKeyKeeper
*
* This file is part of the JBZoo CCK package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package TeamcityKeyKeeper
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/TeamcityKeyKeeper
*/

namespace JBZoo\TeamcityKeyKeeper;

use JBZoo\Data\JSON;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class KeyGetCommand
* @package JBZoo\TeamcityKeyKeeper
*/
class KeyGetCommand extends Command
{
protected function configure()
{
$this
->setName('key:get')
->addOption('name', null, InputOption::VALUE_REQUIRED, 'Name of key')
->addOption('group', null, InputOption::VALUE_OPTIONAL, 'Group of keys', 'default')
->setDescription('Get clean value of key');
}

/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = strtoupper(trim($input->getOption('name')));
$group = trim($input->getOption('group'));

$storage = new JSON(realpath(PATH_STORAGE . "/{$group}.json") ?: []);
$output->write(trim($storage->get($name)) ?: '');
}
}
4 changes: 1 addition & 3 deletions src/KeyRestoreCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$storage = new JSON($storageFile);

if (!$input->getOption('all')) {
$name = trim($input->getOption('name'));
$name = strtoupper(trim($input->getOption('name')));
$value = $storage->get($name);
$name = strtoupper($name);
$output->writeln("##teamcity[setParameter name='env.{$name}' value='{$value}']");
} else {
foreach ($storage->getArrayCopy() as $name => $value) {
$name = strtoupper($name);
$output->writeln("##teamcity[setParameter name='env.{$name}' value='{$value}']");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/KeySaveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = trim($input->getOption('name'));
$name = strtoupper(trim($input->getOption('name')));
$value = trim($input->getOption('value'));
$group = strtolower(trim($input->getOption('group')));

Expand Down
57 changes: 33 additions & 24 deletions tests/KeyKeeperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,73 +23,82 @@
*/
class KeyKeeperTest extends PHPUnit
{
/**
* @var string
*/
protected $bin = '';

public function setUp()
{
$storageFile = __DIR__ . '/../storage/default.json';
if (file_exists($storageFile)) {
unlink($storageFile);
}

$this->bin = 'php ' . realpath(__DIR__ . '/../bin/teamcity-keykeeper');
}

public function testSaveAndRestoreKey()
{
$bin = realpath(__DIR__ . '/../bin/teamcity-keykeeper');
$value = Str::random(10000);
$name = Str::random();

$saveResult = trim(Cli::exec("php {$bin} key:save --name='{$name}' --value='{$value}'"));
$saveResult = trim(Cli::exec("{$this->bin} key:save --name='{$name}' --value='{$value}'"));
$name = strtoupper($name);
isContain("Key '{$name}' saved", $saveResult);

$restoreResult = trim(Cli::exec("php {$bin} key:restore --name='{$name}'"));
$name = strtoupper($name);
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --name='{$name}'"));
isSame("##teamcity[setParameter name='env.{$name}' value='{$value}']", $restoreResult);
}

public function testSpecialChars()
{
$bin = realpath(__DIR__ . '/../bin/teamcity-keykeeper');
$value = 'qwerty1234567890-!@#$%^&*()_+.<>,;{}№';
$name = 'qwerty1234567890-!@#$%^&*()_+.<>,;{}№';
$name = 'qwerty1234567890';

$saveResult = trim(Cli::exec("php {$bin} key:save --name='{$name}' --value='{$value}'"));
$saveResult = trim(Cli::exec("{$this->bin} key:save", ['name' => $name, 'value' => $value]));
$name = strtoupper($name);
isContain("Key '{$name}' saved", $saveResult);

$restoreResult = trim(Cli::exec("php {$bin} key:restore --name='{$name}'"));
$name = strtoupper($name);
$restoreResult = trim(Cli::exec("{$this->bin} key:restore", ['name' => $name]));
isSame("##teamcity[setParameter name='env.{$name}' value='{$value}']", $restoreResult);
}

public function testGetAllKeys()
{
$bin = realpath(__DIR__ . '/../bin/teamcity-keykeeper');

Cli::exec("php {$bin} key:save --name='key1' --value='value1'");
Cli::exec("php {$bin} key:save --name='key2' --value='value2'");
Cli::exec("{$this->bin} key:save --name='key1' --value='value1'");
Cli::exec("{$this->bin} key:save --name='key2' --value='value2'");

$restoreResult = trim(Cli::exec("php {$bin} key:restore --all"));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all"));
isContain("##teamcity[setParameter name='env.KEY1' value='value1']", $restoreResult);
isContain("##teamcity[setParameter name='env.KEY2' value='value2']", $restoreResult);
}

public function testRemoveKeysWithOption()
{
$bin = realpath(__DIR__ . '/../bin/teamcity-keykeeper');
Cli::exec("{$this->bin} key:save --name='key' --value='value1'");
Cli::exec("{$this->bin} key:save --name='key' --value=''");

Cli::exec("php {$bin} key:save --name='key' --value='value1'");
Cli::exec("php {$bin} key:save --name='key' --value=''");

$restoreResult = trim(Cli::exec("php {$bin} key:restore --all"));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all"));
isContain("##teamcity[setParameter name='env.KEY' value='']", $restoreResult);
}

public function testRemoveKeysWithoutOption()
{
$bin = realpath(__DIR__ . '/../bin/teamcity-keykeeper');

Cli::exec("php {$bin} key:save --name='key' --value='value1'");
Cli::exec("php {$bin} key:save --name='key'");
Cli::exec("{$this->bin} key:save --name='key' --value='value1'");
Cli::exec("{$this->bin} key:save --name='key'");

$restoreResult = trim(Cli::exec("php {$bin} key:restore --all"));
$restoreResult = trim(Cli::exec("{$this->bin} key:restore --all"));
isContain("##teamcity[setParameter name='env.KEY' value='']", $restoreResult);
}

public function testGetCleanValue()
{
$key = Str::random();
$value = 'qwerty1234567890-!@#$%^&*()_+.<>,;{}№\'"';

Cli::exec("{$this->bin} key:save", ['name' => $key, 'value' => $value]);

isSame($value, Cli::exec("{$this->bin} key:get --name='{$key}'"));
}
}

0 comments on commit cbae096

Please sign in to comment.