diff --git a/src/Command/Environment/EnvironmentDeleteCommand.php b/src/Command/Environment/EnvironmentDeleteCommand.php index 2072888960..97502bc1d3 100644 --- a/src/Command/Environment/EnvironmentDeleteCommand.php +++ b/src/Command/Environment/EnvironmentDeleteCommand.php @@ -3,6 +3,7 @@ use Platformsh\Cli\Command\CommandBase; use Platformsh\Cli\Console\ArrayArgument; +use Platformsh\Cli\Util\Wildcard; use Platformsh\Client\Model\Environment; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -60,7 +61,8 @@ protected function execute(InputInterface $input, OutputInterface $output) } if ($specifiedEnvironmentIds) { $anythingSpecified = true; - $specifiedEnvironmentIds = $this->findEnvironmentIDsByWildcards($environments, $specifiedEnvironmentIds); + $allIds = \array_map(function (Environment $e) { return $e->id; }, $environments); + $specifiedEnvironmentIds = Wildcard::select($allIds, $specifiedEnvironmentIds); $notFound = array_diff($specifiedEnvironmentIds, array_keys($environments)); if (!empty($notFound)) { // Refresh the environments list if any environment is not found. @@ -146,7 +148,8 @@ function ($environment) { // Exclude environment ID(s) specified in --exclude. $excludeIds = ArrayArgument::getOption($input, 'exclude'); if (!empty($excludeIds)) { - $resolved = $this->findEnvironmentIdsByWildcards($selectedEnvironments, $excludeIds); + $selectedIds = \array_map(function (Environment $e) { return $e->id; }, $selectedEnvironments); + $resolved = Wildcard::select($selectedIds, $excludeIds); if (count($resolved)) { $selectedEnvironments = \array_filter($selectedEnvironments, function (Environment $e) use ($resolved) { return !\in_array($e->id, $resolved, true); @@ -234,25 +237,6 @@ function ($environment) { return $success ? 0 : 1; } - /** - * Finds environments in a list matching a list of ID wildcards. - * - * @param Environment[] $environments - * @param string[] $wildcards - * - * @return string[] - */ - private function findEnvironmentIDsByWildcards(array $environments, $wildcards) - { - $ids = \array_map(function (Environment $e) { return $e->id; }, $environments); - $found = []; - foreach ($wildcards as $wildcard) { - $pattern = '/^' . str_replace('%', '.*', preg_quote($wildcard)) . '$/'; - $found = array_merge($found, \preg_grep($pattern, $ids)); - } - return \array_unique($found); - } - /** * @param array $toDeactivate * @param array $toDeleteBranch diff --git a/src/Command/User/UserAddCommand.php b/src/Command/User/UserAddCommand.php index a6436b728a..00c2d40573 100644 --- a/src/Command/User/UserAddCommand.php +++ b/src/Command/User/UserAddCommand.php @@ -810,7 +810,7 @@ private function getSpecifiedEnvironmentRoles(array $roles) $role = $this->validateEnvironmentRole($role); // Match environment IDs by wildcard. if (strpos($id, '%') !== false) { - $pattern = '/^' . str_replace('%', '.*', preg_quote($id)) . '$/'; + $pattern = '/^' . str_replace('%', '.*', preg_quote($id, '/')) . '$/'; $matched = preg_grep($pattern, array_keys($environments)); if (empty($matched)) { throw new InvalidArgumentException('No environment IDs match: ' . $id); @@ -856,7 +856,7 @@ private function getSpecifiedEnvironmentTypeRoles(array &$roles) $role = $this->validateEnvironmentRole($role); // Match type IDs by wildcard. if (strpos($id, '%') !== false) { - $pattern = '/^' . str_replace('%', '.*', preg_quote($id)) . '$/'; + $pattern = '/^' . str_replace('%', '.*', preg_quote($id, '/')) . '$/'; $matched = preg_grep($pattern, $typeIds); if (empty($matched)) { throw new InvalidArgumentException('No environment type IDs match: ' . $id); diff --git a/src/Util/Wildcard.php b/src/Util/Wildcard.php new file mode 100644 index 0000000000..613a11cda2 --- /dev/null +++ b/src/Util/Wildcard.php @@ -0,0 +1,24 @@ + $case) { + list($subjects, $wildcards, $result) = $case; + $this->assertEquals($result, Wildcard::select($subjects, $wildcards), "Case $i"); + } + } +}