diff --git a/bin/teamcity-keykeeper b/bin/teamcity-keykeeper index c57e3c7..39aefdf 100644 --- a/bin/teamcity-keykeeper +++ b/bin/teamcity-keykeeper @@ -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; @@ -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(); diff --git a/src/KeyGetCommand.php b/src/KeyGetCommand.php new file mode 100644 index 0000000..60dc29f --- /dev/null +++ b/src/KeyGetCommand.php @@ -0,0 +1,49 @@ +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)) ?: ''); + } +} diff --git a/src/KeyRestoreCommand.php b/src/KeyRestoreCommand.php index 05edd10..588a054 100644 --- a/src/KeyRestoreCommand.php +++ b/src/KeyRestoreCommand.php @@ -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}']"); } } diff --git a/src/KeySaveCommand.php b/src/KeySaveCommand.php index 3292a6a..c204c54 100644 --- a/src/KeySaveCommand.php +++ b/src/KeySaveCommand.php @@ -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'))); diff --git a/tests/KeyKeeperTest.php b/tests/KeyKeeperTest.php index 9989283..23a0b27 100644 --- a/tests/KeyKeeperTest.php +++ b/tests/KeyKeeperTest.php @@ -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}'")); + } }